otris

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 27, 2024 License: MIT Imports: 19 Imported by: 0

README

otris

otris is pretty logger, extension for the slog library, developed as part of the Open-Streaming-Solutions project.

Showcase

<--- otris --->
05:26:24 Fri 26 Jul 2024 | FX | FX error level encountered | 101 | FXModule | 05:26:24 Fri 26 Jul 2024
05:26:24 Fri 26 Jul 2024 | FXError | FX critical error! Restart needed. | 202 | CriticalModule | 05:26:24 Fri 26 Jul 2024 | Why don’t programmers like nature? It has too many bugs.
05:26:24 Fri 26 Jul 2024 | DEBUG | Debugging connection issue | 3 | AuthService | 05:26:24 Fri 26 Jul 2024 | A SQL query goes into a bar, walks up to two tables and asks, 'Can I join you?'
05:26:24 Fri 26 Jul 2024 | INFO | Service started successfully | PaymentService | 05:26:24 Fri 26 Jul 2024 | How many programmers does it take to change a light bulb? None. It’s a hardware problem.
05:26:24 Fri 26 Jul 2024 | WARN | High memory usage detected | 85.7 | Cache | 05:26:24 Fri 26 Jul 2024 | Why do Java developers wear glasses? Because they can’t C#.
05:26:24 Fri 26 Jul 2024 | ERROR | Unexpected server shutdown | 48 | MainServer | 05:26:24 Fri 26 Jul 2024 | To understand recursion, you must first understand recursion.

img.png

Documentation

Index

Constants

View Source
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.

View Source
const (
	StructSep = " "
	JSONSep   = ","
	PrettySep = " | "
)
View Source
const (
	DefaultDateTimeLayout       = "15:04:05 02-01-2006"
	DefaultPrettyDateTimeLayout = "15:04:05 Mon _2 Jan 2006"
)

Variables

View Source
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.

View Source
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

func GetLevelName(level slog.Level) (name string)

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

func (*Handler) Enabled

func (h *Handler) Enabled(_ context.Context, level slog.Level) bool

func (*Handler) Handle

func (h *Handler) Handle(_ context.Context, record slog.Record) error

func (*Handler) WithAttrs

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs returns a new Handler with additional attributes specified in `attrs` parameter. TODO Implement custom groupPrefix in v2

func (*Handler) WithGroup

func (h *Handler) WithGroup(name string) slog.Handler

WithGroup returns a new Handler with the given `name` appended to the `groups` slice.

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

type LevelColorMap map[slog.Level]LogColor

type LogColor

type LogColor int

LogColor defines a single SGR Code

Directories

Path Synopsis
internal
slog/buffer
Package buffer provides a pool-allocated byte buffer.
Package buffer provides a pool-allocated byte buffer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL