diff options
author | Marc Pervaz Boocha <mboocha@sudomsg.com> | 2025-08-02 21:51:53 +0530 |
---|---|---|
committer | Marc Pervaz Boocha <mboocha@sudomsg.com> | 2025-08-02 21:51:53 +0530 |
commit | 7c86629d3b164c9fd79dc43ba894ed5c8938446e (patch) | |
tree | 3818a97deea7476d73631c5a471fccb4d964c0f8 /logging | |
parent | Initial Commit (diff) | |
download | kit-d427c236c184747352c6c496f6adb42cc28a91d4.tar kit-d427c236c184747352c6c496f6adb42cc28a91d4.tar.gz kit-d427c236c184747352c6c496f6adb42cc28a91d4.tar.bz2 kit-d427c236c184747352c6c496f6adb42cc28a91d4.tar.lz kit-d427c236c184747352c6c496f6adb42cc28a91d4.tar.xz kit-d427c236c184747352c6c496f6adb42cc28a91d4.tar.zst kit-d427c236c184747352c6c496f6adb42cc28a91d4.zip |
Fixes: Misc Fixes and Doc improvementsv0.1.1
Diffstat (limited to 'logging')
-rw-r--r-- | logging/log.go | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/logging/log.go b/logging/log.go index 8dc8017..5f9af8d 100644 --- a/logging/log.go +++ b/logging/log.go @@ -81,14 +81,35 @@ func RecoverAndLog(ctx context.Context, msg string, err any) { logger.ErrorContext(ctx, msg, "Error", err, "stack", string(buf)) } +// LogSink defines where log output should be written. +// +// It is used to configure the log destination in Setup. type LogSink string const ( + // SinkStdout directs logs to the standard output (os.Stdout). SinkStdout LogSink = "stdout" + + // SinkStderr directs logs to the standard error (os.Stderr). SinkStderr LogSink = "stderr" - SinkFile LogSink = "file" + + // SinkFile directs logs to a file specified by the File field in LogConfig. + // + // If configured, the File field must be a valid path for writing logs. + SinkFile LogSink = "file" ) +// LogConfig holds configuration for the logging setup. +// +// Level specifies the minimum level for logs (e.g., slog.LevelInfo). +// +// Sink specifies the output destination (e.g., SinkStdout, SinkStderr, SinkFile). +// +// Format specifies the log format, either "json" or "text". +// If empty or "json", JSON format is used. +// +// File specifies the output file path when SinkFile is selected. +// It is required if SinkFile is used. type LogConfig struct { Level slog.Level `toml:"level"` Sink LogSink `toml:"sink"` @@ -96,6 +117,25 @@ type LogConfig struct { File string `toml:"file"` } +// Setup initializes the default slog.Logger based on the provided LogConfig. +// +// It configures the log output sink, log format, and log level. +// +// Returns an error if the configuration is invalid or if opening the log file fails. +// +// Example: +// +// cfg := LogConfig{ +// Level: slog.LevelInfo, +// Sink: SinkFile, +// Format: "json", +// File: "/var/log/myapp.log", +// } +// if err := Setup(cfg); err != nil { +// log.Fatalf("failed to configure logging: %v", err) +// } +// +// After a successful call, the global slog logger will log accordingly. func Setup(cfg LogConfig) error { var w io.Writer |