2019-03-01 13:37:13 +00:00
|
|
|
package logutils
|
|
|
|
|
|
|
|
import (
|
2024-10-24 21:46:23 +00:00
|
|
|
"go.uber.org/zap/zapcore"
|
2021-08-18 12:44:10 +00:00
|
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2019-03-01 13:37:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// FileOptions are all options supported by internal rotation module.
|
|
|
|
type FileOptions struct {
|
|
|
|
// Base name for log file.
|
|
|
|
Filename string
|
|
|
|
// Size in megabytes.
|
|
|
|
MaxSize int
|
|
|
|
// Number of rotated log files.
|
|
|
|
MaxBackups int
|
|
|
|
// If true rotated log files will be gzipped.
|
|
|
|
Compress bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// FileHandlerWithRotation instantiates log.Handler with a configured rotation
|
|
|
|
func FileHandlerWithRotation(opts FileOptions, format log.Format) log.Handler {
|
|
|
|
logger := &lumberjack.Logger{
|
|
|
|
Filename: opts.Filename,
|
|
|
|
MaxSize: opts.MaxSize,
|
|
|
|
MaxBackups: opts.MaxBackups,
|
|
|
|
Compress: opts.Compress,
|
|
|
|
}
|
|
|
|
return log.StreamHandler(logger, format)
|
|
|
|
}
|
2024-10-24 21:46:23 +00:00
|
|
|
|
|
|
|
// ZapSyncerWithRotation creates a zapcore.WriteSyncer with a configured rotation
|
|
|
|
func ZapSyncerWithRotation(opts FileOptions) zapcore.WriteSyncer {
|
|
|
|
return zapcore.AddSync(&lumberjack.Logger{
|
|
|
|
Filename: opts.Filename,
|
|
|
|
MaxSize: opts.MaxSize,
|
|
|
|
MaxBackups: opts.MaxBackups,
|
|
|
|
Compress: opts.Compress,
|
|
|
|
})
|
|
|
|
}
|