首页 > 其他分享 >go开发中, 按日志大小滚动存储日志文件

go开发中, 按日志大小滚动存储日志文件

时间:2024-10-01 12:20:59浏览次数:13  
标签:滚动 describe Value json value go 日志 logger ConfigItem

代码如下:

package main
import (

    "github.com/sirupsen/logrus"
    "gopkg.in/natefinch/lumberjack.v2"
)

func initLogger(logSetting Utils.LogObj) *logrus.Logger {
    logger := logrus.New()

    // 使用 lumberjack 提供的 Logger 作为 logrus 的输出
    logFile := &lumberjack.Logger{
        Filename:   "log/cs.log",                // 日志文件的位置
        MaxSize:    logSetting.MaxSize.Value,    // 每个日志文件保存的最大尺寸, 单位:MB
        MaxBackups: logSetting.MaxBackups.Value, // 日志文件最多保存多少个备份(最近多少个文件)
        MaxAge:     logSetting.MaxAge.Value,     // 文件最多保存多少天
        Compress:   logSetting.Compress.Value,   // 是否压缩/归档旧文件
    }

    logger.SetReportCaller(logSetting.ReportCaller.Value)

    logger.Out = logFile

    // 设置日志级别
    logger.SetLevel(logrus.InfoLevel)

    // 设置日志格式(可选)
    formatter := &logrus.TextFormatter{
        TimestampFormat: "2006-01-02 15:04:05",
    }
    if logSetting.TimestampFormat.Value {
        logger.SetFormatter(formatter)
    }

    return logger
}

func main() {
...
//日志
    logger := initLogger(settings.Log)
....
go myFunc(logger);
}

func myFunc(logger *logrus.Logger) {
....
logger.Println("wss url exist not token !")
....
}

附:config.json 中 log 节:
    "log" : 
    {
        "maxSize" : 
        {
            "value" : 100,
            "describe" : "The maximum size of each log file saved, unit: MB"
        },
        "maxBackups" : 
        {
            "value" : 10,
            "describe" : "How many backups of log files can be saved at most (the most recent ones)"
        },
        "maxAge" : 
        {
            "value" : 28,
            "describe" : "How many days can the file be saved for"
        },
        "compress" : 
        {
            "value" : false,
            "describe" : "Whether to compress/archive old files"
        },
        "reportCaller" : 
        {
            "value" : true,
            "describe" : "Whether to display the current line of code"
        },
        "timestampFormat" : 
        {
            "value" : true,
            "describe" : "Whether to display the current time"
        }
    }

以及 结构体:
// 配置参数;
type ConfigSettings struct {
    NetWork NetWorkObj `json:"network"`
    Log     LogObj     `json:"log"`
}

type LogObj struct { // 日志参数;
    MaxSize         ConfigItem_int  `json:"maxSize"`
    MaxBackups      ConfigItem_int  `json:"maxBackups"`
    MaxAge          ConfigItem_int  `json:"maxAge"`
    Compress        ConfigItem_bool `json:"compress"`
    ReportCaller    ConfigItem_bool `json:"reportCaller"`
    TimestampFormat ConfigItem_bool `json:"timestampFormat"`
}
type ConfigItem_bool struct {
    Value    bool   `json:"value"`
    Describe string `json:"describe"`
}
type ConfigItem_int struct {
    Value    int    `json:"value"`
    Describe string `json:"describe"`
}
type ConfigItem_string struct {
    Value    string `json:"value"`
    Describe string `json:"describe"`
}

    

标签:滚动,describe,Value,json,value,go,日志,logger,ConfigItem
From: https://blog.csdn.net/zhouwuhua/article/details/142669693

相关文章