aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc Pervaz Boocha <mboocha@sudomsg.com>2025-08-02 21:51:53 +0530
committerMarc Pervaz Boocha <mboocha@sudomsg.com>2025-08-02 21:51:53 +0530
commit7c86629d3b164c9fd79dc43ba894ed5c8938446e (patch)
tree3818a97deea7476d73631c5a471fccb4d964c0f8
parentInitial Commit (diff)
downloadkit-7c86629d3b164c9fd79dc43ba894ed5c8938446e.tar
kit-7c86629d3b164c9fd79dc43ba894ed5c8938446e.tar.gz
kit-7c86629d3b164c9fd79dc43ba894ed5c8938446e.tar.bz2
kit-7c86629d3b164c9fd79dc43ba894ed5c8938446e.tar.lz
kit-7c86629d3b164c9fd79dc43ba894ed5c8938446e.tar.xz
kit-7c86629d3b164c9fd79dc43ba894ed5c8938446e.tar.zst
kit-7c86629d3b164c9fd79dc43ba894ed5c8938446e.zip
Fixes: Misc Fixes and Doc improvementsv0.1.1
-rw-r--r--http/server.go4
-rw-r--r--logging/log.go42
2 files changed, 43 insertions, 3 deletions
diff --git a/http/server.go b/http/server.go
index e13bc67..dc74008 100644
--- a/http/server.go
+++ b/http/server.go
@@ -48,7 +48,7 @@ func (ls Listeners) CloseAll() error {
// Address is the socket address, e.g., ":8080".
type ServerConfig struct {
Network string `toml:"network"`
- Address string `toml:"network"`
+ Address string `toml:"address"`
}
// OpenConfigListeners opens network listeners as specified by the provided ServerConfig slice.
@@ -105,7 +105,7 @@ func RunHTTPServers(ctx context.Context, lns Listeners, handler http.Handler) er
for _, ln := range lns {
g.Go(func() error {
- logger, ctx := logging.With(ctx, "address", ln.Addr)
+ logger, ctx := logging.With(ctx, "address", ln.Addr())
srv := &http.Server{
Addr: ln.Addr().String(),
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