You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
crate/log/logsdk/logger.go

155 lines
4.3 KiB
Go

2 years ago
package logsdk
import (
"os"
"sync"
"sync/atomic"
)
// New 返回初始未经过参数调整的 Logger
func New() *Logger {
logger := &Logger{}
logger.entry.logger = logger
logger.Reset()
2 years ago
return logger
}
// entry 仅被用来嵌入 Logger
type entry = Entry
// Logger 中保存了日志所需的全局配置,
// 使用 Logger 处理日志.
type Logger struct {
beforeExitFns []func() // protected by lock
levelProcessors levelProcessors // protected by lock
2 years ago
entry
lock sync.RWMutex
exitOnce sync.Once
level atomic.Int32
reportStackLevel atomic.Int32
callerSkip atomic.Int32
reportCaller atomic.Bool
reportStack atomic.Bool
2 years ago
}
// 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)
2 years ago
}
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))
}
2 years ago
// Reset 把 Logger 重置到初始状态
func (logger *Logger) Reset() {
logger.lock.Lock()
logger.beforeExitFns = nil
2 years ago
logger.levelProcessors = levelProcessors{}
logger.lock.Unlock()
logger.SetLevel(LevelInfo)
logger.SetReportStackLevel(LevelWarn)
2 years ago
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]退出程序.
2 years ago
func (logger *Logger) Exit(code int) {
logger.BeforeExit()
os.Exit(code)
2 years ago
}
// AddBeforeExit 增加 Exit 在调用 [os.Exit] 前执行的函数,
// 先增加的后执行.
func (logger *Logger) AddBeforeExit(fn ...func()) {
logger.lock.Lock()
logger.beforeExitFns = append(logger.beforeExitFns, fn...)
logger.lock.Unlock()
}
2 years ago
func (logger *Logger) getLevelProcessors(level Level) []EntryProcessor {
logger.lock.RLock()
defer logger.lock.RUnlock()
return logger.levelProcessors[level+levelOffset]
2 years ago
}
func (logger *Logger) newEntry() Entry {
return Entry{
logger: logger,
callerSkip: logger.GetCallerSkip() + entrySkipOffset,
2 years ago
reportCaller: logger.GetReportCaller(),
reportStack: logger.GetReportStack(),
initialized: true,
}
}