默认日志不保存到文件

This commit is contained in:
yangjiechina
2024-11-13 19:29:55 +08:00
parent 76cf65d649
commit 78d6f15c22
4 changed files with 17 additions and 13 deletions

View File

@@ -73,6 +73,7 @@
}, },
"log": { "log": {
"file_logging": false,
"level": -1, "level": -1,
"name": "./logs/lkm.log", "name": "./logs/lkm.log",
"max_size": 10, "max_size": 10,

View File

@@ -14,20 +14,22 @@ var (
Sugar *zap.SugaredLogger Sugar *zap.SugaredLogger
) )
func InitLogger(leve zapcore.LevelEnabler, func InitLogger(toFile bool, leve zapcore.LevelEnabler,
name string, maxSize, maxBackup, maxAge int, compress bool) { name string, maxSize, maxBackup, maxAge int, compress bool) {
utils.Assert(Sugar == nil) utils.Assert(Sugar == nil)
var sinks []zapcore.Core var sinks []zapcore.Core
writeSyncer := getLogWriter(name, maxSize, maxBackup, maxAge, compress)
encoder := getEncoder() encoder := getEncoder()
fileCore := zapcore.NewCore(encoder, writeSyncer, leve) // 保存到文件
if toFile {
writeSyncer := getLogWriter(name, maxSize, maxBackup, maxAge, compress)
fileCore := zapcore.NewCore(encoder, writeSyncer, leve)
sinks = append(sinks, fileCore)
}
sinks = append(sinks, fileCore) // 打印到控制台
//打印到控制台
sinks = append(sinks, zapcore.NewCore(encoder, zapcore.AddSync(os.Stdout), leve)) sinks = append(sinks, zapcore.NewCore(encoder, zapcore.AddSync(os.Stdout), leve))
core := zapcore.NewTee(sinks...) core := zapcore.NewTee(sinks...)
logger := zap.New(core, zap.AddCaller()) logger := zap.New(core, zap.AddCaller())

View File

@@ -150,7 +150,7 @@ func init() {
} }
// 初始化日志 // 初始化日志
log.InitLogger(zapcore.Level(stream.AppConfig.Log.Level), stream.AppConfig.Log.Name, stream.AppConfig.Log.MaxSize, stream.AppConfig.Log.MaxBackup, stream.AppConfig.Log.MaxAge, stream.AppConfig.Log.Compress) log.InitLogger(config.Log.FileLogging, zapcore.Level(stream.AppConfig.Log.Level), stream.AppConfig.Log.Name, stream.AppConfig.Log.MaxSize, stream.AppConfig.Log.MaxBackup, stream.AppConfig.Log.MaxAge, stream.AppConfig.Log.Compress)
if stream.AppConfig.GB28181.Enable && stream.AppConfig.GB28181.IsMultiPort() { if stream.AppConfig.GB28181.Enable && stream.AppConfig.GB28181.IsMultiPort() {
gb28181.TransportManger = transport.NewTransportManager(uint16(stream.AppConfig.GB28181.Port[0]), uint16(stream.AppConfig.GB28181.Port[1])) gb28181.TransportManger = transport.NewTransportManager(uint16(stream.AppConfig.GB28181.Port[0]), uint16(stream.AppConfig.GB28181.Port[1]))

View File

@@ -89,12 +89,13 @@ type RecordConfig struct {
} }
type LogConfig struct { type LogConfig struct {
Level int `json:"level"` FileLogging bool `json:"file_logging"`
Name string `json:"name"` Level int `json:"level"`
MaxSize int `json:"max_size"` //单位M Name string `json:"name"`
MaxBackup int `json:"max_backup"` MaxSize int `json:"max_size"` //单位M
MaxAge int `json:"max_age"` //天数 MaxBackup int `json:"max_backup"`
Compress bool `json:"compress"` MaxAge int `json:"max_age"` //天数
Compress bool `json:"compress"`
} }
type HttpConfig struct { type HttpConfig struct {