zerolog
Logger structure
New
New
returns a new Logger instance via the newLogger
function
Function Signature:
Sample code:
From
From
returns a new Logger using an existing Logger via newLogger
Function Signature:
Sample code:
GetLogger
GetLogger
returns the default Logger instance and error via the DefaultLogger()
method
Function Signature:
Sample code:
Unwrap
Unwrap
returns the underlying zerolog logger
Function Signature:
Sample code:
Option configuration
WithOutput
WithOutput
returns an Opt function through zerolog’s zerolog.Context.Logger().Output(out).With()
, allowing to specify the output of the logger. By default, it is set to os.Stdout
Function Signature:
Sample code:
WithLevel
WithLevel
specifies the level of the logger through zerolog’s built-in zerolog.Context.Logger().Level(lvl).With()
method. Convert hlog.Level to zerolog.level by matchHlogLevel()
. By default it is set to WarnLevel
Function Signature:
Sample code:
WithField
WithField
adds a field to the logger’s context through zerolog’s zerolog.Context.Interface(name, value)
method
Function Signature:
Sample code:
WithFields
WithFields
adds some fields to the logger’s context through zerolog’s zerolog.Context.Fields(fields)
Function Signature:
Sample code:
WithTimestamp
WithTimestamp
adds the timestamp field to the logger’s context through zerolog’s zerolog.Context.Timestamp()
Function Signature:
Sample code:
WithFormattedTimestamp
WithFormattedTimestamp
is similar to WithTimestamp
, adding a formatted timestamp field to the logger’s context
Function Signature:
Sample code:
WithCaller
WithCaller
adds a caller to the context of the logger through zerolog’s built-in zerolog.Context.Caller()
, and the caller will report the caller’s information
Function Signature:
Sample code:
WithCallerSkipFrameCount
WithCallerSkipFrameCount
adds caller
to logger
’s Context. CallerWithSkipFrameCount
is a method of zerolog’s Context structure. It is used to add the caller’s file name and line number in the log record, and uses zerolog.CallerFieldName
as key name
This method accepts a skipFrameCount
parameter, which specifies the number of stack frames to skip to determine the correct caller location. If the skipFrameCount
parameter is set to -1, the global CallerSkipFrameCount
value is used
After calling the CallerWithSkipFrameCount
method, a new Context structure is created, and the newCallerHook
method is used to create a new hook and add it to the logger
Function Signature:
Sample code:
WithHook
WithHook
adds a hook to the context of logger through zerolog’s zerolog.Context.Logger().Hook(hook).With()
Function Signature:
Sample code:
WithHookFunc
WithHookFunc
is similar to WithHook
, adding a hook function to the context of logger
Function Signature:
Sample code:
A complete logrus example
For more details on how to adapt the interface of hlog, see hertz-contrib/logger/zerolog