logger

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DPanicf

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

DPanicLevel logs are particularly important errors. In development the logger panics after writing the message.

func Debug

func Debug(msg string, fields ...zap.Field)

Debug logs a message at Debug level with structured fields.

func Debugf

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

Debugf logs a message at Debug level using fmt.Sprintf-style formatting.

func Error

func Error(msg string, fields ...zap.Field)

Error logs a message at Error level with structured fields.

func Errorf

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

Errorf logs a message at Error level using fmt.Sprintf-style formatting.

func Fatal

func Fatal(msg string, fields ...zap.Field)

Fatal logs a message at Fatal level with structured fields. The application will terminate immediately.

func Fatalf

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

Fatalf logs a message at Fatal level using fmt.Sprintf-style formatting. The application will terminate immediately.

func GetLoggerWithoutCaller

func GetLoggerWithoutCaller(cfg Config) (*zap.Logger, error)

func Info

func Info(msg string, fields ...zap.Field)

Info logs a message at Info level with structured fields.

func Infof

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

Infof logs a message at Info level using fmt.Sprintf-style formatting.

func InitGlobalLogger

func InitGlobalLogger(cfg Config, callerSkip int) (*zap.Logger, error)

InitGlobalLogger initializes the global zap logger with the provided configuration and caller skip.

func IsLevelEnabled

func IsLevelEnabled(level zapcore.Level) bool

IsLevelEnabled checks if a given level is enabled for the main application logger. Ensure mainAppLogger is initialized before calling.

func New

func New(cfg Config, callerSkip int) (*zap.Logger, error)

New initializes a zap logger instance with the provided configuration and caller skip. This function is safe to call multiple times but the core logger setup happens only once. It returns the configured logger instance.

Parameters:    - cfg: Config structure.    - callerSkip: The number of stack frames to skip to find the original caller.

Returns:    - *zap.Logger: the configured zap logger instance.    - error: any error encountered during logger setup.

Note: This function does NOT automatically set this logger as the global zap logger. You must call zap.ReplaceGlobals() externally if needed.

func Panic

func Panic(msg string, fields ...zap.Field)

Panic logs a message at Panic level with structured fields. It then panics.

func Panicf

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

Panicf logs a message at Panic level using fmt.Sprintf-style formatting. It then panics.

func Sugar

func Sugar() *zap.SugaredLogger

Sugar wraps the Logger to provide a more ergonomic, but slightly slower, API. Sugaring a Logger is quite inexpensive, so it's reasonable for a single application to use both Loggers and SugaredLoggers, converting between them on the boundaries of performance-sensitive code.

func Warn

func Warn(msg string, fields ...zap.Field)

Warn logs a message at Warn level with structured fields.

func Warnf

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

Warnf logs a message at Warn level using fmt.Sprintf-style formatting.

Types

type Config

type Config struct {
	// Mode sets the environment mode for the logger.
	// Accepted values are "development" or "production".
	// Defaults to "development" if unspecified.
	Mode string

	// LogFilePath is the path to the log file used for file logging with rotation.
	//
	// If provided, logs will be written both to this file and stdout.
	// If empty, only stdout will be used.
	LogFilePath string

	// LogLevel sets the verbosity level of the logger.
	//
	// Valid values are: "debug", "info", "warn", "error", "fatal" "panic",
	// and "dpanic".
	// Defaults to "info" if not set.
	LogLevel *LogLevel
}

Config defines the configuration for the logger.

It controls the logger’s operating mode, output destinations, and log verbosity. When no file path is specified, logs are printed to stdout only.

func DefaultConfig

func DefaultConfig() Config

type LogLevel

type LogLevel int

A Level is a logging priority. Higher levels are more important.

const (
	// DebugLevel logs are typically voluminous, and are usually disabled in
	// production.
	LogLevel_Debug LogLevel = -1
	// InfoLevel logs are the default logging level.
	LogLevel_Info LogLevel = 0
	// WarnLevel logs are more important than Info, but don't need individual
	// human review.
	LogLevel_Warn LogLevel = 1
	// ErrorLevel logs are high-priority. If an application is running smoothly,
	// it shouldn't generate any error-level logs.
	LogLevel_Error LogLevel = 2
	// DPanicLevel logs are particularly important errors. In development the
	// logger panics after writing the message.
	LogLevel_DPanic LogLevel = 3
	// PanicLevel logs a message, then panics.
	LogLevel_Panic LogLevel = 4
	// FatalLevel logs a message, then calls os.Exit(1).
	LogLevel_Fatal LogLevel = 5
)

func LogLevelOptions

func LogLevelOptions() []LogLevel

func ParseLogLevelString

func ParseLogLevelString(s string) (LogLevel, error)

func (LogLevel) IsValid

func (e LogLevel) IsValid() bool

func (LogLevel) String

func (e LogLevel) String() string

func (LogLevel) ToZapLevel

func (l LogLevel) ToZapLevel() zapcore.Level

type Mode

type Mode string

Mode can be either "development" or "production"

const (
	// Development mode is used for development and testing purposes.
	Mode_Development Mode = "development"
	// Production mode is used for production environments.
	Mode_Production Mode = "production"
)

func ModeOptions

func ModeOptions() []Mode

func ParseModeString

func ParseModeString(s string) (Mode, error)

func (Mode) IsValid

func (e Mode) IsValid() bool

func (Mode) String

func (e Mode) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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