readme: update with example of logging to file

This commit is contained in:
Simon Eskildsen 2017-02-06 19:16:20 -05:00
parent 11fbf0fa42
commit 141e6dc6a6
3 changed files with 21 additions and 4 deletions

View File

@ -87,7 +87,8 @@ func init() {
// Log as JSON instead of the default ASCII formatter. // Log as JSON instead of the default ASCII formatter.
log.SetFormatter(&log.JSONFormatter{}) 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) log.SetOutput(os.Stdout)
// Only log the warning severity or above. // Only log the warning severity or above.
@ -138,7 +139,15 @@ var log = logrus.New()
func main() { func main() {
// The API for setting attributes is a little different than the package level // The API for setting attributes is a little different than the package level
// exported logger. See Godoc. // 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{ log.WithFields(logrus.Fields{
"animal": "walrus", "animal": "walrus",

View File

@ -2,7 +2,7 @@ package main
import ( import (
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"os" // "os"
) )
var log = logrus.New() var log = logrus.New()
@ -10,6 +10,14 @@ var log = logrus.New()
func init() { func init() {
log.Formatter = new(logrus.JSONFormatter) log.Formatter = new(logrus.JSONFormatter)
log.Formatter = new(logrus.TextFormatter) // default 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 log.Level = logrus.DebugLevel
} }

View File

@ -13,7 +13,7 @@ func IsTerminal(f io.Writer) bool {
var termios Termios var termios Termios
switch v := f.(type) { switch v := f.(type) {
case *os.File: case *os.File:
_, err := unix.IoctlGetTermios(int(f.Fd()), unix.TCGETA) _, err := unix.IoctlGetTermios(int(v.Fd()), unix.TCGETA)
return err == nil return err == nil
default: default:
return false return false