From 78d6f15c224eb7350305b62baa97c4abe41a0f66 Mon Sep 17 00:00:00 2001 From: yangjiechina <1534796060@qq.com> Date: Wed, 13 Nov 2024 19:29:55 +0800 Subject: [PATCH] =?UTF-8?q?=E9=BB=98=E8=AE=A4=E6=97=A5=E5=BF=97=E4=B8=8D?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E5=88=B0=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.json | 1 + log/log.go | 14 ++++++++------ main.go | 2 +- stream/config.go | 13 +++++++------ 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/config.json b/config.json index dbeb097..5d008ec 100644 --- a/config.json +++ b/config.json @@ -73,6 +73,7 @@ }, "log": { + "file_logging": false, "level": -1, "name": "./logs/lkm.log", "max_size": 10, diff --git a/log/log.go b/log/log.go index edc9bab..e5cf119 100644 --- a/log/log.go +++ b/log/log.go @@ -14,20 +14,22 @@ var ( Sugar *zap.SugaredLogger ) -func InitLogger(leve zapcore.LevelEnabler, +func InitLogger(toFile bool, leve zapcore.LevelEnabler, name string, maxSize, maxBackup, maxAge int, compress bool) { utils.Assert(Sugar == nil) var sinks []zapcore.Core - writeSyncer := getLogWriter(name, maxSize, maxBackup, maxAge, compress) 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)) - core := zapcore.NewTee(sinks...) logger := zap.New(core, zap.AddCaller()) diff --git a/main.go b/main.go index 999ca5d..3d3c35f 100644 --- a/main.go +++ b/main.go @@ -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() { gb28181.TransportManger = transport.NewTransportManager(uint16(stream.AppConfig.GB28181.Port[0]), uint16(stream.AppConfig.GB28181.Port[1])) diff --git a/stream/config.go b/stream/config.go index 53ec3d2..c77092f 100644 --- a/stream/config.go +++ b/stream/config.go @@ -89,12 +89,13 @@ type RecordConfig struct { } type LogConfig struct { - Level int `json:"level"` - Name string `json:"name"` - MaxSize int `json:"max_size"` //单位M - MaxBackup int `json:"max_backup"` - MaxAge int `json:"max_age"` //天数 - Compress bool `json:"compress"` + FileLogging bool `json:"file_logging"` + Level int `json:"level"` + Name string `json:"name"` + MaxSize int `json:"max_size"` //单位M + MaxBackup int `json:"max_backup"` + MaxAge int `json:"max_age"` //天数 + Compress bool `json:"compress"` } type HttpConfig struct {