logger

package
v0.1.30 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2024 License: MPL-2.0 Imports: 7 Imported by: 0

Documentation

Overview

logger.go

zaplogger_structured_messaging.go

logger/zaplogger_logger.go Ref: https://betterstack.com/community/guides/logging/go/zap/#logging-errors-with-zap

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnsureLogFilePath added in v0.1.20

func EnsureLogFilePath(logPath string) (string, error)

EnsureLogFilePath checks the provided path and prepares it for use with the logger. If the path is a directory, it appends a timestamp-based filename. If the path includes a filename, it checks for the existence of the file. If no path is provided, it defaults to creating a log file in the current directory with a timestamp-based name.

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 {
	GetLogLevel() LogLevel
	SetLevel(level LogLevel)
	With(fields ...zapcore.Field) Logger
	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)

	// Updated method signatures to include the 'event' parameter
	LogRequestStart(event string, requestID string, userID string, method string, url string, headers map[string][]string)
	LogRequestEnd(event string, method string, url string, statusCode int, duration time.Duration)
	LogError(event string, method string, url string, statusCode int, serverStatusMessage string, err error, rawResponse string)
	LogAuthTokenError(event string, method string, url string, statusCode int, err error)
	LogRetryAttempt(event string, method string, url string, attempt int, reason string, waitDuration time.Duration, err error)
	LogRateLimiting(event string, method string, url string, retryAfter string, waitDuration time.Duration)
	LogResponse(event string, method string, url string, statusCode int, responseBody string, responseHeaders map[string][]string, duration time.Duration)
	LogCookies(direction string, obj interface{}, method, url string)
}

Logger interface with structured logging capabilities at various levels.

func BuildLogger

func BuildLogger(logLevel LogLevel, encoding string, logConsoleSeparator string, logExportPath 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