golang logrus日志按小时拆分(月删除)

一 log.go

package log

import (
    rotatelogs "github.com/lestrrat-go/file-rotatelogs"
    llog "github.com/sirupsen/logrus"
    "time"
    "uwb_pos/config"
)

func init() {
    path := config.LogPath
    writer, _ := rotatelogs.New(
        //path+".%Y%m%d%H%M",
        path+".%Y%m%d%H",
        rotatelogs.WithLinkName(path),
        rotatelogs.WithMaxAge(time.Duration(config.LogMaxAge)*time.Hour),
        rotatelogs.WithRotationTime(time.Duration(config.LogRotatTm)*time.Minute),
    )
    llog.SetOutput(writer)
    if config.LogLevel == 1{
        llog.SetLevel(llog.DebugLevel)
    }else if config.LogLevel == 2{
        llog.SetLevel(llog.InfoLevel)
    }else if config.LogLevel == 3{
        llog.SetLevel(llog.WarnLevel)
    }else if config.LogLevel == 4{
        llog.SetLevel(llog.ErrorLevel)
    }
    // no set , it will be info
}

 

二 main文件用.导入

package main

import (
    ...
    _ "uwb_pos/utils/log"
  ...
) func main() { //tcp process() }

三 process中使用日志

package udpserver

import (
    llog "github.com/sirupsen/logrus"
    "time"
)

func releaseLChan(){
    ...
    llog.Info("$$$end conn read" + time.Now().In(CstSh).Format("2006-01-02 15:04:05") + "$$$")
        ...
}

 

四 配置文件

package config

const(
  ...//logs
    LogPath = "./logs/log"
    LogMaxAge = 24*60   //hour
    LogRotatTm = 60   //min
    LogLevel = 2  //1 debug 2 info 3 warn 4 error
  ...

)

五 验证结果

golang logrus日志按小时拆分(月删除)

 

golang logrus日志按小时拆分(月删除)

上一篇:备忘录模式


下一篇:面试三十三、fail-fast和fail-safe