logger

package
v0.0.46 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

logger.go

logger.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLoggerBasedOnEnv

func GetLoggerBasedOnEnv() *zap.Logger

GetLoggerBasedOnEnv returns a zap.Logger instance configured for either production or development based on the APP_ENV environment variable. If APP_ENV is set to "development", it returns a development logger. Otherwise, it defaults to a production logger.

func ToZapFields

func ToZapFields(keysAndValues ...interface{}) []zap.Field

ToZapFields converts a variadic list of key-value pairs into a slice of Zap fields. This allows for structured logging with strongly-typed values. The function assumes that keys are strings and values can be of any type, leveraging zap.Any for type detection.

Types

type LogLevel

type LogLevel int

LogLevel represents the level of logging. Higher values denote more severe log messages.

const (
	// LogLevelDebug is for messages that are useful during software debugging.
	LogLevelDebug LogLevel = -1 // Zap's DEBUG level
	// LogLevelInfo is for informational messages, indicating normal operation.
	LogLevelInfo LogLevel = 0 // Zap's INFO level
	// LogLevelWarn is for messages that highlight potential issues in the system.
	LogLevelWarn LogLevel = 1 // Zap's WARN level
	// LogLevelError is for messages that highlight errors in the application's execution.
	LogLevelError LogLevel = 2 // Zap's ERROR level
	// LogLevelDPanic is for severe error conditions that are actionable in development.
	LogLevelDPanic LogLevel = 3 // Zap's DPANIC level
	// LogLevelPanic is for severe error conditions that should cause the program to panic.
	LogLevelPanic LogLevel = 4 // Zap's PANIC level
	// LogLevelFatal is for errors that require immediate program termination.
	LogLevelFatal LogLevel = 5 // Zap's FATAL level
	LogLevelNone
)

func ParseLogLevelFromString added in v0.0.14

func ParseLogLevelFromString(levelStr string) LogLevel

ParseLogLevelFromString takes a string representation of the log level and returns the corresponding LogLevel. Used to convert a string log level from a configuration file to a strongly-typed LogLevel.

type Logger

type Logger interface {
	SetLevel(level LogLevel)
	Debug(msg string, fields ...zapcore.Field)
	Info(msg string, fields ...zapcore.Field)
	Warn(msg string, fields ...zapcore.Field)
	Error(msg string, fields ...zapcore.Field) error
	Panic(msg string, fields ...zapcore.Field)
	Fatal(msg string, fields ...zapcore.Field)
	With(fields ...zapcore.Field) Logger
	GetLogLevel() LogLevel
}

Logger interface with structured logging capabilities at various levels.

func BuildLogger

func BuildLogger(logLevel LogLevel, encoding string) Logger

BuildLogger creates and returns a new zap logger instance. It configures the logger with JSON formatting and a custom encoder to ensure the 'pid', 'application', and 'timestamp' fields appear at the end of each log message. The function panics if the logger cannot be initialized.

Jump to

Keyboard shortcuts

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