|
|
|
package logsdk
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
)
|
|
|
|
|
|
|
|
// New 返回初始未经过参数调整的 Logger
|
|
|
|
func New() *Logger {
|
|
|
|
logger := &Logger{}
|
|
|
|
logger.entry.logger = logger
|
|
|
|
logger.Reset()
|
|
|
|
return logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// entry 仅被用来嵌入 Logger
|
|
|
|
type entry = Entry
|
|
|
|
|
|
|
|
// Logger 中保存了日志所需的全局配置,
|
|
|
|
// 使用 Logger 处理日志.
|
|
|
|
type Logger struct {
|
|
|
|
beforeExitFns []func() // protected by lock
|
|
|
|
levelProcessors levelProcessors // protected by lock
|
|
|
|
entry
|
|
|
|
lock sync.RWMutex
|
|
|
|
exitOnce sync.Once
|
|
|
|
level atomic.Int32
|
|
|
|
reportStackLevel atomic.Int32
|
|
|
|
callerSkip atomic.Int32
|
|
|
|
reportCaller atomic.Bool
|
|
|
|
reportStack atomic.Bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddProcessor 把日志处理器增加到 Logger
|
|
|
|
func (logger *Logger) AddProcessor(levels []Level, processor EntryProcessor) {
|
|
|
|
logger.lock.Lock()
|
|
|
|
for _, level := range levels {
|
|
|
|
logger.levelProcessors[level+levelOffset] = append(logger.levelProcessors[level+levelOffset], processor)
|
|
|
|
}
|
|
|
|
logger.lock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLevel 返回日志系统的等级, 严重程度低于返回等级的日志不会被处理.
|
|
|
|
func (logger *Logger) GetLevel() Level {
|
|
|
|
return Level(logger.level.Load())
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLevel 设置日志系统的等级
|
|
|
|
func (logger *Logger) SetLevel(level Level) {
|
|
|
|
logger.level.Store(int32(level))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCallerSkip 获取调用 [runtime.Callers] 时的 skip 参数,
|
|
|
|
// skip 已经被偏移到从调用 Logger 相关方法处获取调用信息.
|
|
|
|
// 0 表示从调用 Logger 相关方法处获取调用信息.
|
|
|
|
func (logger *Logger) GetCallerSkip() int {
|
|
|
|
return int(logger.callerSkip.Load())
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetCallerSkip 设置调用 [runtime.Callers] 时的 skip 参数,
|
|
|
|
// skip 已经被偏移到从调用 Logger 相关方法处获取调用信息.
|
|
|
|
// 0 表示从调用 Logger 相关方法处获取调用信息.
|
|
|
|
func (logger *Logger) SetCallerSkip(callerSkip int) {
|
|
|
|
logger.callerSkip.Store(int32(callerSkip))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetReportCaller 返回是否收集调用信息
|
|
|
|
func (logger *Logger) GetReportCaller() bool {
|
|
|
|
return logger.reportCaller.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetReportCaller 设置是否收集调用信息
|
|
|
|
func (logger *Logger) SetReportCaller(reportCaller bool) {
|
|
|
|
logger.reportCaller.Store(reportCaller)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetReportStack 返回是否收集调用栈信息
|
|
|
|
func (logger *Logger) GetReportStack() bool {
|
|
|
|
return logger.reportStack.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetReportStack 设置是否收集调用栈信息
|
|
|
|
func (logger *Logger) SetReportStack(reportStack bool) {
|
|
|
|
logger.reportStack.Store(reportStack)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetReportStackLevel 获取自动添加调用栈对应的日志等级
|
|
|
|
func (logger *Logger) GetReportStackLevel() Level {
|
|
|
|
return Level(logger.reportStackLevel.Load())
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetReportStackLevel 设置日志等级小于等于 level 时自动添加调用栈
|
|
|
|
func (logger *Logger) SetReportStackLevel(level Level) {
|
|
|
|
logger.reportStackLevel.Store(int32(level))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset 把 Logger 重置到初始状态
|
|
|
|
func (logger *Logger) Reset() {
|
|
|
|
logger.lock.Lock()
|
|
|
|
logger.beforeExitFns = nil
|
|
|
|
logger.levelProcessors = levelProcessors{}
|
|
|
|
logger.lock.Unlock()
|
|
|
|
|
|
|
|
logger.SetLevel(LevelInfo)
|
|
|
|
logger.SetReportStackLevel(LevelWarn)
|
|
|
|
logger.SetCallerSkip(0)
|
|
|
|
logger.SetReportCaller(false)
|
|
|
|
logger.SetReportStack(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BeforeExit 按照先添加后执行的顺序执行 AddBeforeExit 添加的函数,
|
|
|
|
// AddBeforeExit 只会执行1次.
|
|
|
|
func (logger *Logger) BeforeExit() {
|
|
|
|
logger.exitOnce.Do(func() {
|
|
|
|
logger.lock.RLock()
|
|
|
|
beforeExitFns := make([]func(), len(logger.beforeExitFns))
|
|
|
|
copy(beforeExitFns, logger.beforeExitFns)
|
|
|
|
logger.lock.RUnlock()
|
|
|
|
for i := len(beforeExitFns) - 1; i >= 0; i-- {
|
|
|
|
beforeExitFns[i]()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit 执行 BeforeExit 后调用 [os.Exit]退出程序.
|
|
|
|
func (logger *Logger) Exit(code int) {
|
|
|
|
logger.BeforeExit()
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddBeforeExit 增加 Exit 在调用 [os.Exit] 前执行的函数,
|
|
|
|
// 先增加的后执行.
|
|
|
|
func (logger *Logger) AddBeforeExit(fn ...func()) {
|
|
|
|
logger.lock.Lock()
|
|
|
|
logger.beforeExitFns = append(logger.beforeExitFns, fn...)
|
|
|
|
logger.lock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) getLevelProcessors(level Level) []EntryProcessor {
|
|
|
|
logger.lock.RLock()
|
|
|
|
defer logger.lock.RUnlock()
|
|
|
|
return logger.levelProcessors[level+levelOffset]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) newEntry() Entry {
|
|
|
|
return Entry{
|
|
|
|
logger: logger,
|
|
|
|
callerSkip: logger.GetCallerSkip() + entrySkipOffset,
|
|
|
|
reportCaller: logger.GetReportCaller(),
|
|
|
|
reportStack: logger.GetReportStack(),
|
|
|
|
initialized: true,
|
|
|
|
}
|
|
|
|
}
|