Documentation ¶
Overview ¶
Package ltsv provides the LTSV encoder for the zap logging library. See http://ltsv.org/ for LTSV (Labeled Tab-separated Values), https://github.com/uber-go/zap for the zap logging library.
Keys and values are escaped in the same way as JSON strings' content (without enclosing double qoutes).
The LTSV encoder panics if a key contains colon ':'. Values can contain colon characters.
Nested values (like structs, objects, arrays) are encoded in JSON format. See Example (Nested) or Example (Reflected).
Example ¶
package main import ( "time" ltsv "github.com/hnakamur/zap-ltsv" "go.uber.org/zap" ) func main() { err := ltsv.RegisterLTSVEncoder() if err != nil { panic(err) } logger, err := ltsv.NewDevelopmentConfig().Build() if err != nil { panic(err) } logger.Error( "use strongly-typed wrappers to add structured context.", zap.String("library", "zap"), zap.Duration("latency", time.Nanosecond), )
Output:
Example (Nested) ¶
package main import ( "time" ltsv "github.com/hnakamur/zap-ltsv" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) type user struct { Name string `json:"name"` Email string `json:"email"` CreatedAt time.Time `json:"created_at"` } func (u user) MarshalLogObject(enc zapcore.ObjectEncoder) error { enc.AddString("name", u.Name) enc.AddString("email", u.Email) enc.AddInt64("created_at", u.CreatedAt.UnixNano()) return nil } var jane = user{ Name: "Jane Doe", Email: "jane@test.com", CreatedAt: time.Date(1980, 1, 1, 12, 0, 0, 0, time.UTC), } type users []user func (u users) MarshalLogArray(enc zapcore.ArrayEncoder) error { for _, user := range u { err := enc.AppendObject(user) if err != nil { return err } } return nil } func main() { // type user struct { // Name string `json:"name"` // Email string `json:"email"` // CreatedAt time.Time `json:"created_at"` // } // // func (u user) MarshalLogObject(enc zapcore.ObjectEncoder) error { // enc.AddString("name", u.Name) // enc.AddString("email", u.Email) // enc.AddInt64("created_at", u.CreatedAt.UnixNano()) // return nil // } // // var jane = user{ // Name: "Jane Doe", // Email: "jane@test.com", // CreatedAt: time.Date(1980, 1, 1, 12, 0, 0, 0, time.UTC), // } // // type users []user // // func (u users) MarshalLogArray(enc zapcore.ArrayEncoder) error { // for _, user := range u { // err := enc.AppendObject(user) // if err != nil { // return err // } // } // return nil // } // // err := ltsv.RegisterLTSVEncoder() // if err != nil { // panic(err) // } logger, err := ltsv.NewDevelopmentConfig().Build() if err != nil { panic(err) } logger.Info( "test array", zap.String("pacakge", "github.com/hnakamur/zap-ltsv"), zap.String("backslash", `a b`), zap.Array("users", users{jane}), )
Output:
Example (Reflect) ¶
package main import ( "time" ltsv "github.com/hnakamur/zap-ltsv" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) type user struct { Name string `json:"name"` Email string `json:"email"` CreatedAt time.Time `json:"created_at"` } func (u user) MarshalLogObject(enc zapcore.ObjectEncoder) error { enc.AddString("name", u.Name) enc.AddString("email", u.Email) enc.AddInt64("created_at", u.CreatedAt.UnixNano()) return nil } var jane = user{ Name: "Jane Doe", Email: "jane@test.com", CreatedAt: time.Date(1980, 1, 1, 12, 0, 0, 0, time.UTC), } type users []user func (u users) MarshalLogArray(enc zapcore.ArrayEncoder) error { for _, user := range u { err := enc.AppendObject(user) if err != nil { return err } } return nil } func main() { // type user struct { // Name string `json:"name"` // Email string `json:"email"` // CreatedAt time.Time `json:"created_at"` // } // // var jane = user{ // Name: "Jane Doe", // Email: "jane@test.com", // CreatedAt: time.Date(1980, 1, 1, 12, 0, 0, 0, time.UTC), // } // // type users []user // // err := ltsv.RegisterLTSVEncoder() // if err != nil { // panic(err) // } logger, err := ltsv.NewDevelopmentConfig().Build() if err != nil { panic(err) } logger.Info( "test array", zap.String("pacakge", "github.com/hnakamur/zap-ltsv"), zap.String("backslash", `a b`), zap.Array("users", users{jane}), )
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewDevelopmentConfig ¶
NewDevelopmentConfig is a reasonable development configuration. Logging is enabled at DebugLevel and above.
It enables development mode (which makes DPanicLevel logs panic), uses a ltsv encoder, writes to standard error, and disables sampling. Stacktraces are automatically included on logs of WarnLevel and above.
func NewDevelopmentEncoderConfig ¶
func NewDevelopmentEncoderConfig() zapcore.EncoderConfig
NewDevelopmentEncoderConfig returns an opinionated EncoderConfig for development environments.
func NewLTSVEncoder ¶
func NewLTSVEncoder(cfg zapcore.EncoderConfig) zapcore.Encoder
NewLTSVEncoder creates a line-oriented LTSV encoder.
func NewProductionConfig ¶
NewProductionConfig is the recommended production configuration. Logging is enabled at InfoLevel and above.
It uses a JSON encoder, writes to standard error, and enables sampling. Stacktraces are automatically included on logs of ErrorLevel and above.
func NewProductionEncoderConfig ¶
func NewProductionEncoderConfig() zapcore.EncoderConfig
NewProductionEncoderConfig returns an opinionated EncoderConfig for production environments.
func RegisterLTSVEncoder ¶
func RegisterLTSVEncoder() error
RegisterLTSVEncoder registers the LTSV encoder.
Types ¶
This section is empty.