doujiang2020 2018-02-25 08:45
浏览 168
已采纳

更改日志输出日志的格式

I use go logrus and I need to change the format of the output message, I try several of things without success

what I need is format like this with formatter of logrus

[error]: datetime - message

What I've tried without success is this

 logger := &logrus.Logger{
        Out:   os.Stderr,
        Level: level,
        Formatter: &logrus.TextFormatter{
            DisableColors: true,
            TimestampFormat : "2006-01-02 15:04:05",
            FullTimestamp:true,

        },
    }

This produce the following

time="2018-02-25 10:42:20" level=error msg="Starting Process"

And I want it to be

[INFO]:2018-02-25 10:42:20 - Starting Process

update In case zap is better to customize https://github.com/uber-go/zap it could also be a solution

  • 写回答

4条回答 默认 最新

  • doujiong9915 2018-02-25 10:04
    关注

    Standard logrus-prefixed-formater usage

    To achieve this you need to make your own TextFormater which will satisfy logrus Formatter interface. Then when you create your own formater you pass it on logrus struct initialization. Other way arround and close to what you wanna achieve is this formater https://github.com/x-cray/logrus-prefixed-formatter . Based on this formater you can create your own.

    In your case you need to use like that

    logger := &logrus.Logger{
            Out:   os.Stderr,
            Level: logrus.DebugLevel,
            Formatter: &prefixed.TextFormatter{
                DisableColors: true,
                TimestampFormat : "2006-01-02 15:04:05",
                FullTimestamp:true,
                ForceFormatting: true,
            },
        }
    

    Customized output of logrus-prefixed-formater

    Link to gist to use copy of logrus-prefixed-formatter with changed format https://gist.github.com/t-tomalak/146e4269460fc63d6938264bb5aaa1db

    I leave this option if u in the end wanna use it, as in this version you have exact format you want, coloring, and other features available in standard formatter


    Custom formatter

    Third option is to use package create by me https://github.com/t-tomalak/logrus-easy-formatter. It provide simple option to format output as you want and is it only purpose. I removed not necessary options which probably you wouldn't use.

    package main
    
    import (
        "os"
    
        "github.com/sirupsen/logrus"
        "github.com/t-tomalak/logrus-easy-formatter"
    )
    
    func main() {
        logger := &logrus.Logger{
            Out:   os.Stderr,
            Level: logrus.DebugLevel,
            Formatter: &easy.Formatter{
                TimestampFormat: "2006-01-02 15:04:05",
                LogFormat:       "[%lvl%]: %time% - %msg%",
            },
        }
    
        logger.Printf("Log message")
    }
    

    This sample code will produce:

    [INFO]: 2018-02-27 19:16:55 - Log message
    

    Also I wanna point out that if in the future wanna change formatter there shouldn't be any problems to use i.e. default Logrus TextFormatter/JSONFormatter.


    Customized output of logrus-prefixed-formater

    If you really don't wanna copy this formatter to your project you can use my fork logrus-prefixed-formater with copy/pasted this code https://github.com/t-tomalak/logrus-prefixed-formatter

    You can use it like standard option but remember to change import to my repository in you go file

    logger := &logrus.Logger{
            Out:   os.Stderr,
            Level: logrus.DebugLevel,
            Formatter: &prefixed.TextFormatter{
                DisableColors: true,
                TimestampFormat : "2006-01-02 15:04:05",
                FullTimestamp:true,
                ForceFormatting: true,
            },
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容