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

代码如下:

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"`
}

    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风尘无名

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值