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

143 lines
3.9 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 {
exit func(code int) // protected by lock
levelProcessors levelProcessors // protected by lock
entry
lock sync.RWMutex
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)
}
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.exit = nil
logger.levelProcessors = levelProcessors{}
logger.lock.Unlock()
logger.SetLevel(LevelInfo)
logger.SetReportStackLevel(LevelWarn)
2 years ago
logger.SetCallerSkip(0)
logger.SetReportCaller(false)
logger.SetReportStack(false)
}
// Exit 退出程序, 执行的具体过程可以通过 SetExit 指定
func (logger *Logger) Exit(code int) {
logger.lock.RLock()
defer logger.lock.RUnlock()
if logger.exit == nil {
os.Exit(code)
}
logger.exit(code)
}
// SetExit 指定退出程序时执行的函数
func (logger *Logger) SetExit(fn func(code int)) {
logger.lock.Lock()
logger.exit = 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() + 2,
reportCaller: logger.GetReportCaller(),
reportStack: logger.GetReportStack(),
initialized: true,
}
}