logger

package
v2.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2026 License: MIT Imports: 9 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 Field added in v2.0.3

type Field = zap.Field

'Field' is an alias for zap.Field or zapcore.Field

func Any added in v2.0.3

func Any(key string, val any) Field

func Binary added in v2.0.4

func Binary(key string, val []byte) Field

func Bool added in v2.0.3

func Bool(key string, val bool) Field

func Boolp added in v2.0.4

func Boolp(key string, val *bool) Field

func Bools added in v2.0.3

func Bools(key string, val []bool) Field

func ByteString added in v2.0.4

func ByteString(key string, val []byte) Field

func ByteStrings added in v2.0.4

func ByteStrings(key string, val [][]byte) Field

func Complex64 added in v2.0.4

func Complex64(key string, val complex64) Field

func Complex128 added in v2.0.4

func Complex128(key string, val complex128) Field

func Dict added in v2.0.4

func Dict(key string, val ...Field) Field

func Duration added in v2.0.3

func Duration(key string, val time.Duration) Field

func Durationp added in v2.0.4

func Durationp(key string, val *time.Duration) Field

func Durations added in v2.0.4

func Durations(key string, val []time.Duration) Field

func Err added in v2.0.3

func Err(err error) Field

func Errors added in v2.0.4

func Errors(key string, errs []error) Field

func Float32 added in v2.0.4

func Float32(key string, val float32) Field

func Float32p added in v2.0.4

func Float32p(key string, val *float32) Field

func Float64 added in v2.0.3

func Float64(key string, val float64) Field

func Float64p added in v2.0.4

func Float64p(key string, val *float64) Field

func Float64s added in v2.0.3

func Float64s(key string, val []float64) Field

func Inline added in v2.0.4

func Inline(val zapcore.ObjectMarshaler) Field

func Int added in v2.0.3

func Int(key string, val int) Field

func Int8 added in v2.0.4

func Int8(key string, val int8) Field

func Int8p added in v2.0.4

func Int8p(key string, val *int8) Field

func Int16 added in v2.0.4

func Int16(key string, val int16) Field

func Int16p added in v2.0.4

func Int16p(key string, val *int16) Field

func Int32 added in v2.0.4

func Int32(key string, val int32) Field

func Int32p added in v2.0.4

func Int32p(key string, val *int32) Field

func Int64 added in v2.0.3

func Int64(key string, val int64) Field

func Int64p added in v2.0.4

func Int64p(key string, val *int64) Field

func Int64s added in v2.0.3

func Int64s(key string, val []int64) Field

func Ints added in v2.0.3

func Ints(key string, val []int) Field

func Namespace added in v2.0.3

func Namespace(key string) Field

func Object added in v2.0.4

func Object(key string, val zapcore.ObjectMarshaler) Field

func Reflect added in v2.0.3

func Reflect(key string, val any) Field

func Skip added in v2.0.3

func Skip() Field

func Stack added in v2.0.4

func Stack(key string) Field

func StackSkip added in v2.0.4

func StackSkip(key string, skip int) Field

func String added in v2.0.3

func String(key string, val string) Field

func Stringer added in v2.0.4

func Stringer(key string, val fmt.Stringer) Field

func Stringp added in v2.0.4

func Stringp(key string, val *string) Field

func Strings added in v2.0.3

func Strings(key string, val []string) Field

func Time added in v2.0.3

func Time(key string, val time.Time) Field

func Timep added in v2.0.4

func Timep(key string, val *time.Time) Field

func Times added in v2.0.4

func Times(key string, val []time.Time) Field

func Uint added in v2.0.3

func Uint(key string, val uint) Field

func Uint8 added in v2.0.4

func Uint8(key string, val uint8) Field

func Uint8p added in v2.0.4

func Uint8p(key string, val *uint8) Field

func Uint16 added in v2.0.4

func Uint16(key string, val uint16) Field

func Uint16p added in v2.0.4

func Uint16p(key string, val *uint16) Field

func Uint32 added in v2.0.4

func Uint32(key string, val uint32) Field

func Uint32p added in v2.0.4

func Uint32p(key string, val *uint32) Field

func Uint64 added in v2.0.3

func Uint64(key string, val uint64) Field

func Uint64p added in v2.0.4

func Uint64p(key string, val *uint64) Field

func Uintptr added in v2.0.4

func Uintptr(key string, val uintptr) Field

func Uintptrp added in v2.0.4

func Uintptrp(key string, val *uintptr) Field

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