logger

package
v0.0.0-...-17e6f81 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func OpenLogFile

func OpenLogFile(dir string) (*os.File, error)

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

func SetupRoutesContext(c *fiber.Ctx) context.Context

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 LogLevel

type LogLevel string
const (
	LevelDebug LogLevel = "DEBUG"
	LevelInfo  LogLevel = "INFO"
	LevelWarn  LogLevel = "WARN"
	LevelError LogLevel = "ERRORs"
)

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 NewLogger

func NewLogger(ctx context.Context, opts ...LoggerOption) (*Logger, error)

func (*Logger) CleanupOldLogs

func (l *Logger) CleanupOldLogs(ctx context.Context) error

CleanupOldLogs removes log files older than maxAgeDays.

func (*Logger) Close

func (l *Logger) Close()

Close shuts down the logger gracefully.

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) Rotate

func (l *Logger) Rotate() error

Rotate checks file size and create new Log file if necessary.

func (*Logger) Warn

func (l *Logger) Warn(ctx context.Context) *LogBuilder

Warn starts a warn-level log entry.

func (*Logger) Worker

func (l *Logger) Worker()

Worker processes the async logging queue.

func (*Logger) WriteEntry

func (l *Logger) WriteEntry(entry LogEntry) error

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.

Jump to

Keyboard shortcuts

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