Add default logger instance and package-level API#21
Merged
paganotoni merged 1 commit intomainfrom Mar 21, 2026
Merged
Conversation
This change introduces a default logger that can be used without creating a logger instance, similar to the standard library's log package. New API: - logger.Default() - Returns the default FieldLogger instance - logger.SetDefault(l FieldLogger) - Sets a custom default logger - Package-level functions: logger.Debug, logger.Info, logger.Warn, logger.Error, logger.Fatal, logger.Panic, logger.WithField, logger.WithFields, and all *f variants (Debugf, Infof, etc.) The fieldLogger implementation has been moved to its own file (field.go) for better code organization. Added comprehensive tests covering: - Default logger functionality - Package-level wrapper functions - Field logging with WithField/WithFields - Level filtering behavior - The logrus subpackage - The textFormatter implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR introduces a default logger instance and package-level logging functions, making it easier to use the logger package without explicitly creating a logger instance.
New API
Default Logger
logger.Default()- Returns the defaultFieldLoggerinstance (initialized withInfoLevelon first call)logger.SetDefault(l FieldLogger)- Sets a custom default loggerPackage-Level Functions
All logging methods are now available as package-level functions that use the default logger:
Non-formatted:
logger.Debug(args ...any)logger.Info(args ...any)logger.Warn(args ...any)logger.Error(args ...any)logger.Fatal(args ...any)logger.Panic(args ...any)Formatted:
logger.Debugf(msg string, args ...any)logger.Infof(msg string, args ...any)logger.Printf(msg string, args ...any)logger.Warnf(msg string, args ...any)logger.Errorf(msg string, args ...any)logger.Fatalf(msg string, args ...any)With fields:
logger.WithField(key string, value any) FieldLoggerlogger.WithFields(fields map[string]any) FieldLoggerUsage Examples
Code Organization
fieldLoggerstruct and methods tofield.gofor better separation of concernsdefault.goTests
Added comprehensive tests covering:
Default(),SetDefault())WithField/WithFieldsTotal: 74 tests across both packages