zerolog.go
zerolog.go - Overview
This file defines a custom handler ZerologHandler
that adapts the standard slog
package to the zerolog
package. It includes functions to handle log levels, attributes, and grouping for use with zerolog
.
Detailed Documentation
ZerologHandler
- Purpose:
ZerologHandler
is a custom handler that adaptsslog
tozerolog
. - Structure:
type ZerologHandler struct {
logger *zerolog.Logger
}
newZerologHandler
-
Purpose: Creates a new
ZerologHandler
. -
Parameters:
logger
(*zerolog.Logger): A pointer to azerolog.Logger
instance.
-
Returns:
*ZerologHandler
: A pointer to the newly createdZerologHandler
.
func newZerologHandler(logger *zerolog.Logger) *ZerologHandler
Handle
- Purpose: Implements the
slog.Handler
interface. This method handles the actual logging by converting theslog.Record
to azerolog
event. - Parameters:
_
(context.Context): The context (not used in the implementation).record
(slog.Record): The log record containing the log level, attributes, and message.
- Returns:
error
: An error, if any occurred during the logging process.
- Note:
- The
//nolint:gocritic
comment indicates that agocritic
linter warning is being ignored for theslog.Record
struct, possibly due to ahugeParam
issue, but it's acknowledged that the interface is from the standard library and not under direct control.
func (h *ZerologHandler) Handle(_ context.Context, record slog.Record) error
- The
Enabled
- Purpose: Implements the
slog.Handler
interface. Determines if a given log level is enabled for this handler. - Parameters:
_
(context.Context): The context (not used in the implementation).level
(slog.Level): The log level to check.
- Returns:
bool
:true
if the log level is enabled; otherwise,false
.
func (h *ZerologHandler) Enabled(_ context.Context, level slog.Level) bool
WithAttrs
- Purpose: Adds attributes to the log event. Implements the
slog.Handler
interface. - Parameters:
attrs
([]slog.Attr): A slice ofslog.Attr
attributes to add.
- Returns:
slog.Handler
: A newZerologHandler
with the added attributes.
func (h *ZerologHandler) WithAttrs(attrs []slog.Attr) slog.Handler
WithGroup
- Purpose: Returns a handler for a group of logs. Implements the
slog.Handler
interface. - Parameters:
name
(string): The name of the group.
- Returns:
slog.Handler
: A newZerologHandler
for the specified group.
func (h *ZerologHandler) WithGroup(name string) slog.Handler
addAttrToZerolog
- Purpose: A generic function to add a
slog.Attr
to either azerolog.Event
orzerolog.Context
. - Parameters:
attr
(slog.Attr): The attribute to add.target
(T): The target object to add the attribute to (eitherzerolog.Event
orzerolog.Context
).T
is constrained by an interface including methods likeStr
,Int64
,Float64
, etc.
- Returns:
T
: The modified target object with the attribute added.
func addAttrToZerolog[T interface {
Str(string, string) T
Int64(string, int64) T
Uint64(string, uint64) T
Float64(string, float64) T
Bool(string, bool) T
Err(error) T
Time(string, time.Time) T
Dur(string, time.Duration) T
Interface(string, any) T
AnErr(key string, err error) T
}](attr slog.Attr, target T) T
toZerologLevel
- Purpose: Maps
slog
levels tozerolog
levels. - Parameters:
level
(slog.Level): Theslog
level to convert.
- Returns:
zerolog.Level
: The correspondingzerolog
level.
func toZerologLevel(level slog.Level) zerolog.Level
Code Examples
None.