-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogging.go
More file actions
50 lines (40 loc) · 1.4 KB
/
logging.go
File metadata and controls
50 lines (40 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// SPDX-FileCopyrightText: 2026 The SonicWeb contributors.
// SPDX-License-Identifier: MPL-2.0
package main
import (
"errors"
"fmt"
"log/slog"
"os"
)
var errLogConfig = errors.New("invalid log configuration")
// setupLogging sets the log format and level. It can try to guess in which environment
// SonicWeb runs (logStyle "auto"). If its parent seems to not be an init process, then
// text logging is used, otherwise JSON.
func setupLogging(logLevel string, logStyle string) error {
var parsedLogLevel slog.Level
if levelErr := (&parsedLogLevel).UnmarshalText([]byte(logLevel)); levelErr != nil {
return fmt.Errorf("invalid loglevel: %w", levelErr)
}
options := slog.HandlerOptions{
AddSource: func() bool { return parsedLogLevel <= slog.LevelDebug }(),
Level: parsedLogLevel,
ReplaceAttr: func(_ /*groups*/ []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey {
a.Value = slog.StringValue(a.Value.Time().Format("2006-01-02T15:04:05.000000Z07:00"))
}
return a
},
}
ppid := os.Getppid()
switch {
case (logStyle == "auto" && ppid > 1) || logStyle == "text":
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &options)))
case logStyle == "auto" || logStyle == "json":
options.ReplaceAttr = nil
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &options)))
default:
return fmt.Errorf("unsupported log style %s: %w", logStyle, errLogConfig)
}
return nil
}