logger/logger.go

160 lines
4.0 KiB
Go
Raw Normal View History

2022-05-14 13:04:01 +08:00
// Package logger...
//
// Description : logger 日志文件
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-01-02 5:04 下午
package logger
import (
"io"
"os"
2024-07-22 22:29:45 +08:00
"github.com/pkg/errors"
2022-05-14 13:04:01 +08:00
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
)
// NewLogger 获取日志实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 5:05 下午 2021/1/2
2024-06-24 17:54:31 +08:00
func NewLogger(inputLoggerLevel string, splitConfig *RotateLogConfig, optionFunc ...SetLoggerOptionFunc) (*zap.Logger, error) {
2022-05-14 13:04:01 +08:00
if nil == splitConfig {
return nil, errors.New("未配置日志切割规则")
}
2024-06-24 18:08:33 +08:00
loggerLevel := inputLevel2ZapLevel(inputLoggerLevel)
2022-05-14 13:04:01 +08:00
o := &OptionLogger{}
for _, f := range optionFunc {
f(o)
}
if nil == o.Encoder {
o.Encoder = GetEncoder()
}
loggerLevelDeal := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= loggerLevel
})
l := &Logger{
splitConfig: splitConfig,
encoder: o.Encoder,
}
var (
err error
loggerWriter io.Writer
)
// 获取 日志实现
if loggerWriter, err = l.getWriter(); nil != err {
return nil, err
}
fileHandlerList := []zapcore.Core{
zapcore.NewCore(o.Encoder, zapcore.AddSync(loggerWriter), loggerLevelDeal),
2024-07-23 15:38:23 +08:00
zapcore.NewCore(o.Encoder, zapcore.AddSync(&wsWriter{}), loggerLevelDeal), // 设置ws日志输出
2022-05-14 13:04:01 +08:00
}
// 设置控制台输出
if o.ConsoleOutput {
fileHandlerList = append(fileHandlerList, zapcore.NewCore(o.Encoder, zapcore.AddSync(os.Stdout), loggerLevelDeal))
}
// 最后创建具体的Logger
core := zapcore.NewTee(fileHandlerList...)
// 需要传入 zap.AddCaller() 才会显示打日志点的文件名和行数, 跳过一行可以直接显示业务代码行号,否则显示日志包行号
logConfList := make([]zap.Option, 0)
if o.WithCaller {
logConfList = append(logConfList, zap.AddCaller(), zap.AddCallerSkip(o.WithCallerSkip))
}
log := zap.New(core, logConfList...)
return log, nil
}
// NewConsoleLogger 获取控制台输出的日志实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:22 下午 2021/4/17
func NewConsoleLogger(loggerLevel zapcore.Level, optionFunc ...SetLoggerOptionFunc) (*zap.Logger, error) {
o := &OptionLogger{}
for _, f := range optionFunc {
f(o)
}
if nil == o.Encoder {
o.Encoder = GetEncoder()
}
loggerLevelDeal := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= loggerLevel
})
fileHandlerList := []zapcore.Core{
zapcore.NewCore(o.Encoder, zapcore.AddSync(os.Stdout), loggerLevelDeal),
}
// 最后创建具体的Logger
core := zapcore.NewTee(fileHandlerList...)
// 需要传入 zap.AddCaller() 才会显示打日志点的文件名和行数, 跳过一行可以直接显示业务代码行号,否则显示日志包行号
logConfList := make([]zap.Option, 0)
if o.WithCaller {
logConfList = append(logConfList, zap.AddCaller(), zap.AddCallerSkip(o.WithCallerSkip))
}
log := zap.New(core, logConfList...)
return log, nil
}
type Logger struct {
splitConfig *RotateLogConfig
encoder zapcore.Encoder
}
// getWriter 获取日志实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 5:08 下午 2021/1/2
func (l *Logger) getWriter() (io.Writer, error) {
option := make([]rotatelogs.Option, 0)
option = append(option, rotatelogs.WithRotationTime(l.splitConfig.TimeInterval))
if l.splitConfig.MaxAge > 0 {
option = append(option, rotatelogs.WithMaxAge(l.splitConfig.MaxAge))
}
var (
hook *rotatelogs.RotateLogs
err error
)
if hook, err = rotatelogs.New(l.splitConfig.FullLogFormat, option...); nil != err {
return nil, CreateIOWriteError(err)
}
return hook, nil
}
2024-07-22 22:29:45 +08:00
type wsWriter struct {
}
// Write ws的writer
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 22:24 2024/7/22
func (w *wsWriter) Write(p []byte) (n int, err error) {
2024-07-23 15:38:23 +08:00
if nil == wsLoggerConnect {
2024-07-22 22:29:45 +08:00
return 0, nil
}
p = []byte(" " + string(p))
2024-07-23 15:38:23 +08:00
allConnList := wsLoggerConnect.GetCtxList()
2024-07-22 22:29:45 +08:00
for _, conn := range allConnList {
_ = conn.Session.Write(p)
}
return 0, nil
}