From 141e6dc6a6809c83c4fba8fecb687fd2776de5ed Mon Sep 17 00:00:00 2001 From: Simon Eskildsen Date: Mon, 6 Feb 2017 19:16:20 -0500 Subject: [PATCH] readme: update with example of logging to file --- README.md | 13 +++++++++++-- examples/basic/basic.go | 10 +++++++++- terminal_solaris.go | 2 +- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 206c746..3823323 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,8 @@ func init() { // Log as JSON instead of the default ASCII formatter. log.SetFormatter(&log.JSONFormatter{}) - // Output to stdout instead of the default stderr, could also be a file. + // Output to stdout instead of the default stderr + // Can be any io.Writer, see below for File example log.SetOutput(os.Stdout) // Only log the warning severity or above. @@ -138,7 +139,15 @@ var log = logrus.New() func main() { // The API for setting attributes is a little different than the package level // exported logger. See Godoc. - log.Out = os.Stderr + log.Out = os.Stdout + + // You could set this to any `io.Writer` such as a file + // file, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY, 0666) + // if err == nil { + // log.Out = file + // } else { + // log.Info("Failed to log to file, using default stderr") + // } log.WithFields(logrus.Fields{ "animal": "walrus", diff --git a/examples/basic/basic.go b/examples/basic/basic.go index ea7e551..ad703fc 100644 --- a/examples/basic/basic.go +++ b/examples/basic/basic.go @@ -2,7 +2,7 @@ package main import ( "github.com/Sirupsen/logrus" - "os" + // "os" ) var log = logrus.New() @@ -10,6 +10,14 @@ var log = logrus.New() func init() { log.Formatter = new(logrus.JSONFormatter) log.Formatter = new(logrus.TextFormatter) // default + + // file, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY, 0666) + // if err == nil { + // log.Out = file + // } else { + // log.Info("Failed to log to file, using default stderr") + // } + log.Level = logrus.DebugLevel } diff --git a/terminal_solaris.go b/terminal_solaris.go index 943394c..f3d6f96 100644 --- a/terminal_solaris.go +++ b/terminal_solaris.go @@ -13,7 +13,7 @@ func IsTerminal(f io.Writer) bool { var termios Termios switch v := f.(type) { case *os.File: - _, err := unix.IoctlGetTermios(int(f.Fd()), unix.TCGETA) + _, err := unix.IoctlGetTermios(int(v.Fd()), unix.TCGETA) return err == nil default: return false