Documentation ¶
Index ¶
- Constants
- Variables
- func GetColor(m LevelColorMap, lvl slog.Level) int
- func GetLevelName(level slog.Level) (name string)
- type Handler
- func NewHandler(w io.Writer, color LevelColorMap, safe bool, layout string, sep string, ...) *Handler
- func NewJSONHandler(w io.Writer, opts *slog.HandlerOptions) *Handler
- func NewPrettyHandler(w io.Writer, opts *slog.HandlerOptions) *Handler
- func NewStructHandler(w io.Writer, opts *slog.HandlerOptions) *Handler
- type HandlerBuilder
- func (b *HandlerBuilder) Build() *Handler
- func (b *HandlerBuilder) WithColor(color LevelColorMap) *HandlerBuilder
- func (b *HandlerBuilder) WithInsecure() *HandlerBuilder
- func (b *HandlerBuilder) WithJSON() *HandlerBuilder
- func (b *HandlerBuilder) WithOptions(opts *slog.HandlerOptions) *HandlerBuilder
- func (b *HandlerBuilder) WithPretty() *HandlerBuilder
- func (b *HandlerBuilder) WithSeparator(sep string) *HandlerBuilder
- func (b *HandlerBuilder) WithTimeLayout(layout string) *HandlerBuilder
- func (b *HandlerBuilder) WithWriter(w io.Writer) *HandlerBuilder
- type LevelColorMap
- type LogColor
Constants ¶
const ( LevelFx = slog.Level(-8) LevelFxError = slog.Level(-7) LevelDebug = slog.LevelDebug LevelInfo = slog.LevelInfo LevelWarning = slog.LevelWarn LevelError = slog.LevelError )
LevelFx and LevelFxError represents a custom logging level for FxHandler. It has a value of -8 and -7.
const ( StructSep = " " JSONSep = "," PrettySep = " | " )
const ( DefaultDateTimeLayout = "15:04:05 02-01-2006" DefaultPrettyDateTimeLayout = "15:04:05 Mon _2 Jan 2006" )
Variables ¶
var DefaultColorMap = LevelColorMap{ LevelFx: LogColor(color.FgCyan), LevelFxError: LogColor(color.FgHiRed), LevelDebug: LogColor(color.FgBlue), LevelInfo: LogColor(color.FgHiGreen), LevelWarning: LogColor(color.FgYellow), LevelError: LogColor(color.FgRed), }
DefaultColorMap is the default color mapping used for logging.
var EmptyColorMap = LevelColorMap{}
EmptyColorMap is the empty color mapping used for safe logging.
Functions ¶
func GetColor ¶
func GetColor(m LevelColorMap, lvl slog.Level) int
GetColor returns the integer value of the LogColor associated with the given slog.Level in the LevelColorMap. If the given slog.Level is not found in the LevelColorMap, it returns the integer value of color.FgWhite (37).
func GetLevelName ¶
GetLevelName takes a slog.Level as input and returns the corresponding name as a string. If the level is LevelFx, it returns "FX". If the level is LevelFxError, it returns "FXError". For any other level, it returns the string representation of the level.
Types ¶
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler is a modified version of the original commonHandler from the log/slog.
func NewHandler ¶
func NewHandler(w io.Writer, color LevelColorMap, safe bool, layout string, sep string, opts *slog.HandlerOptions) *Handler
NewHandler is manually constructor, please use NewHandlerBuilder. `safeSet` from coding/json/tables.go is used to escape the string Warning! HandlerOptions is WIP. You can use it, but at one's own risk. Only for tests, use builder please! Default setting close to NewPrettyHandler.
func NewJSONHandler ¶
func NewJSONHandler(w io.Writer, opts *slog.HandlerOptions) *Handler
NewJSONHandler is analog for slog.NewJSONHandler
func NewPrettyHandler ¶
func NewPrettyHandler(w io.Writer, opts *slog.HandlerOptions) *Handler
NewPrettyHandler is default otris handler without settings. If you need advanced settings please use NewHandlerBuilder
func NewStructHandler ¶
func NewStructHandler(w io.Writer, opts *slog.HandlerOptions) *Handler
NewStructHandler is analog for slog.NewTextHandler
type HandlerBuilder ¶
type HandlerBuilder struct {
// contains filtered or unexported fields
}
HandlerBuilder is a type that helps in building a Handler by setting various options.
Usage:
handler := NewHandlerBuilder().WithColor(color).WithSafeSet(safe).WithTimeLayout(layout).Build()
func NewHandlerBuilder ¶
func NewHandlerBuilder() *HandlerBuilder
NewHandlerBuilder creates a new instance of HandlerBuilder. It initializes the fields of HandlerBuilder with default values. The returned HandlerBuilder can be used to configure and build a Handler.
Returns a pointer to the created HandlerBuilder.
func (*HandlerBuilder) Build ¶
func (b *HandlerBuilder) Build() *Handler
Build returns the final built Handler instance from the HandlerBuilder. It simply returns the value of the h field in the HandlerBuilder. If pretty is true, then insecure is enabled. If json is true, then pretty, insecure, color is disabled and sep is ','. Returns the final built Handler instance.
func (*HandlerBuilder) WithColor ¶
func (b *HandlerBuilder) WithColor(color LevelColorMap) *HandlerBuilder
WithColor sets the color map for different log levels in the HandlerBuilder. If the color map is not nil, it updates the color map of the Handler. Returns the updated HandlerBuilder.
func (*HandlerBuilder) WithInsecure ¶
func (b *HandlerBuilder) WithInsecure() *HandlerBuilder
WithInsecure sets the safe flag to FALSE for the HandlerBuilder. If the insecure flag is true, it indicates that the handler is in a safe set. Returns the updated HandlerBuilder.
func (*HandlerBuilder) WithJSON ¶
func (b *HandlerBuilder) WithJSON() *HandlerBuilder
WithJSON sets the JSON flag to TRUE for the HandlerBuilder. If the JSON flag is true, it indicates that the log messages should be formatted in JSON. Returns the updated HandlerBuilder.
!!!Warning!!! JSON mode disable all addition otris functions. !!!Warning!!!
func (*HandlerBuilder) WithOptions ¶
func (b *HandlerBuilder) WithOptions(opts *slog.HandlerOptions) *HandlerBuilder
WithOptions sets the options for the HandlerBuilder. If the options are not nil, it updates the options of the Handler. Returns the updated HandlerBuilder.
func (*HandlerBuilder) WithPretty ¶
func (b *HandlerBuilder) WithPretty() *HandlerBuilder
WithPretty sets the `pretty`, `safe`, `color`, `layout`, and `sep` fields of the HandlerBuilder to their pretty values. It updates the pretty flag to true, the safe flag to true, the color map with the DefaultColorMap, the layout to DefaultPrettyDateTimeLayout, and the sep to PrettySep. Returns the updated HandlerBuilder.
func (*HandlerBuilder) WithSeparator ¶
func (b *HandlerBuilder) WithSeparator(sep string) *HandlerBuilder
WithSeparator sets the separator for the log message attributes in the HandlerBuilder. If the separator is not nil, it updates the separator of the Handler. Returns the updated HandlerBuilder.
func (*HandlerBuilder) WithTimeLayout ¶
func (b *HandlerBuilder) WithTimeLayout(layout string) *HandlerBuilder
WithTimeLayout sets the custom time layout for log messages in the HandlerBuilder. If the layout is not nil, it updates the time layout of the Handler. Returns the updated HandlerBuilder.
func (*HandlerBuilder) WithWriter ¶
func (b *HandlerBuilder) WithWriter(w io.Writer) *HandlerBuilder
WithWriter sets the writer for the HandlerBuilder. If the writer is not nil, it updates the writer of the Handler. Returns the updated HandlerBuilder.
type LevelColorMap ¶
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
internal
|
|
slog/buffer
Package buffer provides a pool-allocated byte buffer.
|
Package buffer provides a pool-allocated byte buffer. |