"log/slog" package:import "log/slog"
// [...]
myLogger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
golog.Install(myLogger) // OR just golog.Install(slog.Default())
InstallStd method was removed, use the Install method instead, it receives any instead of just ExternalLogger now.Add golog.Now variable so 3rd-parties can customize the time.Now functionality used in golog.Log.Time and Timestamp.
Fix Clone not inherite the parent's formatters field (fixes SetLevelFormat on childs).
Introduce the Formatter interface. Example.
Logger.RegisterFormatter(Formatter) to register a custom Formatter.Logger.SetFormat(formatter string, opts ...interface{}) to set the default formatter for all log levels.Logger.SetLevelFormat(levelName string, formatter string, opts ...interface{}) to change the output format for the given "levelName".Logger.SetLevelOutput(levelName string, w io.Writer) to customize the writer per level.Logger.GetLevelOutput(levelName string) io.Writer to get the leveled output or the default one.Logger.Child accepts an interface{} instead of string. This way you can register children for pointers without forcing to naming them. If the key is string or completes the fmt.Stringer interface, then it's used as prefix (like always did).SetStacktraceLimit method. If 0 (default) all debug stacktrace will be logged, if negative the field is disabled.TimeFormat field is empty then timestamp field is disabled.Fields type that can be passed to Logf/Debugf/Infof/Warnf/Errorf/Fatalf functions and set the Log.Fields data field (which can be retrieved through a custom LogHandler).Log.Stacktrace of new Frame type which holds the callers stack trace when Debug/Debugf.json struct fields to the Log structure.Add a Warningf method to complete the dgraph-io/badger.Logger interface and set the Prefix text right before the log's actual message instead of the beginning of the log line.
This release provides support for colorized log level per registered output. Log's level will be colorful for registered io.Writer(via AddOutput) that supports colors, even when the rest of the writers (e.g. files) don't.
Breaking changes on the Levels map. See the corresponding updated example for migration.
NewLine on Clone method which golog makes use inside its Child("...") method.Add fatal level and Fatal/Fatalf funcs.
Add a SetPrefix(string) and Child(string) *Logger.
Default package-level logger
// automatically prefixed as "Router: "
golog.Child("Router").Errorf("Route %s already exists", "/mypath")
// able to prefix the main logger or child
golog.Child("Server").SetPrefix("HTTP Server: ").Infof("Server is running at %s", ":8080")
// Child does return a new *Logger based on its parent
srvLogger := golog.Child("Server")
Same for independent instances
log := golog.New()
log.SetPrefix("App#1: ")
routerLogger := log.Child("Router")
routerLogger.Errorf("Route %s already exists", "/mypath")
Example can be found here.
Users are now able to add custom or modify existing levels with an easy to remember API.
package main
import (
"github.com/kataras/golog"
)
func main() {
// Let's add a custom level,
//
// It should be starting from level index 6,
// because we have 6 built'n levels (0 is the start index):
// disable,
// fatal,
// error,
// warn,
// info
// debug
// First we create our level to a golog.Level
// in order to be used in the Log functions.
var SuccessLevel golog.Level = 6
// Register our level, just three fields.
golog.Levels[SuccessLevel] = &golog.LevelMetadata{
Name: "success",
RawText: "[SUCC]",
// ColorfulText (Green Color[SUCC])
ColorfulText: "\x1b[32m[SUCC]\x1b[0m",
}
// create a new golog logger
myLogger := golog.New()
// set its level to the higher in order to see it
// ("success" is the name we gave to our level)
myLogger.SetLevel("success")
// and finally print a log message with our custom level
myLogger.Logf(SuccessLevel, "This is a success log message with green color")
}
Example can be found here.
Fix an issue occurred by previous chnages, which pio appends a trailing new line.
Add a new method golog#NewLine which can override the default line breaker chars "\n".
Default output is os.Stdout instead of os.Stderr now, you can change it by golog#SetOutput.
Users are now able to customize both raw and colorful leveled log messages' prefix, example below.
Example Code:
package main
import (
"github.com/kataras/golog"
)
func main() {
// First argument is the raw text for outputs
// that are not support colors,
// second argument is the full colorful text (yes it can be different if you wish to).
//
// If the second argument is empty then golog will update the colorful text to the
// default color (i.e red on ErrorText) based on the first argument.
// Default is "[ERRO]"
golog.ErrorText("|ERROR|", "")
// Default is "[WARN]"
golog.WarnText("|WARN|", "")
// Default is "[INFO]"
golog.InfoText("|INFO|", "")
// Default is "[DBUG]"
golog.DebugText("|DEBUG|", "")
// Business as usual...
golog.SetLevel("debug")
golog.Println("This is a raw message, no levels, no colors.")
golog.Info("This is an info message, with colors (if the output is terminal)")
golog.Warn("This is a warning message")
golog.Error("This is an error message")
golog.Debug("This is a debug message")
}
This feature has been implemented after @carlca 's suggestion, here.
Increase the logger's performance by reducing the use of buffers on the pio library