Documentation
¶
Overview ¶
Package log provides a custom logging solution with multi-output support and log rotation for file output. ----------------------------------------------------------------------------- When Logger.{Debug, Info, Warn, Error, Fatal} is called, the log message is passed to all underlying handlers represented by Logger.handler Then multiHandler.Handle is called to pass the log message to all underlying handlers. ----------------------------------------------------------------------------- The rotation mechanism works by locking the logger, checking if it's time to rotate, and then calling the Rotate method on all rotatable handlers.
Index ¶
- type Config
- type ElasticsearchConfig
- type ElasticsearchHandler
- func (h *ElasticsearchHandler) Enabled(ctx context.Context, level slog.Level) bool
- func (h *ElasticsearchHandler) Handle(ctx context.Context, r slog.Record) error
- func (h *ElasticsearchHandler) NextRotation() time.Time
- func (h *ElasticsearchHandler) Rotate() error
- func (h *ElasticsearchHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *ElasticsearchHandler) WithGroup(name string) slog.Handler
- type Entry
- type LogfileConfig
- type Logger
- func (l *Logger) Debug(msg string, args ...any)
- func (l *Logger) Error(msg string, args ...any)
- func (l *Logger) Errors() <-chan error
- func (l *Logger) Fatal(msg string, args ...any)
- func (l *Logger) Info(msg string, args ...any)
- func (l *Logger) StopErrorLog()
- func (l *Logger) StopRotation()
- func (l *Logger) Warn(msg string, args ...any)
- func (l *Logger) WatchErrors()
- func (l *Logger) WithFields(fields map[string]interface{}) *Entry
- func (l *Logger) Writer(level slog.Level) io.Writer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { FileConfig *LogfileConfig FileLevel slog.Level StdoutEnabled bool StdoutLevel slog.Level RotateLogFile bool ElasticsearchConfig *ElasticsearchConfig RotateElasticSearchIndex bool }
Config holds the configuration for the logger
type ElasticsearchConfig ¶
type ElasticsearchConfig struct { Addresses []string Username string Password string IndexPrefix string Level slog.Level }
ElasticsearchConfig holds the configuration for Elasticsearch logging
type ElasticsearchHandler ¶
type ElasticsearchHandler struct {
// contains filtered or unexported fields
}
ElasticsearchHandler implements slog.Handler for Elasticsearch
func (*ElasticsearchHandler) Enabled ¶
Enabled checks if any of the underlying handlers are enabled for a given log level. It's used internally to determine if a log message should be processed by a given handler
func (*ElasticsearchHandler) Handle ¶
Handle is responsible for passing the log record to all underlying handlers. It's called internally when a log message needs to be written.
func (*ElasticsearchHandler) NextRotation ¶
func (h *ElasticsearchHandler) NextRotation() time.Time
NextRotation calculates the next rotation time, which is the start of the next day
func (*ElasticsearchHandler) Rotate ¶
func (h *ElasticsearchHandler) Rotate() error
Rotate implements the rotation for the Elasticsearch handler. It updates the index name to use the current date and creates the new index if it doesn't exist.
type Entry ¶
type Entry struct {
// contains filtered or unexported fields
}
Entry is a log entry with fields.
func (*Entry) Fatal ¶
Fatal logs a message at Fatal level with the fields specified in WithFields, then calls os.Exit(1).
type LogfileConfig ¶
LogfileConfig represents the configuration for the log file output
func (*LogfileConfig) Filename ¶
func (s *LogfileConfig) Filename() string
Filename returns the computed filename of the log file with the current timestamp
type Logger ¶
Logger wraps slog.Logger to provide multi-output functionality
func Default ¶
func Default() *Logger
Default returns the default Logger instance. The default logger writes to both stdout (text format) and a file named "app.log" (JSON format). Both outputs are set to log messages at Info level and above. This function uses sync.Once to ensure that the default logger is only created once.
Returns:
- *Logger: The default Logger instance
func New ¶
New creates a new Logger instance with the given configuration. It sets up handlers for stdout (text format) and file output (JSON format) if specified. If FileOutput is empty, only stdout logging will be enabled.
Parameters:
- cfg: Config struct containing logger configuration options
Returns:
- *Logger: A new Logger instance
- error: An error if there was a problem creating the logger (e.g., unable to open log file)
func (*Logger) Debug ¶
Debug logs a message at Debug level. The first argument is the message to log, and subsequent arguments are key-value pairs that will be included in the log entry.
Parameters:
- msg: The message to log
- args: Optional key-value pairs to include in the log entry
func (*Logger) Error ¶
Error logs a message at Error level. The first argument is the message to log, and subsequent arguments are key-value pairs that will be included in the log entry.
Parameters:
- msg: The message to log
- args: Optional key-value pairs to include in the log entry
func (*Logger) Fatal ¶
Fatal logs a message at Error level and then calls os.Exit(1). The first argument is the message to log, and subsequent arguments are key-value pairs that will be included in the log entry.
Parameters:
- msg: The message to log
- args: Optional key-value pairs to include in the log entry
func (*Logger) Info ¶
Info logs a message at Info level. The first argument is the message to log, and subsequent arguments are key-value pairs that will be included in the log entry.
Parameters:
- msg: The message to log
- args: Optional key-value pairs to include in the log entry
func (*Logger) StopRotation ¶
func (l *Logger) StopRotation()
StopRotation stops the rotation goroutine
func (*Logger) Warn ¶
Warn logs a message at Warn level. The first argument is the message to log, and subsequent arguments are key-value pairs that will be included in the log entry.
Parameters:
- msg: The message to log
- args: Optional key-value pairs to include in the log entry
func (*Logger) WatchErrors ¶
func (l *Logger) WatchErrors()
WatchErrors watches for errors in the logger and prints them to stderr.
func (*Logger) WithFields ¶
WithFields returns a new log entry with the given fields. The fields are key-value pairs that will be included only in the next log entry.
This method returns a log Entry, which can be used to log a message with the specified fields.
Parameters:
- fields: A map of key-value pairs to be included in the next log entry
Returns:
- Entry: A log entry that can be used to log a message with the specified fields
Example:
logger := log.Default() logger.WithFields(map[string]interface{}{ "user_id": 12345, "ip": "192.168.1.1", }).Info("User logged in")