logrus

command
v0.0.0-...-a2a1f02 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2020 License: MIT Imports: 3 Imported by: 0

README

logrus (github.com/sirupsen/logrus) example

Logging using non-standard logrus package.

Refer to the github.com/sirupsen/logrus package for more info. This is not a standard package.

GitHub Webpage

RUN

To get,

go get -u -v github.com/sirupsen/logrus

Run,

go run logrus.go

LOG LEVELS

Logrus has seven levels in this order,

  • log.Panic("I'm bailing. Calls panic() after logging.")
  • log.Fatal("Bye. Calls os.Exit(1) after logging.")
  • log.Error("Something failed but I'm not quitting.")
  • log.Warn("You should probably take a look at this.")
  • log.Info("Something noteworthy happened!")
  • log.Debug("Useful debugging information.")
  • log.Trace("Something very low level.")

USING LOGRUS

This is a very simple package.

SET LOG LEVEL
// SET LOG LEVEL
// log.SetLevel(log.InfoLevel)
log.SetLevel(log.TraceLevel)
SET FORMAT
// SET FORMAT
log.SetFormatter(&log.TextFormatter{})
// log.SetFormatter(&log.JSONFormatter{})
SET OUTPUT (DEFAULT stderr)

Output to stdout instead of the default stderr,

// SET OUTPUT (DEFAULT stderr)
log.SetOutput(os.Stdout)
LOGGING TO FILE (APPEND) (io.Writer)
// LOGGING TO FILE (APPEND) (io.Writer)
myfile, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err == nil {
    log.SetOutput(myfile)
} else {
    log.Info("Failed to log to file, using default stderr")
}
// don't forget to close it
defer myfile.Close()
NORMAL LOGGING
// NORMAL LOGGING
//log.Panic("I'm bailing. Calls panic() after logging.")
//log.Fatal("Bye. Calls os.Exit(1) after logging.")
log.Error("Something failed but I'm not quitting.")
log.Warn("You should probably take a look at this.")
log.Info("Something noteworthy happened!")
log.Debug("Useful debugging information.")
log.Trace("Something very low level.")
NORMAL LOGGING WITH FORMATTING
// NORMAL LOGGING WITH FORMATTING
name := "jeff"
s := fmt.Sprintf("This is from %s", name)
log.Info(s)
USING FIELDS
// USING FIELDS
log.WithFields(log.Fields{
    "animal": "cat",
}).Info("A cat appears")
REUSING FIELDS
// REUSING FIELDS
jeffLogger := log.WithFields(log.Fields{
    "animal": "cat",
    "other": "I also should be logged always",
})
jeffLogger.Info("I'll be logged with common and other field")
jeffLogger.Info("Me too")

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL