All notable changes to this project will be documented in this file.
The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.
- Memory Management for Child Loggers: New methods to prevent memory leaks in long-running applications
RemoveChild(key any) bool- Remove specific child logger by keyClearChildren()- Remove all child loggers at onceChildCount() int- Get current number of child loggersListChildKeys() []any- List all child logger keys
- Enhanced Scan Functionality: Complete implementation of the
Scan()method- Time-formatted input scanning with
bufio.Scanner - Goroutine-based processing for non-blocking operation
- Proper resource cleanup and error handling
- Time-formatted input scanning with
- Removed External Dependencies: Eliminated
github.com/kataras/piodependency- Replaced with lightweight internal printer system
- Maintained 100% API compatibility
- Reduced binary size and simplified dependency management
- Improved Performance: Streamlined internal architecture
- Direct formatting via
formatLog()method instead of complex hijacking - Atomic operations for thread-safe multi-writer support
- More efficient color and styling system
- Direct formatting via
Accept slog.Attr among with golog.Fields for extra log data.
- Add integration for
"log/slog"package:
import "log/slog"
// [...]
myLogger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
golog.Install(myLogger) // OR just golog.Install(slog.Default())- The
InstallStdmethod was removed, use theInstallmethod instead, it receivesanyinstead of justExternalLoggernow.
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.
- Add
Logger.RegisterFormatter(Formatter)to register a customFormatter. - Add
Logger.SetFormat(formatter string, opts ...any)to set the default formatter for all log levels. - Add
Logger.SetLevelFormat(levelName string, formatter string, opts ...any)to change the output format for the given "levelName".
- Add
Logger.SetLevelOutput(levelName string, w io.Writer)to customize the writer per level. - Add
Logger.GetLevelOutput(levelName string) io.Writerto get the leveled output or the default one.
Logger.Childaccepts ananyinstead ofstring. This way you can register children for pointers without forcing to naming them. If the key is string or completes thefmt.Stringerinterface, then it's used as prefix (like always did).
- Use locks on hijacker.
- New
SetStacktraceLimitmethod. If 0 (default) all debug stacktrace will be logged, if negative the field is disabled. - If
TimeFormatfield is empty thentimestampfield is disabled.
- New
Fieldstype that can be passed toLogf/Debugf/Infof/Warnf/Errorf/Fatalffunctions and set theLog.Fieldsdata field (which can be retrieved through a customLogHandler). - Add
Log.Stacktraceof newFrametype which holds the callers stack trace whenDebug/Debugf. - Add
jsonstruct fields to theLogstructure. - Update the customize-output example.
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.
- Update pio dependency to version 0.0.2 as it contains a small but important bugfix for GUI apps.
- Set the Logger's
NewLineonClonemethod whichgologmakes use inside itsChild("...")method. - Go module (v0.0.9).
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#NewLinewhich 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