This is a wrapper of Logrus and the original work is of github.com/prometheus/common/log authors. I have added two functionalities on top of the existing code.
- Log to a file/or basically to any io.Writer
- Added WithFields functionality
PS: The code which I changed refers to commit #61f87aac8082fa8c3c5655c7608d7478d46ac2ad.
This is an improvement for github.com/prometheus/common/log package where it lets you write logs to a file. Existing NewLogger function looked like
func NewLogger(w io.Writer) Logger {
l := logrus.New()
l.Out = w
return logger{entry: logrus.NewEntry(l)}
}
But this implementation writes log to console only/stdout. See issue #82, #104 raising the same thing.
I changed the function to this:
func NewLogger() Logger {
l := logrus.New()
return logger{entry: logrus.NewEntry(l)}
}
and added a new SetOutput
function for the purpose of setting output i.e., writing to a file.
func SetOutput(w io.Writer) {
origLogger.Out = w
}
I have also added functionality to call WithFields
function, so now you can do something like this:
log.WithFields(log.Fields{
"animal": "walrus",
"size": 10,
}).Info("A group of walrus emerges from the ocean")
To use this package and log to a file, add the following lines in your package:
f, err := os.OpenFile("logs.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
log.Fatal(err)
}
defer f.Close()
log.NewLogger()
log.SetOutput(f)
log.WithFields(log.Fields{
"animal": "walrus",
"size": 10,
}).Info("A group of walrus emerges from the ocean")