Documentation
¶
Overview ¶
Package log implements a simple structured JSON logger
To use, override the package-scoped variables at runtime.
This code may be copied and pasted into your microservice and modified to your liking. Put it in a package called log. A little copying is better than a little dependency.
Example ¶
package main import ( "io" "os" log "github.com/cbsinteractive/mc-log" ) func main() { log.SetOutput(os.Stdout) log.Service = "ex" log.Time = func() interface{} { return 1000 } log.Error.F("hello, error: %v", io.EOF) }
Output: {"svc":"ex", "ts":1000, "level":"error", "msg":"hello, error: EOF"}
Example (Second) ¶
package main import ( "io" "os" log "github.com/cbsinteractive/mc-log" ) func main() { log.SetOutput(os.Stdout) log.Service = "ex" log.Time = func() interface{} { return 1000 } log.Error.Add("severity", "high").Printf("hello, error: %v", io.EOF) }
Output: {"svc":"ex", "ts":1000, "level":"error", "severity":"high", "msg":"hello, error: EOF"}
Example (Third) ¶
package main import ( "io" "os" log "github.com/cbsinteractive/mc-log" ) func main() { log.SetOutput(os.Stdout) log.Service = "ex" log.Time = func() interface{} { return "2121.12.04" } log.Error.Add( "env", "prod", "burning", true, "pi", 3.14, ).Printf("error: %v", io.EOF) }
Output: {"svc":"ex", "ts":"2121.12.04", "level":"error", "env":"prod", "burning":true, "pi":3.14, "msg":"error: EOF"}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // Service name (can be set in main or elsewhere) Service = os.Getenv("SVC") // Time is your time function. Default is a second timestamp. Time = func() interface{} { return time.Now().Unix() } // Tags are global static fields to publish for this process on // all log levels and callers Tags = fields{} // Default is the level used when calling Printf and Fatalf Default = Info )
View Source
var ( // Info, Warn, and so forth are commonly encountered log "levels". Info = line{Level: "info"} Warn = line{Level: "warn"} Error = line{Level: "error"} Fatal = line{Level: "fatal"} // Debug is a special level, it is only printed if DebugOn is true Debug = line{Level: "debug"} DebugOn = false )
Functions ¶
func Printf ¶
func Printf(f string, v ...interface{})
Printf and Fatalf exist to make this package somewhat compatible with the go standard log.
Example ¶
package main import ( "os" log "github.com/cbsinteractive/mc-log" ) func main() { log.SetOutput(os.Stdout) log.Service = "ex" log.Time = func() interface{} { return 1000 } log.Printf("hello, world") }
Output: {"svc":"ex", "ts":1000, "level":"info", "msg":"hello, world"}
Types ¶
Click to show internal directories.
Click to hide internal directories.