Skip to content

Commit

Permalink
feat: support custom filename for init log (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
qloog authored Aug 3, 2023
1 parent 3e08388 commit bf3b452
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 28 deletions.
6 changes: 2 additions & 4 deletions config/docker/logger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ Encoding: json # json or console
Level: info # 日志级别,INFO, WARN, ERROR
Name: eagle
Writers: console # 有2个可选项:file,console 选择file会将日志记录到logger_file指定的日志文件中,选择console会将日志输出到标准输出,当然也可以两者同时选择
LoggerFile: /tmp/log/eagle.log
LoggerWarnFile: /tmp/log/eagle.wf.log
LoggerErrorFile: /tmp/log/eagle.err.log
LoggerFile: /data/logs
LogRollingPolicy: daily
LogRotateDate: 1
LogRotateSize: 1
LogBackupCount: 7
LogBackupCount: 7
6 changes: 2 additions & 4 deletions config/local/logger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ Encoding: json # json or console
Level: info # 日志级别,INFO, WARN, ERROR
Name: eagle
Writers: console # 有2个可选项:file,console 选择file会将日志记录到logger_file指定的日志文件中,选择console会将日志输出到标准输出,当然也可以两者同时选择
LoggerFile: /tmp/log/eagle.log
LoggerWarnFile: /tmp/log/eagle.wf.log
LoggerErrorFile: /tmp/log/eagle.err.log
LoggerFile: /data/logs
LogRollingPolicy: daily
LogRotateDate: 1
LogRotateSize: 1
LogBackupCount: 7
LogBackupCount: 7
12 changes: 12 additions & 0 deletions examples/log/logger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Development: false
DisableCaller: false
DisableStacktrace: false
Encoding: json # json or console
Level: info # 日志级别,INFO, WARN, ERROR
Name: eagle
Writers: file # 有2个可选项:file,console 选择file会将日志记录到logger_file指定的日志文件中,选择console会将日志输出到标准输出,当然也可以两者同时选择
LoggerDir: /tmp/logs
LogRollingPolicy: daily
LogRotateDate: 1
LogRotateSize: 1
LogBackupCount: 7
23 changes: 23 additions & 0 deletions examples/log/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"github.com/go-eagle/eagle/pkg/config"
"github.com/go-eagle/eagle/pkg/log"
)

