Skip to content

Commit

Permalink
Merge pull request sirupsen#1017 from freeformz/ffz/docWriter
Browse files Browse the repository at this point in the history
some minimal documentation for Logger.Writer{,Level}
  • Loading branch information
Edward Muller authored Oct 12, 2019
2 parents e5e927c + 6cd8d68 commit e33eea3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ import (
"runtime"
)

// Writer at INFO level. See WriterLevel for details.
func (logger *Logger) Writer() *io.PipeWriter {
return logger.WriterLevel(InfoLevel)
}

// WriterLevel returns an io.Writer that can be used to write arbitrary text to
// the logger at the given log level. Each line written to the writer will be
// printed in the usual way using formatters and hooks. The writer is part of an
// io.Pipe and it is the callers responsibility to close the writer when done.
// This can be used to override the standard library logger easily.
func (logger *Logger) WriterLevel(level Level) *io.PipeWriter {
return NewEntry(logger).WriterLevel(level)
}
Expand Down
34 changes: 34 additions & 0 deletions writer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package logrus_test

import (
"log"
"net/http"

"github.com/sirupsen/logrus"
)

func ExampleLogger_Writer_httpServer() {
logger := logrus.New()
w := logger.Writer()
defer w.Close()

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

if err := srv.ListenAndServe(); err != nil {
logger.Fatal(err)
}
}

func ExampleLogger_Writer_stdlib() {
logger := logrus.New()
logger.Formatter = &logrus.JSONFormatter{}

// Use logrus for standard log output
// Note that `log` here references stdlib's log
// Not logrus imported under the name `log`.
log.SetOutput(logger.Writer())
}

0 comments on commit e33eea3

Please sign in to comment.