Skip to content

Commit

Permalink
add logger root method (#20)
Browse files Browse the repository at this point in the history
* add logger root methoe

* rotate date

* modify rotate date

* rotate by date

---------

Co-authored-by: chuyali <[email protected]>
  • Loading branch information
chuyali and chuyali authored Sep 26, 2023
1 parent db5a1bc commit abf20c8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
23 changes: 23 additions & 0 deletions logger/customize_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,26 @@ func newCustomizeRootLogger(level string, options int, ws io.Writer) (*zap.Logge
l := zap.New(core, zap.AddCaller())
return l, nil
}

func newOnlyMessageRootLogger(ws io.Writer) (*zap.Logger, error) {
// use lumberjack.Logger for rotate
w := zapcore.AddSync(ws)

cfg := zapcore.EncoderConfig{
MessageKey: NameKeyMsg,

EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeCaller: zapcore.FullCallerEncoder,
}

//enc := zapcore.NewJSONEncoder(cfg)
enc := zapcore.NewConsoleEncoder(cfg)
lvl := zap.DebugLevel

core := zapcore.NewCore(enc, w, lvl)
// NOTE: why need add another option while
// zapcore.ShortCallerEncoder/FullCallerEncoder been set
l := zap.New(core, zap.AddCaller())
return l, nil
}
8 changes: 5 additions & 3 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ type Logger struct {
}

type Option struct {
Path string
Level string
Flags int
Path string
Level string
MaxSize int
Flags int
Compress bool
}

func Reset() {
Expand Down
44 changes: 39 additions & 5 deletions logger/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os"
"path/filepath"
"strings"
"time"

"go.uber.org/zap"
"gopkg.in/natefinch/lumberjack.v2"
Expand Down Expand Up @@ -64,11 +65,11 @@ func SetGlobalRootLogger(fpath, level string, options int) error {
}

// InitRoot used to setup global root logger, include
// - log level
// - log path
// - set to disk file(with or without rotate)
// - set to some remtoe TCP/UDP server
// - a bounch of other OPT_XXXs
// - log level
// - log path
// - set to disk file(with or without rotate)
// - set to some remtoe TCP/UDP server
// - a bounch of other OPT_XXXs
func InitRoot(opt *Option) error {
if opt == nil {
opt = defaultOption
Expand Down Expand Up @@ -149,3 +150,36 @@ func newRootLogger(fpath, level string, options int) (*zap.Logger, error) {

return newNormalRootLogger(fpath, level, options)
}

// InitCustomizeRoot used to setup global root logger, include
// - log path
// - log maxsize
// - log compress

func InitCustomizeRoot(opt *Option) (*zap.Logger, error) {
mtx.Lock()
defer mtx.Unlock()

lumberLog := &lumberjack.Logger{
Filename: opt.Path,
MaxSize: opt.MaxSize,
Compress: opt.Compress,
}
go func() {
next := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()+1, 0, 0, 0, 0, time.Local)
after := next.Unix() - time.Now().Unix() - 1

time.Sleep(time.Duration(after) * time.Second)
lumberLog.Rotate()

ticker := time.NewTicker(24 * time.Hour)
defer ticker.Stop()
for {
select {
case <-ticker.C:
lumberLog.Rotate()
}
}
}()
return newOnlyMessageRootLogger(lumberLog)
}

0 comments on commit abf20c8

Please sign in to comment.