Skip to content

Instantly share code, notes, and snippets.

@panta
Last active May 27, 2025 21:01
Show Gist options
  • Select an option

  • Save panta/2530672ca641d953ae452ecb5ef79d7d to your computer and use it in GitHub Desktop.

Select an option

Save panta/2530672ca641d953ae452ecb5ef79d7d 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
}
}
@douglarek
Copy link
Copy Markdown

https://gist.github.com/panta/2530672ca641d953ae452ecb5ef79d7d#file-logging-go-L77 is needless, lubmberjack will detect it and make all if needed.

@panta
Copy link
Copy Markdown
Author

panta commented May 14, 2021

https://gist.github.com/panta/2530672ca641d953ae452ecb5ef79d7d#file-logging-go-L77 is needless, lubmberjack will detect it and make all if needed.

I didn't know that, thank you Lingchao!

@ydm
Copy link
Copy Markdown

ydm commented Aug 19, 2021

Thanks! :)

@mcwalrus
Copy link
Copy Markdown

mcwalrus commented Aug 1, 2023

Nice!

@fkirill
Copy link
Copy Markdown

fkirill commented Oct 28, 2024

We shouldn't report about log configuration issues using the logging, it should be plain Printf, or using Go's standard logging.
https://gist.github.com/panta/2530672ca641d953ae452ecb5ef79d7d#file-logging-go-L78

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment