golib/logger/log.go

114 lines
2.9 KiB
Go
Raw Normal View History

2023-06-15 21:22:51 +08:00
//
// log.go
// Copyright (C) 2022 tiglog <me@tiglog.com>
//
// Distributed under terms of the MIT license.
//
package logger
import (
2023-08-12 20:16:29 +08:00
"fmt"
2023-08-12 20:03:19 +08:00
"io"
2023-06-15 21:22:51 +08:00
"os"
2023-08-12 20:03:19 +08:00
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
2023-06-15 21:22:51 +08:00
)
2023-08-12 20:03:19 +08:00
type Level = zapcore.Level
2023-06-15 21:22:51 +08:00
2023-08-12 20:03:19 +08:00
const (
DebugLevel = zapcore.DebugLevel
InfoLevel = zapcore.InfoLevel
WarnLevel = zapcore.WarnLevel
ErrorLevel = zapcore.ErrorLevel
PanicLevel = zapcore.PanicLevel
FatalLevel = zapcore.FatalLevel
)
2023-06-15 21:22:51 +08:00
2023-08-12 20:03:19 +08:00
type Logger struct {
l *zap.Logger
// https://pkg.go.dev/go.uber.org/zap#example-AtomicLevel
al *zap.AtomicLevel
2023-06-15 21:22:51 +08:00
}
2023-08-12 20:03:19 +08:00
func New(out io.Writer, level Level, opts ...Option) *Logger {
if out == nil {
out = os.Stderr
}
2023-06-15 21:22:51 +08:00
2023-08-12 20:03:19 +08:00
al := zap.NewAtomicLevelAt(level)
cfg := zap.NewProductionEncoderConfig()
cfg.EncodeTime = zapcore.RFC3339TimeEncoder
2023-06-15 21:22:51 +08:00
2023-08-12 20:03:19 +08:00
core := zapcore.NewCore(
zapcore.NewJSONEncoder(cfg),
zapcore.AddSync(out),
al,
)
return &Logger{l: zap.New(core, opts...), al: &al}
}
2023-06-15 21:22:51 +08:00
2023-08-12 20:03:19 +08:00
// SetLevel 动态更改日志级别
// 对于使用 NewTee 创建的 Logger 无效,因为 NewTee 本意是根据不同日志级别
// 创建的多个 zap.Core不应该通过 SetLevel 将多个 zap.Core 日志级别统一
func (l *Logger) SetLevel(level Level) {
if l.al != nil {
l.al.SetLevel(level)
}
}
2023-06-15 21:22:51 +08:00
2023-08-12 20:03:19 +08:00
type Field = zap.Field
2023-06-15 21:22:51 +08:00
2023-08-12 20:03:19 +08:00
func (l *Logger) Debug(msg string, fields ...Field) {
l.l.Debug(msg, fields...)
2023-06-15 21:22:51 +08:00
}
2023-08-12 20:03:19 +08:00
func (l *Logger) Info(msg string, fields ...Field) {
l.l.Info(msg, fields...)
2023-06-15 21:22:51 +08:00
}
2023-08-12 20:03:19 +08:00
func (l *Logger) Warn(msg string, fields ...Field) {
l.l.Warn(msg, fields...)
2023-06-15 21:22:51 +08:00
}
2023-08-12 20:03:19 +08:00
func (l *Logger) Error(msg string, fields ...Field) {
l.l.Error(msg, fields...)
2023-06-15 21:22:51 +08:00
}
2023-08-12 20:03:19 +08:00
func (l *Logger) Panic(msg string, fields ...Field) {
l.l.Panic(msg, fields...)
2023-06-15 21:22:51 +08:00
}
2023-08-12 20:03:19 +08:00
func (l *Logger) Fatal(msg string, fields ...Field) {
l.l.Fatal(msg, fields...)
2023-06-15 21:22:51 +08:00
}
2023-08-12 20:03:19 +08:00
func (l *Logger) Sync() error {
return l.l.Sync()
2023-06-15 21:22:51 +08:00
}
2023-08-12 20:03:19 +08:00
var std = New(os.Stderr, InfoLevel)
func Default() *Logger { return std }
func ReplaceDefault(l *Logger) { std = l }
func SetLevel(level Level) { std.SetLevel(level) }
func Debug(msg string, fields ...Field) { std.Debug(msg, fields...) }
func Info(msg string, fields ...Field) { std.Info(msg, fields...) }
func Warn(msg string, fields ...Field) { std.Warn(msg, fields...) }
func Error(msg string, fields ...Field) { std.Error(msg, fields...) }
func Panic(msg string, fields ...Field) { std.Panic(msg, fields...) }
func Fatal(msg string, fields ...Field) { std.Fatal(msg, fields...) }
2023-08-12 20:16:29 +08:00
func Debugf(format string, args ...any) { std.Debug(fmt.Sprintf(format, args...)) }
func Infof(format string, args ...any) { std.Info(fmt.Sprintf(format, args...)) }
func Warnf(format string, args ...any) { std.Warn(fmt.Sprintf(format, args...)) }
func Errorf(format string, args ...any) { std.Error(fmt.Sprintf(format, args...)) }
func Panicf(format string, args ...any) { std.Panic(fmt.Sprintf(format, args...)) }
func Fatalf(format string, args ...any) { std.Fatal(fmt.Sprintf(format, args...)) }
2023-08-12 20:03:19 +08:00
func Sync() error { return std.Sync() }