Skip to content

Instantly share code, notes, and snippets.

@maestre3d
Forked from panta/logging.go
Created March 21, 2022 02:06
Show Gist options
  • Save maestre3d/50d2522fa511dd11ec3f2973b26ebbf4 to your computer and use it in GitHub Desktop.
Save maestre3d/50d2522fa511dd11ec3f2973b26ebbf4 to your computer and use it in GitHub Desktop.
zerolog with file log rotation (lumberjack) and console output
package logging
import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"gopkg.in/natefinch/lumberjack.v2"
"os"
"path"
"io"
)
// Configuration for logging
type Config struct {
// Enable console logging
ConsoleLoggingEnabled bool
// EncodeLogsAsJson makes the log framework log JSON
EncodeLogsAsJson bool
// FileLoggingEnabled makes the framework log to a file
// the fields below can be skipped if this value is false!
FileLoggingEnabled bool
// Directory to log to to when filelogging is enabled
Directory string
// Filename is the name of the logfile which will be placed inside the directory
Filename string
// MaxSize the max size in MB of the logfile before it's rolled
MaxSize int
// MaxBackups the max number of rolled files to keep
MaxBackups int
// MaxAge the max age in days to keep a logfile
MaxAge int
}
type Logger struct {
*zerolog.Logger
}
// Configure sets up the logging framework
//
// In production, the container logs will be collected and file logging should be disabled. However,
// during development it's nicer to see logs as text and optionally write to a file when debugging
// problems in the containerized pipeline
//
// The output log file will be located at /var/log/service-xyz/service-xyz.log and
// will be rolled according to configuration set.
func Configure(config Config) *Logger {
var writers []io.Writer
if config.ConsoleLoggingEnabled {
writers = append(writers, zerolog.ConsoleWriter{Out: os.Stderr})
}
if config.FileLoggingEnabled {
writers = append(writers, newRollingFile(config))
}
mw := io.MultiWriter(writers...)
// zerolog.SetGlobalLevel(zerolog.DebugLevel)
logger := zerolog.New(mw).With().Timestamp().Logger()
logger.Info().
Bool("fileLogging", config.FileLoggingEnabled).
Bool("jsonLogOutput", config.EncodeLogsAsJson).
Str("logDirectory", config.Directory).
Str("fileName", config.Filename).
Int("maxSizeMB", config.MaxSize).
Int("maxBackups", config.MaxBackups).
Int("maxAgeInDays", config.MaxAge).
Msg("logging configured")
return &Logger{
Logger: &logger,
}
}
func newRollingFile(config Config) io.Writer {
if err := os.MkdirAll(config.Directory, 0744); err != nil {
log.Error().Err(err).Str("path", config.Directory).Msg("can't create log directory")
return nil
}
return &lumberjack.Logger{
Filename: path.Join(config.Directory, config.Filename),
MaxBackups: config.MaxBackups, // files
MaxSize: config.MaxSize, // megabytes
MaxAge: config.MaxAge, // days
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment