-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use log/slog
instead of log
#4635
base: main
Are you sure you want to change the base?
Conversation
Azure Dev CLI Install InstructionsInstall scriptsMacOS/Linux
bash:
pwsh:
WindowsPowerShell install
MSI install
Standalone Binary
MSI
Documentationlearn.microsoft.com documentationtitle: Azure Developer CLI reference
|
Go added
log/slog
to the standard library in Go 1.21. It provides a leveled logging API that allows for structured logging. If this package had existed in the standard library when we startedazd
, we would have used it overlog
.This change moves us from
log
tolog/slog
.To do so, I've replaced all the calls to
log.Print
,log.Printf
andlog.Println
with calls toslog.InfoContext
. If acontext.Context
object was available in the method, I used it, otherwise I usedcontext.TODO()
so we can go back and thread the context parameter. Passing the context object like this should allow us to tie these log messages to tracing spans at some point. In practice,slog.InfoContext(context.TODO, ....)
behavesslog.Info(...)
(sincecontext.TODO()
is likecontext.Background()
andslog.Info
callsslog.InfoContext
withcontext.Background()
.While
slog
does have levels, for the first pass, I chose to always useslog.InfoContext
even ifslog.ErrorContext
orslog.WarningContext
may have made more sense based on the log message. We can evolve this over time.Since
slog
does structured logging, it is expected that insead of intermixing the logging message with the data to be logged, that you pass these as separate values, I did light editing of the logging messages when it made sense.Calls to
log.Panic
andlog.Panicf
were replaced with direct calls topanic
(perhaps with a call tofmt.Sprintf
to produce the message to pass to panic) sinceslog
doesn't have helpers likePanic
orFatal
. This does mean we no longer log a message in these cases, but from inspection it felt like this would be OK.The
--debug
output ofazd
is slightly different after this change, since we now have a level printed for the log message and the data is printed after the message:Before:
After: