Skip to content

Add default logger instance and package-level API#21

Merged
paganotoni merged 1 commit intomainfrom
use-slog-as-default
Mar 21, 2026
Merged

Add default logger instance and package-level API#21
paganotoni merged 1 commit intomainfrom
use-slog-as-default

Conversation

@paganotoni
Copy link
Member

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 default FieldLogger instance (initialized with InfoLevel on first call)
  • logger.SetDefault(l FieldLogger) - Sets a custom default logger

Package-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) FieldLogger
  • logger.WithFields(fields map[string]any) FieldLogger

Usage Examples

// Simple usage - no need to create a logger
logger.Info("Server starting")
logger.WithField("port", 8080).Info("Server started")
logger.Errorf("Failed to connect: %v", err)

// Or get the default logger and use it directly
log := logger.Default()
log.Info("Using default logger")

// Set a custom default logger
customLogger := logger.New(logger.DebugLevel)
logger.SetDefault(customLogger)

Code Organization

  • Moved fieldLogger struct and methods to field.go for better separation of concerns
  • Default logger functions remain in default.go

Tests

Added comprehensive tests covering:

  • Default logger functionality (Default(), SetDefault())
  • All package-level wrapper functions
  • Field logging with WithField/WithFields
  • Level filtering behavior
  • The logrus subpackage (27 tests)
  • The textFormatter implementation (9 tests)

Total: 74 tests across both packages

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
@paganotoni paganotoni merged commit 7ee68fb into main Mar 21, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant