-
Notifications
You must be signed in to change notification settings - Fork 5
Customization
Michael Kenney edited this page Aug 13, 2018
·
1 revision
The logger has several options youc an use to further customize it's output in your code:
package main
import (
"os"
log "github.com/bdlm/log"
)
func init() {
// Blacklist a string from appearing in the log. The characters in any
// substring match to a word that has been added will be replaced with
// asterisks (*).
log.AddSecret("some-secret-text")
// Register a function that will execute prior to the os.Exit call when a
// fatal error is logged.
log.RegisterExitHandler(func() {
// gracefully shutdown something...
})
// Log as JSON instead of the default ASCII formatter.
log.SetFormatter(&log.TextFormatter{
// Set various formatter options:
// DisableCaller: true,
// DisableHostname: true,
// DisableLevel: true,
// DisableMessage: true,
// DisableTimestamp: true,
// DisableTTY: true,
// EnableTrace: true,
// EscapeHTML: true,
// ForceTTY: true,
// FieldMap: FieldMap{LabelMsg: "message"},
// TimestampFormat: 2006",
})
// Output to stdout instead of the default stderr. Can be any io.Writer, see
// below for a File example.
log.SetOutput(os.Stdout)
// Only log the warning severity or above.
log.SetLevel(log.WarnLevel)
}
func main() {
log.WithFields(log.Fields{
"animal": "walrus",
"size": 10,
}).Info("A group of walrus emerges from the ocean")
log.WithFields(log.Fields{
"omg": true,
"number": 122,
}).Warn("The group's number increased tremendously!")
log.WithFields(log.Fields{
"omg": true,
"number": 100,
}).Fatal("The ice breaks!")
// A common pattern is to re-use fields between logging statements by re-using
// the log.Entry returned from WithFields()
contextLogger := log.WithFields(log.Fields{
"common": "this is a common field",
"other": "I also should be logged always",
})
contextLogger.Info("I'll be logged with common and other field")
contextLogger.Info("Me too")
}Which one will reach the other side of the river: The one who dreams of a raft, or the one that hitchhikes to the next bridge?