Documentation
¶
Index ¶
- Constants
- type Config
- type ConsoleConfig
- type FileConfig
- type Logger
- type Option
- func WithAddSource(addSource bool) Option
- func WithConsole(enabled bool) Option
- func WithConsoleColor(enabled bool) Option
- func WithConsoleFormat(format OutputFormat) Option
- func WithConsoleFormatter(formatter string) Option
- func WithFile(enabled bool) Option
- func WithFileFormat(format OutputFormat) Option
- func WithFileFormatter(formatter string) Option
- func WithFilePath(path string) Option
- func WithFormat(format OutputFormat) Option
- func WithFormatter(formatter string) Option
- func WithLevel(level slog.Level) Option
- func WithMaxSizeMB(maxSizeMB int) Option
- func WithReplaceAttr(replaceAttr func(groups []string, a slog.Attr) slog.Attr) Option
- func WithRetentionDays(retentionDays int) Option
- func WithTimeFormat(timeFormat string) Option
- func WithTimeZone(timeZone *time.Location) Option
- type OutputFormat
- type ParsedTemplate
- type Token
- type TokenType
Constants ¶
const ( FormatText OutputFormat = "text" FormatJSON OutputFormat = "json" FormatCustom OutputFormat = "custom" DefaultTimeFormat = "2006/01/02 15:04:05" DefaultMaxSizeMB = 10 DefaultRetentionDays = 7 DefaultFormatter = "{time} {level} {message} {file} {attrs}" DefaultFormat = FormatText )
const ( // Placeholders PlaceholderTime = "{time}" PlaceholderLevel = "{level}" PlaceholderMessage = "{message}" PlaceholderFile = "{file}" PlaceholderAttrs = "{attrs}" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { // Base configuration Level slog.Level AddSource bool TimeFormat string TimeZone *time.Location // Configurations for different log destinations Console ConsoleConfig File FileConfig // ReplaceAttr is a function that can be used to replace attributes in log messages ReplaceAttr func(groups []string, a slog.Attr) slog.Attr }
func DefaultConfig ¶
func DefaultConfig() *Config
type ConsoleConfig ¶
type ConsoleConfig struct { Enabled bool // Enable console logging Color bool // Enable colorized output Format OutputFormat // text, json, custom Formatter string // Custom formatter string, only used if Format is FormatCustom }
func (*ConsoleConfig) GetColor ¶
func (c *ConsoleConfig) GetColor() bool
func (*ConsoleConfig) GetFormat ¶
func (c *ConsoleConfig) GetFormat() OutputFormat
ConsoleConfig implements outputConfig interface
func (*ConsoleConfig) GetFormatter ¶
func (c *ConsoleConfig) GetFormatter() string
type FileConfig ¶
type FileConfig struct { Enabled bool Format OutputFormat Formatter string // Custom formatter string, only used if Format is FormatCustom Path string // Path to the log file MaxSizeMB int // Maximum size of the log file in megabytes RetentionDays int // Number of days to retain log files }
func (*FileConfig) GetColor ¶
func (c *FileConfig) GetColor() bool
func (*FileConfig) GetFormat ¶
func (c *FileConfig) GetFormat() OutputFormat
FileConfig implements outputConfig interface
func (*FileConfig) GetFormatter ¶
func (c *FileConfig) GetFormatter() string
type Logger ¶
Logger wraps slog.Logger with automatic resource management By embedding *slog.Logger, it inherits all methods like Info, Error, Debug, Warn, With, WithGroup, etc.
func Default ¶
func Default() *Logger
Default returns a new Logger using the default slog configuration
func New ¶
New creates a new Logger with automatic resource cleanup This is the recommended way to create a logger
func (*Logger) Close ¶
Close cleans up any resources held by the logger Always call this when you're done with the logger to prevent resource leaks
func (*Logger) SetDefault ¶
func (l *Logger) SetDefault()
SetDefault sets the current logger as the default logger After setting, standard library calls like slog.Info(), slog.Error() will use this logger Note: Since Logger embeds *slog.Logger, the following methods can be used directly without additional implementation: - With(args ...any): Adds attributes and returns a new Logger - WithGroup(name string): Adds a group and returns a new Logger - Info, Warn, Error, Debug and other log level methods
type Option ¶
type Option func(*Config)
Option is a function that modifies a Config
func WithAddSource ¶
func WithConsole ¶
func WithConsoleColor ¶
func WithConsoleFormat ¶
func WithConsoleFormat(format OutputFormat) Option
func WithConsoleFormatter ¶
WithConsoleFormatter sets the console formatter for logging, and automatically sets the format to FormatCustom The formatter string can contain the following placeholders: - {time}: The timestamp of the log message - {level}: The log level of the message - {message}: The log message - {file}: The source file where the log message was generated - {attrs}: Any additional attributes associated with the log message For example: "{time} [{level}] {file} {message} {attrs}"
func WithFileFormat ¶
func WithFileFormat(format OutputFormat) Option
func WithFileFormatter ¶
func WithFilePath ¶
func WithFormat ¶
func WithFormat(format OutputFormat) Option
WithFormat sets the format of the log message for both console and file logging
func WithFormatter ¶
WithFormatter sets the formatter for both console and file logging, and automatically sets the format to FormatCustom
func WithMaxSizeMB ¶
WithMaxSizeMB sets the maximum size of the log file in megabytes. Set to 0 to disable file rotation. Negative values will be reset to the default.
func WithReplaceAttr ¶
WithReplaceAttr sets a function that can be used to replace attributes in log messages
func WithRetentionDays ¶
WithRetentionDays sets the number of days to retain log files
func WithTimeFormat ¶
func WithTimeZone ¶
type OutputFormat ¶
type OutputFormat string
type ParsedTemplate ¶
type ParsedTemplate struct {
// contains filtered or unexported fields
}
ParsedTemplate holds the pre-parsed template tokens