Documentation
¶
Index ¶
- func OpenLogFile(dir string) (*os.File, error)
- func SetupLogger(l *Logger) fiber.Handler
- func SetupRoutesContext(c *fiber.Ctx) context.Context
- type LogBuilder
- type LogEntry
- type LogLevel
- type Logger
- func (l *Logger) CleanupOldLogs(ctx context.Context) error
- func (l *Logger) Close()
- func (l *Logger) Debug(ctx context.Context) *LogBuilder
- func (l *Logger) Error(ctx context.Context) *LogBuilder
- func (l *Logger) Info(ctx context.Context) *LogBuilder
- func (l *Logger) Middleware() fiber.Handler
- func (l *Logger) Rotate() error
- func (l *Logger) Warn(ctx context.Context) *LogBuilder
- func (l *Logger) Worker()
- func (l *Logger) WriteEntry(entry LogEntry) error
- type LoggerOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func OpenLogFile ¶
OpenLogFile opens a new log file with a timestamp of now.
func SetupLogger ¶
func SetupLogger(l *Logger) fiber.Handler
SetupLogger initializes the logger and adds it to Fiber locals.
func SetupRoutesContext ¶
SetupRoutesContext adds request ID and user ID to the context.
Types ¶
type LogBuilder ¶
type LogBuilder struct { Logger *Logger Ctx context.Context Level LogLevel Msg string Meta map[string]string Fields []interface{} }
LogBuilder builds a log entry with a fluent interface.
func (*LogBuilder) Logs ¶
func (b *LogBuilder) Logs(msg string)
LogWithLevel logs a message with the specified level and context.
func (*LogBuilder) WithFields ¶
func (b *LogBuilder) WithFields(fields ...interface{}) *LogBuilder
WithFields adds formatted fields to the message.
func (*LogBuilder) WithMeta ¶
func (b *LogBuilder) WithMeta(meta map[string]string) *LogBuilder
WithMeta adds metadata to the log entry.
type LogEntry ¶
type LogEntry struct { TimeStamp string `json:"timestamp"` Level string `json:"level"` RequestID string `json:"request_id,omitempty"` UserID string `json:"user_id,omitempty"` Message string `json:"message"` Path string `json:"path,omitempty"` Method string `json:"method,omitempty"` Status int `json:"status,omitempty"` Latency string `json:"latency,omitempty"` Error string `json:"error,omitempty"` Meta map[string]string `json:"meta,omitempty"` }
LogEntry represents a structured log entry in JSON.
type Logger ¶
type Logger struct { Mu sync.Mutex Format string TimeFormat string OutputDir string MaxSizeMB int MaxAgeDays int File *os.File FileSize int64 Log *log.Logger FiberLog fiber.Handler Queue chan LogEntry Quit chan struct{} }
Logger manages structured logging with rotation and color
func (*Logger) CleanupOldLogs ¶
CleanupOldLogs removes log files older than maxAgeDays.
func (*Logger) Debug ¶
func (l *Logger) Debug(ctx context.Context) *LogBuilder
Debug starts a debug-level log entry.
func (*Logger) Error ¶
func (l *Logger) Error(ctx context.Context) *LogBuilder
Error starts an error-level log entry.
func (*Logger) Info ¶
func (l *Logger) Info(ctx context.Context) *LogBuilder
Info starts an info-level log entry.
func (*Logger) Middleware ¶
func (l *Logger) Middleware() fiber.Handler
Middleware returns the Fiber logger middleware.
func (*Logger) Warn ¶
func (l *Logger) Warn(ctx context.Context) *LogBuilder
Warn starts a warn-level log entry.
func (*Logger) WriteEntry ¶
WriteEntry writes a structured JSON log entry with color.
type LoggerOption ¶
type LoggerOption func(*Logger)
LoggerOption defines a function to configure the logger.
func WithFormat ¶
func WithFormat(format string) LoggerOption
WithFormat sets the Fiber logger format.
func WithMaxDays ¶
func WithMaxDays(days int) LoggerOption
WithMaxDays sets the maxiMum age for the log files.
func WithMaxFileSize ¶
func WithMaxFileSize(size int) LoggerOption
WithMaxFileSize sets the maxiMum size of single Log file.
func WithOutputDir ¶
func WithOutputDir(dir string) LoggerOption
WithOutputDir sets the output directory of Log File.
func WithTimeFormat ¶
func WithTimeFormat(timeformat string) LoggerOption
WithTimeFormat sets the timestamp format.