logger

package module
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2025 License: MIT Imports: 5 Imported by: 14

README

LOGGER

Tests Go Report Card Go Go Reference GitHub Release

Logger provides a configurable logging solution with multiple output options, log levels, and rotation capabilities built on top of Go's slog package.

Installation

go get github.com/jkaninda/logger

Usage Example

l := logger.New(
WithOutputFile("/var/log/app.log"),
WithCaller(),
WithInfoLevel(),
WithMaxAge(1),
WithMaxSize(100),
WithJSONFormat(),
)
l.Info("Application started", "version", "1.0.0")

Default config

	l := logger.Default()
	l.Info("Application started", "version", "1.0.0", "config", "default")

Using global logger instance

	logger.New(logger.WithJSONFormat())

	logger.Info("Application started", "version", "1.0.0")

Contributing

Contributions are welcome!

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to your fork
  5. Open a Pull Request

Give a Star! ⭐

⭐ If you find Okapi useful, please consider giving it a star on GitHub!

License

This project is licensed under the MIT License. See the LICENSE file for details.

Copyright (c) 2025 Jonas Kaninda

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug added in v0.0.5

func Debug(msg string, args ...any)

Debug logs a message at Debug level

func Error added in v0.0.4

func Error(msg string, args ...any)

Error logs a message at Error level

func Fatal added in v0.0.4

func Fatal(msg string, args ...any)

Fatal logs a message at error level and exits the program with status 1

func Info added in v0.0.4

func Info(msg string, args ...any)

Info logs a message at info level

func Warn added in v0.0.4

func Warn(msg string, args ...any)

Warn logs a message at Warn level

Types

type Config added in v0.0.2

type Config struct {
	Level      LogLevel // Minimum log level to output
	OutputFile string   // Path to log file (empty for stdout)
	MaxAgeDays int      // Maximum number of days to retain old log files
	MaxBackups int      // Maximum number of old log files to retain
	MaxSizeMB  int      // Maximum size in megabytes of the log file before rotation
	Compress   bool     // Whether to compress rotated log files
	JSONFormat bool     // Whether to use JSON formatting
	UseDefault bool     // Whether to use slog.Default() format instead of custom handler
	AddCaller  bool     // Whether to add caller information (file and line number)
}

Config holds all configurable parameters for the logger

type LogLevel

type LogLevel string

LogLevel represents the severity levels for log messages

const (
	LevelDebug   LogLevel = "debug"   // Debug level for detailed debugging information
	LevelInfo    LogLevel = "info"    // Info level for general operational messages
	LevelWarning LogLevel = "warning" // Warning level for potentially harmful situations
	LevelError   LogLevel = "error"   // Error level for error events
	LevelOff     LogLevel = "off"     // Off level completely disables logging
)

Supported log levels constants

type Logger

type Logger struct {
	*slog.Logger // Embedded slog.Logger for core logging functionality
	// contains filtered or unexported fields
}

Logger is the main logger struct that wraps slog.Logger with additional features

func Default

func Default() *Logger

Default returns a new logger using slog.Default() configuration Output format: "2025/05/31 09:00:08 INFO Application started version=1.0.0 config=default"

func New

func New(opts ...Option) *Logger

New creates a new Logger instance with customizable options Default configuration:

  • Level: info
  • MaxAgeDays: 7
  • MaxBackups: 3
  • MaxSizeMB: 100
  • Format: text
  • Output: stdout
  • AddCaller: false

func (*Logger) Close

func (l *Logger) Close() error

Close releases resources used by the logger (primarily file handles) Should be called when the logger is no longer needed

func (*Logger) Fatal

func (l *Logger) Fatal(msg string, args ...any)

Fatal logs a message at error level and exits the program with status 1

func (*Logger) GetConfig added in v0.0.2

func (l *Logger) GetConfig() Config

GetConfig returns a copy of the current logger configuration

func (*Logger) GetLevel added in v0.0.2

func (l *Logger) GetLevel() LogLevel

GetLevel returns the current log level setting

func (*Logger) IsDebugEnabled

func (l *Logger) IsDebugEnabled() bool

IsDebugEnabled returns true if debug level logging is enabled

func (*Logger) IsErrorEnabled added in v0.0.2

func (l *Logger) IsErrorEnabled() bool

IsErrorEnabled returns true if error level logging is enabled

func (*Logger) IsInfoEnabled added in v0.0.2

func (l *Logger) IsInfoEnabled() bool

IsInfoEnabled returns true if info level logging is enabled

func (*Logger) IsWarningEnabled added in v0.0.2

func (l *Logger) IsWarningEnabled() bool

IsWarningEnabled returns true if warning level logging is enabled

func (*Logger) WithOptions added in v0.0.2

func (l *Logger) WithOptions(opts ...Option) *Logger

WithOptions creates a new Logger instance with additional configuration options applied Returns a new Logger instance, leaving the original unchanged (immutable pattern)

type Option added in v0.0.2

type Option func(*Config)

Option defines the type for configuration functions that modify Logger settings

func WithCaller added in v0.0.3

func WithCaller() Option

WithCaller enables including caller information (file and line number) in logs

func WithCompression added in v0.0.2

func WithCompression() Option

WithCompression enables compression of rotated log files

func WithDebugLevel

func WithDebugLevel() Option

WithDebugLevel sets log level to debug

func WithDefaultFormat added in v0.0.2

func WithDefaultFormat() Option

WithDefaultFormat configures the logger to use slog.Default() format

func WithErrorLevel

func WithErrorLevel() Option

WithErrorLevel sets log level to error

func WithInfoLevel

func WithInfoLevel() Option

WithInfoLevel sets log level to info

func WithJSONFormat added in v0.0.2

func WithJSONFormat() Option

WithJSONFormat enables JSON formatting for log output

func WithLevel added in v0.0.2

func WithLevel(level LogLevel) Option

WithLevel sets the minimum log level for output

func WithLevelOff

func WithLevelOff() Option

WithLevelOff completely disables logging output

func WithMaxAge added in v0.0.2

func WithMaxAge(days int) Option

WithMaxAge sets maximum days to retain log files

func WithMaxBackups

func WithMaxBackups(count int) Option

WithMaxBackups sets maximum number of old log files to keep

func WithMaxSize

func WithMaxSize(sizeMB int) Option

WithMaxSize sets maximum log file size in megabytes before rotation

func WithOutputFile added in v0.0.2

func WithOutputFile(file string) Option

WithOutputFile configures file output with the given path

func WithWarningLevel

func WithWarningLevel() Option

WithWarningLevel sets log level to warning

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL