Documentation
¶
Overview ¶
Package log provides a unified logging interface for the Lynx framework. It wraps the Kratos logging system and provides convenient methods for different log levels.
Package log provides core application functionality for the Lynx framework ¶
Package log - zerolog adapter for Kratos log.Logger
Index ¶
- Variables
- func Caller(depth int) log.Valuer
- func CleanupLoggers()
- func Debug(a ...any)
- func DebugCtx(ctx context.Context, a ...any)
- func Debugf(format string, a ...any)
- func DebugfCtx(ctx context.Context, format string, a ...any)
- func Debugw(keyvals ...any)
- func DebugwCtx(ctx context.Context, keyvals ...any)
- func EnablePerformanceMonitoring(enabled bool)
- func Error(a ...any)
- func ErrorCtx(ctx context.Context, a ...any)
- func Errorf(format string, a ...any)
- func ErrorfCtx(ctx context.Context, format string, a ...any)
- func Errorw(keyvals ...any)
- func ErrorwCtx(ctx context.Context, keyvals ...any)
- func Fatal(a ...any)
- func FatalCtx(ctx context.Context, a ...any)
- func Fatalf(format string, a ...any)
- func FatalfCtx(ctx context.Context, format string, a ...any)
- func Fatalw(keyvals ...any)
- func FatalwCtx(ctx context.Context, keyvals ...any)
- func GetLogPerformanceMetrics() map[string]LogPerformanceMetrics
- func GetProxyLogger() log.Logger
- func Info(a ...any)
- func InfoCtx(ctx context.Context, a ...any)
- func Infof(format string, a ...any)
- func InfofCtx(ctx context.Context, format string, a ...any)
- func Infow(keyvals ...any)
- func InfowCtx(ctx context.Context, keyvals ...any)
- func InitLogger(name string, host string, version string, cfg kconf.Config) error
- func NewConsoleWriter(config ConsoleWriterConfig) io.Writer
- func NewOptimizedConsoleWriter(w io.Writer) io.Writer
- func ResetLogPerformanceMetrics()
- func SetLevel(level Level)
- func Warn(a ...any)
- func WarnCtx(ctx context.Context, a ...any)
- func Warnf(format string, a ...any)
- func WarnfCtx(ctx context.Context, format string, a ...any)
- func Warnw(keyvals ...any)
- func WarnwCtx(ctx context.Context, keyvals ...any)
- type AsyncLogWriter
- type BatchWriter
- type BufferedWriter
- type ConsoleWriterConfig
- type Level
- type LogPerformanceMetrics
- type RotationInterval
- type RotationStrategy
- type TimeRotationWriter
Constants ¶
This section is empty.
Variables ¶
var ( // Logger is the primary logging interface. // Provides structured logging capabilities for the application. Logger log.Logger // LHelper is a convenience wrapper around logger. // Provides simplified logging methods with predefined fields. LHelper log.Helper )
Functions ¶
func Caller ¶
Caller returns a log.Valuer that provides the caller's source location. The depth parameter determines how many stack frames to skip.
Example output: "app/handler/user.go:42"
func CleanupLoggers ¶
func CleanupLoggers()
CleanupLoggers properly closes all writers and cleans up resources
func EnablePerformanceMonitoring ¶
func EnablePerformanceMonitoring(enabled bool)
EnablePerformanceMonitoring enables or disables performance monitoring
func GetLogPerformanceMetrics ¶
func GetLogPerformanceMetrics() map[string]LogPerformanceMetrics
GetLogPerformanceMetrics returns aggregated performance metrics
func GetProxyLogger ¶
GetProxyLogger returns a process-wide proxy logger for passing into Kratos app.
func InitLogger ¶
InitLogger initializes the application's logging system. InitLogger initializes the application's logging system with the provided configuration. It returns an error if initialization fails.
Parameters:
- name: The name of the service
- host: The host identifier
- version: The service version
- cfg: The configuration instance
Returns:
- error: An error if initialization fails, nil otherwise
func NewConsoleWriter ¶
func NewConsoleWriter(config ConsoleWriterConfig) io.Writer
NewConsoleWriter creates a console writer based on format
func NewOptimizedConsoleWriter ¶
NewOptimizedConsoleWriter returns a writer suitable for console output. It currently returns the provided writer directly; formatting is handled by zerolog.
func ResetLogPerformanceMetrics ¶
func ResetLogPerformanceMetrics()
ResetLogPerformanceMetrics resets all performance metrics
Types ¶
type AsyncLogWriter ¶
type AsyncLogWriter struct {
// contains filtered or unexported fields
}
AsyncLogWriter is a non-blocking writer that writes in background.
func NewAsyncLogWriter ¶
func NewAsyncLogWriter(w io.Writer, queueSize int, enableDynamicAdjust bool) *AsyncLogWriter
NewAsyncLogWriter creates an async writer with queue size. queueSize: initial queue size (default: 2000) enableDynamicAdjust: enable dynamic queue size adjustment based on metrics
func (*AsyncLogWriter) Close ¶
func (a *AsyncLogWriter) Close() error
Close stops the background goroutine and closes the underlying writer if closable.
func (*AsyncLogWriter) GetMetrics ¶
func (a *AsyncLogWriter) GetMetrics() LogPerformanceMetrics
GetMetrics returns a snapshot including queue utilization.
func (*AsyncLogWriter) SetQueueSize ¶
func (a *AsyncLogWriter) SetQueueSize(newSize int)
SetQueueSize manually sets the queue size (for external control)
type BatchWriter ¶
type BatchWriter struct {
// contains filtered or unexported fields
}
BatchWriter wraps an io.Writer with batch writing optimization It collects multiple writes and flushes them together to reduce system calls
func NewBatchWriter ¶
NewBatchWriter creates a new batch writer
func (*BatchWriter) Flush ¶
func (bw *BatchWriter) Flush() error
Flush forces a flush of the current batch
func (*BatchWriter) GetMetrics ¶
func (bw *BatchWriter) GetMetrics() (writes, batches, flushes, errors int64)
GetMetrics returns batch writer metrics
type BufferedWriter ¶
type BufferedWriter struct {
// contains filtered or unexported fields
}
BufferedWriter wraps an io.Writer with buffering and simple metrics.
func NewBufferedWriter ¶
func NewBufferedWriter(w io.Writer, size int) *BufferedWriter
NewBufferedWriter creates a buffered writer with the given buffer size.
func (*BufferedWriter) Close ¶
func (b *BufferedWriter) Close() error
Close flushes and marks the writer closed.
func (*BufferedWriter) Flush ¶
func (b *BufferedWriter) Flush() error
Flush forces buffered data to be written.
func (*BufferedWriter) GetMetrics ¶
func (b *BufferedWriter) GetMetrics() LogPerformanceMetrics
GetMetrics returns a snapshot of metrics.
func (*BufferedWriter) ResetMetrics ¶
func (b *BufferedWriter) ResetMetrics()
ResetMetrics resets counters.
type ConsoleWriterConfig ¶
type ConsoleWriterConfig struct {
Format string // "json", "text", "pretty"
ColorOutput bool // Enable color output
NoColor bool // Disable color output
TimeFormat string // Time format string
}
ConsoleWriterConfig configures console output format
type Level ¶
type Level int32
Level represents the logging level.
const ( // DebugLevel logs are typically voluminous, and are usually disabled in production. DebugLevel Level = iota // InfoLevel is the default logging priority. InfoLevel // WarnLevel logs are more important than Info, but don't need individual human review. WarnLevel // ErrorLevel logs are high-priority. If an application is running smoothly, // it shouldn't generate any error-level logs. ErrorLevel // FatalLevel logs are particularly important errors. In development the logger panics. FatalLevel )
type LogPerformanceMetrics ¶
type LogPerformanceMetrics struct {
TotalLogs int64
DroppedLogs int64
AvgWriteTime time.Duration
BufferUtilization float64
FlushCount int64
ErrorCount int64
// contains filtered or unexported fields
}
LogPerformanceMetrics holds lightweight metrics for logging performance.
type RotationInterval ¶
type RotationInterval string
RotationInterval defines the time-based rotation interval
const ( RotationIntervalHourly RotationInterval = "hourly" RotationIntervalDaily RotationInterval = "daily" RotationIntervalWeekly RotationInterval = "weekly" )
type RotationStrategy ¶
type RotationStrategy string
RotationStrategy defines the rotation strategy
const ( RotationStrategySize RotationStrategy = "size" RotationStrategyTime RotationStrategy = "time" RotationStrategyBoth RotationStrategy = "both" )
type TimeRotationWriter ¶
type TimeRotationWriter struct {
// contains filtered or unexported fields
}
TimeRotationWriter wraps lumberjack with time-based rotation support
func NewTimeRotationWriter ¶
func NewTimeRotationWriter(filename string, maxSizeMB, maxBackups, maxAgeDays int, compress bool, strategy RotationStrategy, interval RotationInterval, maxTotalSizeMB int) *TimeRotationWriter
NewTimeRotationWriter creates a new time-based rotation writer
func (*TimeRotationWriter) Close ¶
func (trw *TimeRotationWriter) Close() error
Close implements io.Closer