Controlling Log output coloring
By default, logs output on console should be colorized depending on the detected TTY.
Customize level title, text, color and styling at general.
Import golog
and pio
:
import (
"github.com/kataras/golog"
"github.com/kataras/pio"
// [...]
)
Get a level to customize e.g. DebugLevel
:
level := golog.Levels[golog.DebugLevel]
You have full control over his text, title and style:
// The Name of the Level
// that named (lowercased) will be used
// to convert a string level on `SetLevel`
// to the correct Level type.
Name string
// AlternativeNames are the names that can be referred to this specific log level.
// i.e Name = "warn"
// AlternativeNames = []string{"warning"}, it's an optional field,
// therefore we keep Name as a simple string and created this new field.
AlternativeNames []string
// Tha Title is the prefix of the log level.
// See `ColorCode` and `Style` too.
// Both `ColorCode` and `Style` should be respected across writers.
Title string
// ColorCode a color for the `Title`.
ColorCode int
// Style one or more rich options for the `Title`.
Style []pio.RichOption
Example Code:
level := golog.Levels[golog.DebugLevel]
level.Name = "debug" // default
level.Title = "[DBUG]" // default
level.ColorCode = pio.Yellow // default
To change the output format:
app.Logger().SetFormat("json", " ")
To register a custom Formatter:
app.Logger().RegisterFormatter(new(myFormatter))
The golog.Formatter interface looks like this:
// Formatter is responsible to print a log to the logger's writer.
type Formatter interface {
// The name of the formatter.
String() string
// Set any options and return a clone,
// generic. See `Logger.SetFormat`.
Options(opts ...interface{}) Formatter
// Writes the "log" to "dest" logger.
Format(dest io.Writer, log *Log) bool
}
To change the output and the format per level:
app.Logger().SetLevelOutput("error", os.Stderr)
app.Logger().SetLevelFormat("json")