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 ¶
- Variables
- func Fatalf(f string, v ...interface{})
- func New(fields ...interface{}) line
- func Printf(f string, v ...interface{})
- func RegexPassthrough(f string, v ...interface{}) bool
- func SetLogConfig(config LogConfig) error
- func SetOutput(w io.Writer) (old io.Writer)
- func Trap()
- type LevelInt
- type Line
- type LogConfig
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 Config = LogConfig{ MinLevel: LevelDebug, RegexPassthrough: []string{}, RegexEnabled: false, DebugOn: false, UpdatedAt: time.Time{}, } )
View Source
var ( // Info, Warn, and so forth are commonly encountered log "levels". Info = line{Level: "info", LevelInt: LevelInfo} Warn = line{Level: "warn", LevelInt: LevelWarn} Error = line{Level: "error", LevelInt: LevelError} Fatal = line{Level: "fatal", LevelInt: LevelFatal} // Debug is a special level, it is only printed if Config.DebugOn is true Debug = line{Level: "debug", LevelInt: LevelDebug} )
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"}
func RegexPassthrough ¶ added in v0.1.5
func SetLogConfig ¶ added in v0.1.5
Types ¶
Click to show internal directories.
Click to hide internal directories.