Hi,
I've created this question in stack overflow.
What I need is formatting like
[INFO]:2018-02-25 10:42:20 - Starting Process
There is a simple way to do it ?
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
Yes, the documentation lacks a bit.
@IceflowRE - if it possible can you please provide example how to do it ?
or please answer here ?
https://stackoverflow.com/questions/48971780/go-change-format-of-log-output-logrus
*
Thanks!
You can make a new object that implements Formatter (https://github.com/sirupsen/logrus/blob/master/formatter.go#L7). Alternatively, you can embed the existing logrus.TextFormatter and override the Format method:
type myFormatter struct {
logrus.TextFormatter
}
func (f *myFormatter) Format(entry *Entry) ([]byte, error) {
// as an example, we prepend a shamrock to all log messages
// but you can do whatever you want here.
return append([]byte("☘ "), f.TextFormatter.Format(entry))
}
// then use this formatter as your formatter
logger := &logrus.Logger{
Out: os.Stderr,
Level: level,
Formatter: &myFormatter{logrus.TextFormatter{ /* ... use your config here ..*/ }},
}
@stevvooe ,HI
Looks promising,
unresolved type Entryreturn append([]byte("[lvl]"), f.TextFormatter.Format(entry))Can you please assist ? Thanks!
@stevvooe - Can you assist please ?
As you are implementing this formatter outside the logrus package itself, you need to prefix those type by the package name: logrus.Entry and logrus.TextFormatter
For Go n00bs (like me) who come across this in the future:
@stevvooe gave a good jumping-off point, but since he was writing this off-the-cuff, the details are a little off (such as management of the error, and the use of ... in the call to append). The code I'm currently (provisionally) working with is as follows:
type myFormatter struct {
logrus.TextFormatter
}
func (f *myFormatter) Format(entry *logrus.Entry) ([]byte, error) {
l, e := f.TextFormatter.Format(entry)
return append([]byte("prefix: "), l...), e
}
(Here, I always prepend prefix: regardless of whether an error was encountered. I'm not sure how necessary that is.)
I'm going to close this because the accepted answer on stackoverflow is pretty complete and accurate.
Would be nice if you had an example of how to create a simple custom Formatter in the package documentation itself.
Would safe people some googling and also provide them with a authoritative example.
Most helpful comment
You can make a new object that implements
Formatter(https://github.com/sirupsen/logrus/blob/master/formatter.go#L7). Alternatively, you can embed the existinglogrus.TextFormatterand override theFormatmethod: