@@ -5,9 +5,7 @@ go 1.15 | |||||
require ( | require ( | ||||
github.com/go-redis/redis v6.15.9+incompatible | github.com/go-redis/redis v6.15.9+incompatible | ||||
github.com/go-sql-driver/mysql v1.6.0 | github.com/go-sql-driver/mysql v1.6.0 | ||||
github.com/gomodule/redigo v1.8.8 | |||||
go.uber.org/zap v1.16.0 | |||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0 | |||||
xorm.io/builder v0.3.10 // indirect | |||||
xorm.io/xorm v1.3.0 | |||||
github.com/gomodule/redigo/redis v0.0.1 | |||||
go.uber.org/zap v1.23.0 | |||||
xorm.io/xorm v1.3.1 | |||||
) | ) |
@@ -46,42 +46,6 @@ func defaultLogger() *LogX { | |||||
return &LogX{logger: lg, atomLevel: lv} | return &LogX{logger: lg, atomLevel: lv} | ||||
} | } | ||||
// initial standard log, if you don't init, it will use default logger setting | |||||
func InitDefaultLogger(cfg *LogConfig) { | |||||
var writers []zapcore.WriteSyncer | |||||
if cfg.IsStdOut || (!cfg.IsStdOut && !cfg.IsFileOut) { | |||||
writers = append(writers, os.Stdout) | |||||
} | |||||
if cfg.IsFileOut { | |||||
writers = append(writers, NewRollingFile(cfg.FileDir, cfg.FileName, cfg.FileMaxSize, cfg.FileMaxAge)) | |||||
} | |||||
lg, lv := newZapLogger(setLogLevel(cfg.Level), setLogLevel(cfg.StacktraceLevel), cfg.Encoding, cfg.TimeFormat, cfg.Skip, zapcore.NewMultiWriteSyncer(writers...)) | |||||
zap.RedirectStdLog(lg) | |||||
if cfg.AppName != "" { | |||||
lg = lg.With(zap.String("app", cfg.AppName)) // 加上应用名称 | |||||
} | |||||
l = &LogX{logger: lg, atomLevel: lv} | |||||
} | |||||
// create a new logger | |||||
func NewLogger(cfg *LogConfig) *LogX { | |||||
var writers []zapcore.WriteSyncer | |||||
if cfg.IsStdOut || (!cfg.IsStdOut && !cfg.IsFileOut) { | |||||
writers = append(writers, os.Stdout) | |||||
} | |||||
if cfg.IsFileOut { | |||||
writers = append(writers, NewRollingFile(cfg.FileDir, cfg.FileName, cfg.FileMaxSize, cfg.FileMaxAge)) | |||||
} | |||||
lg, lv := newZapLogger(setLogLevel(cfg.Level), setLogLevel(cfg.StacktraceLevel), cfg.Encoding, cfg.TimeFormat, cfg.Skip, zapcore.NewMultiWriteSyncer(writers...)) | |||||
zap.RedirectStdLog(lg) | |||||
if cfg.AppName != "" { | |||||
lg = lg.With(zap.String("app", cfg.AppName)) // 加上应用名称 | |||||
} | |||||
return &LogX{logger: lg, atomLevel: lv} | |||||
} | |||||
// create a new zaplog logger | // create a new zaplog logger | ||||
func newZapLogger(level, stacktrace zapcore.Level, encoding, timeType string, skip int, output zapcore.WriteSyncer) (*zap.Logger, *zap.AtomicLevel) { | func newZapLogger(level, stacktrace zapcore.Level, encoding, timeType string, skip int, output zapcore.WriteSyncer) (*zap.Logger, *zap.AtomicLevel) { | ||||
encCfg := zapcore.EncoderConfig{ | encCfg := zapcore.EncoderConfig{ | ||||
@@ -1,105 +0,0 @@ | |||||
package zhios_o2o_business_logx | |||||
import ( | |||||
"bytes" | |||||
"io" | |||||
"os" | |||||
"path/filepath" | |||||
"time" | |||||
"gopkg.in/natefinch/lumberjack.v2" | |||||
) | |||||
// output interface | |||||
type WriteSyncer interface { | |||||
io.Writer | |||||
Sync() error | |||||
} | |||||
// split writer | |||||
func NewRollingFile(dir, filename string, maxSize, MaxAge int) WriteSyncer { | |||||
s, err := os.Stat(dir) | |||||
if err != nil || !s.IsDir() { | |||||
os.RemoveAll(dir) | |||||
if err := os.MkdirAll(dir, 0766); err != nil { | |||||
panic(err) | |||||
} | |||||
} | |||||
return newLumberjackWriteSyncer(&lumberjack.Logger{ | |||||
Filename: filepath.Join(dir, filename), | |||||
MaxSize: maxSize, // megabytes, MB | |||||
MaxAge: MaxAge, // days | |||||
LocalTime: true, | |||||
Compress: false, | |||||
}) | |||||
} | |||||
type lumberjackWriteSyncer struct { | |||||
*lumberjack.Logger | |||||
buf *bytes.Buffer | |||||
logChan chan []byte | |||||
closeChan chan interface{} | |||||
maxSize int | |||||
} | |||||
func newLumberjackWriteSyncer(l *lumberjack.Logger) *lumberjackWriteSyncer { | |||||
ws := &lumberjackWriteSyncer{ | |||||
Logger: l, | |||||
buf: bytes.NewBuffer([]byte{}), | |||||
logChan: make(chan []byte, 5000), | |||||
closeChan: make(chan interface{}), | |||||
maxSize: 1024, | |||||
} | |||||
go ws.run() | |||||
return ws | |||||
} | |||||
func (l *lumberjackWriteSyncer) run() { | |||||
ticker := time.NewTicker(1 * time.Second) | |||||
for { | |||||
select { | |||||
case <-ticker.C: | |||||
if l.buf.Len() > 0 { | |||||
l.sync() | |||||
} | |||||
case bs := <-l.logChan: | |||||
_, err := l.buf.Write(bs) | |||||
if err != nil { | |||||
continue | |||||
} | |||||
if l.buf.Len() > l.maxSize { | |||||
l.sync() | |||||
} | |||||
case <-l.closeChan: | |||||
l.sync() | |||||
return | |||||
} | |||||
} | |||||
} | |||||
func (l *lumberjackWriteSyncer) Stop() { | |||||
close(l.closeChan) | |||||
} | |||||
func (l *lumberjackWriteSyncer) Write(bs []byte) (int, error) { | |||||
b := make([]byte, len(bs)) | |||||
for i, c := range bs { | |||||
b[i] = c | |||||
} | |||||
l.logChan <- b | |||||
return 0, nil | |||||
} | |||||
func (l *lumberjackWriteSyncer) Sync() error { | |||||
return nil | |||||
} | |||||
func (l *lumberjackWriteSyncer) sync() error { | |||||
defer l.buf.Reset() | |||||
_, err := l.Logger.Write(l.buf.Bytes()) | |||||
if err != nil { | |||||
return err | |||||
} | |||||
return nil | |||||
} |