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 - 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 Debug ¶
func Debug(a ...any)
Debug logs at debug level, falling back to stderr if the logger is uninitialized.
func DebugCtx ¶
DebugCtx logs at debug level with context. Unlike Debug it has no stderr fallback: the message is dropped when the logger is uninitialized.
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 builds the logging system from the "lynx.log" section of cfg using the given service name, host, and version as default fields. It is safe to call again to reinitialize; a prior logger is cleaned up first. name, host, and cfg must be non-empty/non-nil. Configuration changes are applied at runtime via Watch, falling back to 2s polling when the source lacks Watch.
func NewConsoleWriter ¶
func NewConsoleWriter(config ConsoleWriterConfig) io.Writer
NewConsoleWriter returns a writer for the given format, defaulting to raw JSON on os.Stdout for any unrecognized 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 enqueues writes onto a buffered channel drained by a background goroutine, so callers never block on I/O. Entries are dropped when the queue is full. It is safe for concurrent use.
func NewAsyncLogWriter ¶
func NewAsyncLogWriter(w io.Writer, queueSize int, enableDynamicAdjust bool) *AsyncLogWriter
NewAsyncLogWriter starts an async writer. queueSize is the initial channel capacity (defaults to 2000 when <=0); enableDynamicAdjust lets the queue grow or shrink at runtime based on utilization. If w is an io.Closer it is closed by Close.
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 requests a queue resize, clamped to [minQueueSize, maxQueueSize].
type BatchWriter ¶
type BatchWriter struct {
// contains filtered or unexported fields
}
BatchWriter buffers writes and flushes them together to reduce syscalls. It runs a background flush loop and is safe for concurrent use.
func NewBatchWriter ¶
NewBatchWriter starts a BatchWriter that flushes when the buffer reaches batchSize bytes or every flushInterval (<=0 disables periodic flushing).
func (*BatchWriter) Close ¶
func (bw *BatchWriter) Close() error
Close stops the flush loop and flushes any remaining data. It is idempotent.
func (*BatchWriter) Flush ¶
func (bw *BatchWriter) Flush() error
Flush writes out any buffered data immediately.
func (*BatchWriter) GetMetrics ¶
func (bw *BatchWriter) GetMetrics() (writes, batches, flushes, errors int64)
GetMetrics returns cumulative counts of writes, batches, flushes, and errors.
type BufferedWriter ¶
type BufferedWriter struct {
// contains filtered or unexported fields
}
BufferedWriter wraps an io.Writer with buffering and simple metrics. It is safe for concurrent use.
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 to add time-based rotation (hourly/daily/ weekly) and an optional total-size cap across all log files. It is safe for concurrent use.
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 stops the rotation loop. It is idempotent and returns nil even if the background goroutine doesn't stop within the shutdown timeout.