func Example_combinedOutputs() { conf := xlog.Config{ Output: xlog.NewOutputChannel(xlog.MultiOutput{ // Output interesting messages to console 0: xlog.FilterOutput{ Cond: func(fields map[string]interface{}) bool { val, found := fields["type"] return found && val == "interesting" }, Output: xlog.NewConsoleOutput(), }, // Also setup by-level loggers 1: xlog.LevelOutput{ // Send debug messages to console if they match type Debug: xlog.FilterOutput{ Cond: func(fields map[string]interface{}) bool { val, found := fields["type"] return found && val == "interesting" }, Output: xlog.NewConsoleOutput(), }, }, // Also send everything over syslog 2: xlog.NewSyslogOutput("", "", ""), }), } lh := xlog.NewHandler(conf) _ = lh }
// setupHandlerChain does plumbing for logging and such. func setupHandlerChain(c *xhandler.Chain) { host, _ := os.Hostname() conf := xlog.Config{ // Log info level and higher Level: xlog.LevelInfo, // Set some global env fields Fields: xlog.F{ "role": "my-shitty-service", "host": host, }, // Output everything on console Output: xlog.NewOutputChannel(xlog.NewConsoleOutput()), } // Add close notifier handler so context is cancelled when the client closes // the connection c.UseC(xhandler.CloseHandler) // Install the logger handler c.UseC(xlog.NewHandler(conf)) // Add timeout handler (HAHA) //c.UseC(xhandler.TimeoutHandler(2 * time.Second)) // Install some provided extra handler to set some request's context fields. // Thanks to those handler, all our logs will come with some pre-populated fields. c.UseC(xlog.MethodHandler("method")) c.UseC(xlog.URLHandler("url")) c.UseC(xlog.RemoteAddrHandler("ip")) c.UseC(xlog.UserAgentHandler("user_agent")) c.UseC(xlog.RefererHandler("referer")) c.UseC(xlog.RequestIDHandler("req_id", "Request-Id")) }
func ExampleSetLogger() { xlog.SetLogger(xlog.New(xlog.Config{ Level: xlog.LevelInfo, Output: xlog.NewConsoleOutput(), Fields: xlog.F{ "role": "my-service", }, })) }
func ExampleMultiOutput() { conf := xlog.Config{ Output: xlog.NewOutputChannel(xlog.MultiOutput{ // Output everything to console 0: xlog.NewConsoleOutput(), // and also to local syslog 1: xlog.NewSyslogOutput("", "", ""), }), } lh := xlog.NewHandler(conf) _ = lh }
func Example_stdlog() { // Define logger conf conf := xlog.Config{ Output: xlog.NewConsoleOutput(), } // Remove timestamp and other decorations of the std logger log.SetFlags(0) // Plug a xlog instance to Go's std logger log.SetOutput(xlog.New(conf)) }
func ExampleLevelOutput() { conf := xlog.Config{ Output: xlog.NewOutputChannel(xlog.LevelOutput{ // Send debug message to console Debug: xlog.NewConsoleOutput(), // and error messages to syslog Error: xlog.NewSyslogOutput("", "", ""), // other levels are discarded }), } lh := xlog.NewHandler(conf) _ = lh }
func (c *config) initDefaultConfig() { host, _ := os.Hostname() c.loggerConfig = xlog.Config{ Level: xlog.LevelInfo, Fields: xlog.F{ "role": "unoconv-api", "host": host, }, } if os.Getenv("LOGFMT") == "json" { c.loggerConfig.Output = xlog.NewOutputChannel(xlog.NewJSONOutput(os.Stdout)) } else { c.loggerConfig.Output = xlog.NewOutputChannel(xlog.NewConsoleOutput()) } }
func ExampleFilterOutput() { conf := xlog.Config{ Output: xlog.NewOutputChannel(xlog.FilterOutput{ // Match messages containing a field type = interesting Cond: func(fields map[string]interface{}) bool { val, found := fields["type"] return found && val == "interesting" }, // Output matching messages to the console Output: xlog.NewConsoleOutput(), }), } lh := xlog.NewHandler(conf) _ = lh }