Charles Iliya Krempeaux 1665d92ed6 | ||
---|---|---|
LICENSE | ||
README.md | ||
calltrace.go | ||
context.go | ||
context_test.go | ||
copying_router.go | ||
copying_router_test.go | ||
default.go | ||
default_writing_router.go | ||
default_writing_router_test.go | ||
discarding_router.go | ||
discarding_router_test.go | ||
doc.go | ||
error_test.go | ||
errors.go | ||
fanout_router.go | ||
fanout_router_test.go | ||
filtering_router.go | ||
filtering_router_test.go | ||
flogger.go | ||
internal_flogger.go | ||
internal_flogger_debug.go | ||
internal_flogger_error.go | ||
internal_flogger_fatal.go | ||
internal_flogger_panic.go | ||
internal_flogger_print.go | ||
internal_flogger_test.go | ||
internal_flogger_trace.go | ||
internal_flogger_warn.go | ||
internal_flogger_with.go | ||
internal_flogger_with_test.go | ||
internal_logger.go | ||
internal_logger_alert.go | ||
internal_logger_alert_test.go | ||
internal_logger_begin.go | ||
internal_logger_debug.go | ||
internal_logger_debug_test.go | ||
internal_logger_end.go | ||
internal_logger_error.go | ||
internal_logger_error_test.go | ||
internal_logger_fatal.go | ||
internal_logger_highlight.go | ||
internal_logger_highlight_test.go | ||
internal_logger_inform.go | ||
internal_logger_inform_test.go | ||
internal_logger_mute.go | ||
internal_logger_panic.go | ||
internal_logger_prefix.go | ||
internal_logger_prefix_test.go | ||
internal_logger_trace.go | ||
internal_logger_trace_test.go | ||
internal_logger_unmute.go | ||
internal_logger_warn.go | ||
internal_logger_warn_test.go | ||
logger.go | ||
logger_test.go | ||
mapping_router.go | ||
non_blocking_router.go | ||
pretty_writing_router.go | ||
router.go |
README.md
go-log
A library that provides structured and formatted logging for the Go programming language.
(This Go package was originally named flog
, but in version 2 was renamed to log
.)
Online Documention
Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-log
Basic Usage
Basic usage of this logger looks like this:
router := log.NewPrettyWritingRouter(os.Stdout)
logger := log.New(router)
Once you have the logger, you can do things such as:
logger.Print("Hello world!")
logger.Println("Hello world!")
logger.Printf("Hello %s!", name)
logger.Panic("Uh oh!")
logger.Panicln("Uh oh!")
logger.Panicf("Uh oh, had a problem happen: %s.", problemDescription)
logger.Fatal("Something really bad happened!")
logger.Fatalln("Something really bad happened!")
logger.Fatalf("Something really bad happened: %s.", problemDescription)
BTW, if the PrettyWritingRouter was being used, then this:
logger.Print("Hello world!")
Would generate output like the following:
Hello world! (2015-10-10 17:28:49.397356044 -0700 PDT)
(Although note that in actual usage this would have color.)
(Note that for for other routers the actual output would look very different! What the output looks like is router dependent.)
Structured Logging
But those method calls all generated unstructure data.
To include structured data the logger's With method needs to be used. For example:
newLogger := logger.With(map[string]interface{}{
"method":"Toil",
"secret_note":"Hi there! How are you?",
})
Then if the PrettyWritingRouter was being used, then this:
newLogger.Print("Hello world!")
Would generate output like the following:
Hello world! (2015-10-10 17:28:49.397356044 -0700 PDT) method="Toil" secret_note="Hi there! How are you?"
(Again, note that in actual usage this would have color.)
Deployment Environment
Of course in a real application system you should (probably) create a different kind of logger for each deployment environment.
Even though the PrettyWritingRouter is great for a development deployment environment (i.e., "DEV") it is probably not appropriate for a production deployment environment (i.e., "PROD").
For example:
var logger log.Logger
switch deploymentEnvironment {
case "DEV":
router := log.NewPrettyWritingRouter(os.Stdout)
logger = log.New(router)
case "PROD":
verboseRouter = log.NewDiscardingRouter()
if isVerboseMode {
verboseRouter = NewCustomVerboseRouter()
}
panicDetectionRouter := log.NewFilteringRouter(NewCustomerPanicRecordingRouter(), filterOnlyPanicsFunc)
errorDetectionRouter := log.NewFilteringRouter(NewCustomerPanicRecordingRouter(), filterOnlyErrorsFunc)
router := NewFanoutRouter(verboseRouter, panicDetectionRouter, errorDetectionRouter)
logger = log.New(router)
}