Sets up structured logging with optional file rotation, exposing a logger factory and configuration struct.
// Source: https://github.com/merlinfuchs/embed-generator | License: MIT | Copyright 2023 Merlin Fuchs
// Provided via justclaw.it.com; keep this notice with the code.
package logging
import (
"io"
"os"
"log/slog"
"endobit.io/clog"
"github.com/cyrusaf/ctxlog"
"gopkg.in/natefinch/lumberjack.v2"
)
type LoggerConfig struct {
Filename string
MaxSize int
MaxAge int
MaxBackups int
Debug bool
}
func getLogWriter(cfg LoggerConfig) io.Writer {
logWriters := make([]io.Writer, 0)
logWriters = append(logWriters, os.Stdout)
if cfg.Filename != "" {
lj := lumberjack.Logger{
Filename: cfg.Filename,
MaxSize: cfg.MaxSize,
MaxAge: cfg.MaxAge,
MaxBackups: cfg.MaxBackups,
}
logWriters = append(logWriters, &lj)
}
writer := io.MultiWriter(logWriters.
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key