Documentation ¶
Overview ¶
Light-weight json logging
Index ¶
- Constants
- Variables
- func SetLogLevel(level LogLevel)
- type Entry
- type LogLevel
- type Logger
- func (l *Logger) Debugf(format string, args ...interface{})
- func (l *Logger) Errorf(format string, args ...interface{})
- func (l *Logger) Fatalf(format string, args ...interface{})
- func (l *Logger) Infof(format string, args ...interface{})
- func (l *Logger) SetLogLevel(level LogLevel)
- func (l *Logger) Warnf(format string, args ...interface{})
- func (l *Logger) WithField(key string, value interface{}) *Logger
Examples ¶
Constants ¶
const ( // FieldKeyLevel is the log level log field name FieldKeyLevel = "@level" // FieldKeyMsg is the log message log field name FieldKeyMsg = "@message" // FieldKeyTime is the time log field name FieldKeyTime = "@timestamp" // FieldKeyCommit is the commit log field name FieldKeyCommit = "@commit" )
Variables ¶
var Now = func() time.Time { return time.Now().UTC() }
Functions ¶
Types ¶
type Entry ¶
type Entry map[string]interface{}
func (Entry) MarshalEasyJSON ¶
MarshalEasyJSON supports easyjson.Marshaler interface
func (*Entry) UnmarshalEasyJSON ¶
UnmarshalEasyJSON supports easyjson.Unmarshaler interface
type LogLevel ¶
type LogLevel int
LogLevel represents a log level used by Logger type
const ( // UnknownLevel means the log level could not be parsed UnknownLevel LogLevel = iota // DebugLevel is the most verbose output and logs messages on all levels DebugLevel // InfoLevel logs messages on all levels except the DebugLevel InfoLevel // WarningLevel logs messages on all levels except DebugLevel and InfoLevel WarningLevel // ErrorLevel logs messages on ErrorLevel and FatalLevel ErrorLevel // FatalLevel logs messages on FatalLevel FatalLevel )
func (LogLevel) MarshalEasyJSON ¶
type Logger ¶
type Logger struct { FieldKeyMsg string FieldKeyLevel string FieldKeyTime string // contains filtered or unexported fields }
Logger logs json formatted messages to a certain output destination
func DefaultLogger ¶
func DefaultLogger() *Logger
DefaultLogger returns a new default logger logging to stdout
func NewLogger ¶
NewLogger creates a new logger which will write to the passed in io.Writer
Example ¶
package main import ( "os" "github.com/dcmn-com/jlo" ) func main() { l := jlo.NewLogger(os.Stdout) l.FieldKeyLevel = "lvl" l.FieldKeyMsg = "msg" l.FieldKeyTime = "time" l.Infof("I'm real") }
Output: {"lvl":"info","msg":"I'm real","time":"2018-08-02T21:48:56.856339554Z"}
Example (CustomFields) ¶
package main import ( "os" "github.com/dcmn-com/jlo" ) func main() { l := jlo.NewLogger(os.Stdout) l.Infof("I'm real") }
Output: {"@level":"info","@message":"I'm real","@timestamp":"2018-08-02T21:48:56.856339554Z"}
func (*Logger) Debugf ¶
Debugf logs a messages on DebugLevel
Example ¶
package main import ( "os" "github.com/dcmn-com/jlo" ) func main() { l := jlo.NewLogger(os.Stdout) l.SetLogLevel(jlo.DebugLevel) l.Debugf("I'm real") }
Output: {"@level":"debug","@message":"I'm real","@timestamp":"2018-08-02T21:48:56.856339554Z"}
func (*Logger) Errorf ¶
Errorf logs a messages on ErrorLevel
Example ¶
package main import ( "os" "github.com/dcmn-com/jlo" ) func main() { l := jlo.NewLogger(os.Stdout) l.Errorf("I'm real") }
Output: {"@level":"error","@message":"I'm real","@timestamp":"2018-08-02T21:48:56.856339554Z"}
func (*Logger) Fatalf ¶
Fatalf logs a message on FatalLevel
Example ¶
package main import ( "os" "github.com/dcmn-com/jlo" ) func main() { l := jlo.NewLogger(os.Stdout) l.Fatalf("I'm real") }
Output: {"@level":"fatal","@message":"I'm real","@timestamp":"2018-08-02T21:48:56.856339554Z"}
func (*Logger) Infof ¶
Infof logs a messages on InfoLevel
Example ¶
package main import ( "os" "github.com/dcmn-com/jlo" ) func main() { l := jlo.NewLogger(os.Stdout) l.Infof("I'm real") }
Output: {"@level":"info","@message":"I'm real","@timestamp":"2018-08-02T21:48:56.856339554Z"}
func (*Logger) SetLogLevel ¶
SetLogLevel changes the log level
Example ¶
package main import ( "os" "github.com/dcmn-com/jlo" ) func main() { l := jlo.NewLogger(os.Stdout) l.SetLogLevel(jlo.DebugLevel) l.Debugf("I'm real") }
Output: {"@level":"debug","@message":"I'm real","@timestamp":"2018-08-02T21:48:56.856339554Z"}
func (*Logger) Warnf ¶
Warnf logs a messages on WarningLevel
Example ¶
package main import ( "os" "github.com/dcmn-com/jlo" ) func main() { l := jlo.NewLogger(os.Stdout) l.Warnf("I'm real") }
Output: {"@level":"warning","@message":"I'm real","@timestamp":"2018-08-02T21:48:56.856339554Z"}
func (*Logger) WithField ¶
WithField returns a copy of the logger with a custom field set, which will be included in all subsequent logs
Example ¶
package main import ( "os" "github.com/dcmn-com/jlo" ) func main() { l := jlo.NewLogger(os.Stdout) l = l.WithField("@request_id", "aa33ee55") l.Infof("I'm real") }
Output: {"@level":"info","@message":"I'm real","@request_id":"aa33ee55","@timestamp":"2018-08-02T21:48:56.856339554Z"}