logger

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultLogLevel string = "warn"

	LoggerTypeFile   LoggerType = 1
	LoggerTypeStdout LoggerType = 2
	LoggerTypeOtel   LoggerType = 4

	LoggerProviderZerolog LoggerProvider = "zerolog"

	LogLevelTrace LogLevel = "trace"
	LogLevelDebug LogLevel = "debug"
	LogLevelInfo  LogLevel = "info"
	LogLevelWarn  LogLevel = "warn"
	LogLevelError LogLevel = "error"
	LogLevelFatal LogLevel = "fatal"
	LogLevelPanic LogLevel = "panic"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type LogLevel

type LogLevel string // LogLevel represents the severity level of log messages.

type Logger

type Logger interface {
	With(fields map[string]interface{}) Logger // Enhances a logger with additional fields.

	// Base logging methods for different levels without additional fields.
	Trace(message string, args ...interface{})
	Debug(message string, args ...interface{})
	Info(message string, args ...interface{})
	Warn(message string, args ...interface{})
	Error(message string, args ...interface{})
	Fatal(message string, args ...interface{})
	Panic(message string, args ...interface{})
	Log(level LogLevel, message string, args ...interface{})

	// Logging methods for different levels with the ability to include contextual fields.
	TraceWithFields(fields map[string]interface{}, message string, args ...interface{})
	DebugWithFields(fields map[string]interface{}, message string, args ...interface{})
	InfoWithFields(fields map[string]interface{}, message string, args ...interface{})
	WarnWithFields(fields map[string]interface{}, message string, args ...interface{})
	ErrorWithFields(fields map[string]interface{}, message string, args ...interface{})
	FatalWithFields(fields map[string]interface{}, message string, args ...interface{})
	PanicWithFields(fields map[string]interface{}, message string, args ...interface{})
	LogWithFields(level LogLevel, fields map[string]interface{}, message string, args ...interface{})
}

Logger interface defines a standard set of logging functions for structured logging across various levels (Trace, Debug, Info, Warn, Error, Fatal, Panic). It allows for logging with dynamic fields for enhanced log context, suitable for complex applications that require detailed and structured logging.

Methods:

  • With: Returns a new Logger instance that includes additional fields provided in the map. These fields are included in all subsequent log entries.

  • Trace, Debug, Info, Warn, Error, Fatal, Panic: These methods log messages at their respective levels. 'args' allows for additional formatting or parameters similar to fmt.Printf.

  • TraceWithFields, DebugWithFields, InfoWithFields, WarnWithFields, ErrorWithFields, FatalWithFields, PanicWithFields: These methods function similarly to their simple counterparts but allow for additional context-specific fields to be logged with each message, enhancing the granularity and usefulness of log entries.

Usage:

  • Use this interface to abstract logging mechanisms allowing for flexible logging backends and formats.
  • Typical use involves logging messages throughout an application with optional context fields that provide more information about the log event.

Example:

logger := GetLogger("application").With(map[string]interface{}{"userID": "12345"})
logger.Info("User logged in at time %v", time.Now())
logger.ErrorWithFields(map[string]interface{}{"reason": "timeout"}, "Failed to load user data")

Note:

  • The Fatal and Panic methods are expected to terminate the program or panic after logging, respectively.
  • It's important to use the appropriate logging level to convey the correct importance and impact of the log message.

func Get

func Get(name string) Logger

Get retrieves a Logger instance by name. It checks if the Logger has been initialized and stored in the instances map. If the Logger has not been initialized, it outputs a message indicating that the Logger must be initialized first.

Parameters:

  • name: The name of the Logger to retrieve.

Returns:

  • Logger: The Logger instance associated with the given name.

func Init

func Init(config *LoggerConfig) Logger

Init initializes a Logger instance and panics on configuration errors. Use InitE when the caller can return initialization errors.

func InitE

func InitE(config *LoggerConfig) (Logger, error)

InitE initializes a Logger instance using the specified configuration. It stores the logger by name and returns the existing instance on duplicate init.

type LoggerConfig

type LoggerConfig struct {
	Provider   LoggerProvider
	Name       string
	Type       LoggerType
	Level      LogLevel
	BaseDir    string
	TimeFormat string
	Writer     io.Writer
}

LoggerConfig contains configuration details used to initialize a logger. It specifies the logger provider, name, type, log level, base directory for log files, and the time format to be used in log entries.

Fields:

  • Provider: LoggerProvider indicating the logging framework to be used (e.g., Zerolog, Logrus).
  • Name: A unique name for the logger. This name is used to retrieve the logger instance.
  • Type: A uint8 value representing the type of logger (e.g., file, console).
  • Level: A string that sets the minimum level of messages that should be logged (e.g., INFO, DEBUG).
  • BaseDir: The directory where log files will be stored if the logger type supports file output.
  • TimeFormat: The format string for timestamping log entries. This should be compatible with Go's time formatting rules.
  • Writer: An io.Writer interface for custom output destinations. This is optional and can be nil.

type LoggerProvider

type LoggerProvider string // LoggerProvider represents the logging framework/library to be used.

type LoggerType

type LoggerType uint8 // LoggerType represents the type of logger (e.g., file, stdout).

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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