Documentation ¶
Index ¶
Constants ¶
const ( RFC3339Milli = "2006-01-02T15:04:05.000Z07:00" RFC3339Micro = "2006-01-02T15:04:05.000000Z07:00" )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler is a custom implementation of slog.Handler that provides advanced formatting capabilities for log records. It offers the following features:
- Customizable time value formatting for both log timestamps and time attributes
- Support for log record tagging using square brackets before the message
- Optional inclusion of source code information (file and line number)
func NewHandler ¶
NewHandler creates a new Handler with the provided io.Writer and Options. If no Options are provided, it will use the default Options. The default Options include:
- Level set to slog.LevelInfo
- Time formats are set to RFC3339Milli
- Time values are in local time zone
If Level is not set in opts, it will default to slog.LevelInfo.
func NewHandlerWithOptions ¶ added in v0.2.0
NewHandlerWithOptions creates a new Handler with the provided io.Writer and a set of configurable options. The options allow customizing the log level, whether to include source location, the time format, and whether to use UTC time. If no options are provided, it will use the default options with the time format set to RFC3339Milli.
func (*Handler) Enabled ¶
Enabled returns whether the given log level is enabled for this Handler. The Handler will only log records with a level greater than or equal to the configured level.
func (*Handler) Handle ¶
Handle processes a log record and writes it to the configured io.Writer. It appends the time, level, tag (if set), source location (if configured), message, and attributes to the output. The output is formatted according to the configured Options.
type Option ¶ added in v0.2.0
type Option = func(*Options)
func WithAddSource ¶ added in v0.2.0
WithAddSource returns an Option that sets whether to include the source code location (file and line number) in the log record.
func WithLevel ¶ added in v0.2.0
WithLevel returns an Option that sets the log level for the Handler. The provided level will be used to filter log records, only records at or above the specified level will be logged.
func WithTimeAttributeFormat ¶ added in v0.2.0
WithTimeAttributeFormat returns an Option that sets the time format for the time attribute in the log record. The provided timeAttributeFormat string should be a valid time.Format layout string.
func WithTimeAttributeInUTC ¶ added in v0.2.0
WithTimeAttributeInUTC returns an Option that sets whether to use UTC time for the time attribute in the log record. If timeAttributeInUTC is true, the time attribute will be in UTC time, otherwise it will be in the local time zone.
func WithTimeFormat ¶ added in v0.2.0
WithTimeFormat returns an Option that sets the time format for the log record timestamp. The provided timeFormat string should be a valid time.Format layout string.
func WithTimeInUTC ¶ added in v0.2.0
WithTimeInUTC returns an Option that sets whether to use UTC time for the log record timestamp. If timeInUTC is true, the timestamp will be in UTC time, otherwise it will be in the local time zone.
type Options ¶
type Options struct { // Level reports the minimum level to log. // Levels with lower levels are discarded. // If nil, the Handler uses [slog.LevelInfo]. Level slog.Leveler // AddSource causes the handler to compute the source code position // of the log statement and add a SourceKey attribute to the output. AddSource bool // TimeFormat is the format used for timestamps in the output. // If empty, the Handler will omit the timestamps. TimeFormat string // TimeInUTC specifies whether the time format should use UTC instead of the local time zone. TimeInUTC bool // TimeAttributeFormat specifies the time format used for the time attribute // in the log record. If empty, the default time format of time.RFC3339 is used. TimeAttributeFormat string // TimeAttributeInUTC specifies whether the time attribute in the log record // should use UTC instead of the local time zone. TimeAttributeInUTC bool }