123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- package fslog
- import (
- "go.uber.org/zap"
- "go.uber.org/zap/zapcore"
- "gopkg.in/natefinch/lumberjack.v2"
- "io"
- "os"
- )
- var log *Logger
- func init() {
- consoleCfg := zap.NewDevelopmentEncoderConfig()
- consoleCfg.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05")
- jsonCfg := zap.NewProductionEncoderConfig()
- jsonCfg.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05")
- log = &Logger{
- lv: zap.NewAtomicLevelAt(zap.DebugLevel),
- }
- log.cores = append(log.cores,
- zapcore.NewCore(zapcore.NewConsoleEncoder(consoleCfg), zapcore.AddSync(os.Stdout), log.lv),
- zapcore.NewCore(zapcore.NewJSONEncoder(jsonCfg), zapcore.AddSync(&lumberjack.Logger{
- Filename: "logs/log",
- MaxSize: 1,
- MaxAge: 10,
- LocalTime: true,
- }), log.lv),
- )
- log.flushLogger()
- }
- // With 见 Logger.With
- func With(k string, v any) *Logger {
- return log.With(k, v)
- }
- // Info 见 Logger.Info
- func Info(msg string, vs ...any) *Logger {
- log.Info(msg, vs...)
- return log
- }
- // Debug 见 Logger.Debug
- func Debug(msg string, vs ...any) *Logger {
- log.Debug(msg, vs...)
- return log
- }
- // Warn 见 Logger.Warn
- func Warn(msg string, vs ...any) *Logger {
- log.Warn(msg, vs...)
- return log
- }
- // Error 见 Logger.Error
- func Error(vs ...any) *Logger {
- log.Error(vs...)
- return log
- }
- // Lv 见 Logger.Lv
- func Lv() Level {
- return log.Lv()
- }
- // SetLv 见 Logger.SetLv
- func SetLv(lv Level) *Logger {
- log.SetLv(lv)
- return log
- }
- // NewFileOutput 见 Logger.NewFileOutput
- func NewFileOutput(opts ...FileOutputOpt) *Logger {
- log.NewFileOutput(opts...)
- return log
- }
- // NewOutput 见 Logger.NewOutput
- func NewOutput(writer io.Writer) *Logger {
- log.NewOutput(writer)
- return log
- }
- // SetLogger 替换fslog包默认的log对象
- // Logger 支持了zapcore级别的底层配置,见 Logger
- func SetLogger(logger *Logger) *Logger {
- log = logger
- return log
- }
- // Flush 见 Logger.Flush
- func Flush() {
- log.Flush()
- }
|