Skip to content

io.Writer

Michael Kenney edited this page Aug 13, 2018 · 2 revisions

bdlm/log can be transformed into an io.Writer. That writer is the end of an io.Pipe and it is your responsibility to close it.

w := logger.Writer()
defer w.Close()

srv := http.Server{
    // create a stdlib log.Logger that writes to
    // log.Logger.
    ErrorLog: log.New(w, "", 0),
}

Each line written to that writer will be printed the usual way, using formatters and hooks. The level for those entries is info.

This means that we can override the standard library logger easily:

logger := log.New()
logger.Formatter = &log.JSONFormatter{}

// Use `bdlm/log` for standard log output
// Note that `log` here references stdlib's log
log.SetOutput(logger.Writer())