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,
},
}