Logging: structured slog messages throughout, and WithLogger options so callers choose the logger
Log messages are lower-case event names with their details as key/value
attributes ("error" for errors), in place of formatted strings.
Loggable package take a WithLogger option as part of their "New*" call,
defaulting to slog.Default(). Each tags its lines with its `component`.
Some debug loggers print directly rather than using slog when in
terminal logging mode.
This commit is contained in:
1 parent
0b5fde9563
commit
043e307663
49 files changed
+598
-248
No files matched your search
@@ -93,7 +93,7 @@ func TestInitNoConfigFile(t *testing.T) {
|
||||
if err := Init("", &conf); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(logs.String(), "No config file found") {
|
||||
if !strings.Contains(logs.String(), "no config file found") {
|
||||
t.Errorf("no warning logged: %s", logs)
|
||||
}
|
||||
if conf.Logging.Level != "warn" {
|
||||
@@ -116,7 +116,7 @@ func TestInitKeepsCommandLineLevel(t *testing.T) {
|
||||
t.Errorf("Logging.Level = %q; want %s kept", conf.Logging.Level, level)
|
||||
}
|
||||
// At debug, the environment and loaded config are logged.
|
||||
gotDebug := strings.Contains(logs.String(), "Loaded config:")
|
||||
gotDebug := strings.Contains(logs.String(), "loaded config value")
|
||||
if gotDebug != (level == "debug") {
|
||||
t.Errorf("debug output logged = %v at level %s: %s", gotDebug, level, logs)
|
||||
}
|
||||
@@ -241,9 +241,9 @@ func TestPrintLoadedConfig(t *testing.T) {
|
||||
|
||||
out := logs.String()
|
||||
for _, want := range []string{
|
||||
`logging.level = \"info\" <string>`,
|
||||
`server.websocket.connection_limit.enabled = true <bool>`,
|
||||
`database.file = \"/tmp/js.db\" <string>`,
|
||||
`key=logging.level value=info type=string`,
|
||||
`key=server.websocket.connection_limit.enabled value=true type=bool`,
|
||||
`key=database.file value=/tmp/js.db type=string`,
|
||||
} {
|
||||
if !strings.Contains(out, want) {
|
||||
t.Errorf("missing %q in:\n%s", want, out)
|
||||
|
||||
Reference in new issue
Block a user