func main() {
// load config
_ = config.New(".")

// print log using default filename
log.Init()
log.Info("test log")

// print log using custom filename
log.Init(log.WithFilename("custom"))
log.Info("test log with filename")

// print log using custom dir and filename
log.Init(log.WithFilename("user/custom"))
log.Info("test log with dir and filename")
}
6 changes: 2 additions & 4 deletions pkg/log/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ type Config struct {
DisableStacktrace bool
Encoding string
Level string
Name string
Name string // service name
Writers string
LoggerFile string
LoggerWarnFile string
LoggerErrorFile string
LoggerDir string
LogFormatText bool
LogRollingPolicy string
LogBackupCount uint
Expand Down
8 changes: 4 additions & 4 deletions pkg/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,27 @@ func loadConf() (ret *Config, err error) {
}

// Init init log
func Init() Logger {
func Init(opts ...Option) Logger {
var err error
cfg, err := loadConf()
if err != nil {
panic(fmt.Sprintf("load logger conf err: %v", err))
}

// new zap logger
zl, err = newZapLogger(cfg)
zl, err = newZapLogger(cfg, opts...)
if err != nil {
_ = fmt.Errorf("init newZapLogger err: %v", err)
}
_ = zl

// log 用于支持模块级的方法调用,所以要比其他 Logger 多跳一层
log, err = newLoggerWithCallerSkip(cfg, 1)
log, err = newLoggerWithCallerSkip(cfg, 1, opts...)
if err != nil {
_ = fmt.Errorf("init newLogger err: %v", err)
}

logger, err = newLogger(cfg)
logger, err = newLogger(cfg, opts...)
if err != nil {
_ = fmt.Errorf("init logger err: %v", err)
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/log/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package log

type Option func(*Config)

// WithFilename set log filename
func WithFilename(filename string) Option {
return func(cfg *Config) {
cfg.Name = filename
}
}

// WithLogDir set log dir
func WithLogDir(dir string) Option {
return func(cfg *Config) {
cfg.LoggerDir = dir
}
}
8 changes: 8 additions & 0 deletions pkg/log/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package log

import "github.com/go-eagle/eagle/pkg/utils"

// GetLogFile get log file absolute path
func GetLogFile(filename string, suffix string) string {
return utils.ConcatString(logDir, "/", hostname, "/", filename, suffix)
}
53 changes: 41 additions & 12 deletions pkg/log/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@ import (
"sync"
"time"

"github.com/go-eagle/eagle/pkg/utils"

rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"github.com/go-eagle/eagle/pkg/utils"
)

const (
// WriterConsole console输出
WriterConsole = "console"
// WriterFile 文件输出
WriterFile = "file"

logSuffix = ".log"
warnLogSuffix = "_warn.log"
errorLogSuffix = "_error.log"
)

const (
Expand All @@ -30,6 +34,11 @@ const (

const defaultSkip = 1 // zapLogger 包装了一层 zap.Logger,默认要跳过一层

var (
hostname string
logDir string
)

// For mapping config logger to app logger levels
var loggerLevelMap = map[string]zapcore.Level{
"debug": zapcore.DebugLevel,
Expand Down Expand Up @@ -59,21 +68,35 @@ type zapLogger struct {
}

// newZapLogger new zap logger
func newZapLogger(cfg *Config) (*zap.Logger, error) {
func newZapLogger(cfg *Config, opts ...Option) (*zap.Logger, error) {
for _, opt := range opts {
opt(cfg)
}
return buildLogger(cfg, defaultSkip), nil
}

// newLoggerWithCallerSkip new logger with caller skip
func newLoggerWithCallerSkip(cfg *Config, skip int) (Logger, error) {
func newLoggerWithCallerSkip(cfg *Config, skip int, opts ...Option) (Logger, error) {
for _, opt := range opts {
opt(cfg)
}
return &zapLogger{sugarLogger: buildLogger(cfg, defaultSkip+skip).Sugar()}, nil
}

// newLogger new logger
func newLogger(cfg *Config) (Logger, error) {
func newLogger(cfg *Config, opts ...Option) (Logger, error) {
for _, opt := range opts {
opt(cfg)
}
return newLoggerWithCallerSkip(cfg, 0)
}

func buildLogger(cfg *Config, skip int) *zap.Logger {
logDir = cfg.LoggerDir
if strings.HasSuffix(logDir, "/") {
logDir = strings.TrimRight(logDir, "/")
}

var encoderCfg zapcore.EncoderConfig
if cfg.Development {
encoderCfg = zap.NewDevelopmentEncoderConfig()
Expand All @@ -92,7 +115,7 @@ func buildLogger(cfg *Config, skip int) *zap.Logger {
var cores []zapcore.Core
var options []zap.Option
// init option
hostname, _ := os.Hostname()
hostname, _ = os.Hostname()
option := zap.Fields(
zap.String("ip", utils.GetLocalIP()),
zap.String("app_id", cfg.Name),
Expand Down Expand Up @@ -147,23 +170,23 @@ func buildLogger(cfg *Config, skip int) *zap.Logger {
}

func getAllCore(encoder zapcore.Encoder, cfg *Config) zapcore.Core {
allWriter := getLogWriterWithTime(cfg, cfg.LoggerFile)
allWriter := getLogWriterWithTime(cfg, GetLogFile(cfg.Name, logSuffix))
allLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl <= zapcore.FatalLevel
})
return zapcore.NewCore(encoder, zapcore.AddSync(allWriter), allLevel)
}

func getInfoCore(encoder zapcore.Encoder, cfg *Config) zapcore.Core {
infoWrite := getLogWriterWithTime(cfg, cfg.LoggerFile)
infoWrite := getLogWriterWithTime(cfg, GetLogFile(cfg.Name, logSuffix))
infoLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl <= zapcore.InfoLevel
})
return zapcore.NewCore(encoder, zapcore.AddSync(infoWrite), infoLevel)
}

func getWarnCore(encoder zapcore.Encoder, cfg *Config) (zapcore.Core, zap.Option) {
warnWrite := getLogWriterWithTime(cfg, cfg.LoggerWarnFile)
warnWrite := getLogWriterWithTime(cfg, GetLogFile(cfg.Name, warnLogSuffix))
var stacktrace zap.Option
warnLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
if !cfg.DisableCaller {
Expand All @@ -177,7 +200,7 @@ func getWarnCore(encoder zapcore.Encoder, cfg *Config) (zapcore.Core, zap.Option
}

func getErrorCore(encoder zapcore.Encoder, cfg *Config) (zapcore.Core, zap.Option) {
errorFilename := cfg.LoggerErrorFile
errorFilename := GetLogFile(cfg.Name, errorLogSuffix)
errorWrite := getLogWriterWithTime(cfg, errorFilename)
var stacktrace zap.Option
errorLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
Expand All @@ -197,14 +220,20 @@ func getLogWriterWithTime(cfg *Config, filename string) io.Writer {
rotationPolicy := cfg.LogRollingPolicy
backupCount := cfg.LogBackupCount
// 默认
var rotateDuration time.Duration
var (
rotateDuration time.Duration
// 时间格式使用shell的date时间格式
timeFormat string
)
if rotationPolicy == RotateTimeHourly {
rotateDuration = time.Hour
timeFormat = ".%Y%m%d%H"
} else if rotationPolicy == RotateTimeDaily {
rotateDuration = time.Hour * 24
timeFormat = ".%Y%m%d"
}
hook, err := rotatelogs.New(
logFullPath+".%Y%m%d%H", // 时间格式使用shell的date时间格式
logFullPath+timeFormat,
rotatelogs.WithLinkName(logFullPath), // 生成软链,指向最新日志文件
rotatelogs.WithRotationCount(backupCount), // 文件最大保存份数
rotatelogs.WithRotationTime(rotateDuration), // 日志切割时间间隔
Expand Down
12 changes: 12 additions & 0 deletions pkg/utils/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ func ConcatString(s ...string) string {
return buffer.String()
}

// ConcatStringBySlash concat string by slash
func ConcatStringBySlash(s ...string) string {
var buffer bytes.Buffer
for idx, i := range s {
buffer.WriteString(i)
if idx != len(s)-1 {
buffer.WriteString("/")
}
}
return buffer.String()
}

// StringToUint64 字符串转uint64
func StringToUint64(str string) (uint64, error) {
if str == "" {
Expand Down

0 comments on commit bf3b452

Please sign in to comment.