slogx

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2026 License: MIT Imports: 7 Imported by: 1

Documentation

Overview

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. The original code is from: 1.blog https://avito.tech/old/tpost/yfhycud6h1-logirovanie-kak-v-avito-goslog 2.code https://github.com/avito-tech/avitotech-presentations/commit/cf5ff7ea041dcdd3846634239d9ac27d5c80a86a

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CombineContexts

func CombineContexts(logctx context.Context, requestCtx context.Context) context.Context

func ErrorCtx

func ErrorCtx(ctx context.Context, err error) context.Context

ErrorCtx adds ctx to err

func WithLogAppName

func WithLogAppName(ctx context.Context, appName string) context.Context

WithLogAppName by execution add appName to log record

func WithLogLanguage

func WithLogLanguage(ctx context.Context, language string) context.Context

WithLogLanguage by execution add language to log record

func WithLogMessage

func WithLogMessage(ctx context.Context, message string) context.Context

WithLogMessage by execution add message to log record

func WithLogPhone

func WithLogPhone(ctx context.Context, phone string) context.Context

WithLogPhone by execution add phone to log record

func WithLogScope

func WithLogScope(ctx context.Context, envScope string) context.Context

WithLogScope by execution add scope to log record

func WithLogTraceID

func WithLogTraceID(ctx context.Context, traceID string) context.Context

func WithLogType

func WithLogType(ctx context.Context, typelog string) context.Context

WithLogType by execution add type to log record

func WithLogUserID

func WithLogUserID(ctx context.Context, userID uuid.UUID) context.Context

WithLogUserID by execution add userID to log record

func WithSpanID

func WithSpanID(ctx context.Context, spanID string) context.Context

func WrapError

func WrapError(ctx context.Context, err error) error

WrapError wraps err into ctx like linked list

Types

type HandlerMiddlware

type HandlerMiddlware struct {
	// contains filtered or unexported fields
}

func NewHandlerMiddleware

func NewHandlerMiddleware(next slog.Handler) *HandlerMiddlware

NewHandlerMiddleware create new middleware

func (*HandlerMiddlware) Enabled

func (h *HandlerMiddlware) Enabled(ctx context.Context, rec slog.Level) bool

Enabled reports whether the handler handles records at the given level.

func (*HandlerMiddlware) Handle

func (h *HandlerMiddlware) Handle(ctx context.Context, rec slog.Record) error

Handle is called for each log record and add it to the record.

func (*HandlerMiddlware) WithAttrs

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

WithAttrs adds attributes to the log record.

func (*HandlerMiddlware) WithGroup

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

WithGroup adds a new group to the log record.

type MultiHandler

type MultiHandler struct {
	// contains filtered or unexported fields
}

MultiHandler объединяет несколько slog.Handler и вызывает их при логировании.

func InitLogging

func InitLogging(opts ...Option) *MultiHandler

InitLogging initializes logging with the given options and sets slog.Default. If no handler options (WithJSONHandler, WithTextHandler, WithHandler) are provided, NO default handler is added — you must explicitly choose your format.

Returns *MultiHandler so you can inject more handlers later (e.g. OTel bridge).

func InitLoggingJSON added in v0.3.0

func InitLoggingJSON(w io.Writer, opts ...Option) *MultiHandler

InitLoggingJSON is a convenience shortcut that behaves like the old InitLogging: creates a JSON handler writing to w, wraps it in HandlerMiddleware, applies extra opts.

func NewMultiHandler

func NewMultiHandler(handlers ...slog.Handler) *MultiHandler

NewMultiHandler создаёт новый мульти-обработчик.

func (*MultiHandler) AddHandler added in v0.3.0

func (mh *MultiHandler) AddHandler(h slog.Handler)

AddHandler добавляет хендлер в рантайме (thread-safe). Позволяет inject'ить хендлеры после создания логгера.

func (*MultiHandler) Enabled

func (mh *MultiHandler) Enabled(ctx context.Context, level slog.Level) bool

Enabled возвращает true, если хотя бы один из обработчиков включён для данного уровня.

func (*MultiHandler) Handle

func (mh *MultiHandler) Handle(ctx context.Context, r slog.Record) error

Handle вызывает Handle для каждого из обработчиков.

func (*MultiHandler) WithAttrs

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

WithAttrs возвращает новый MultiHandler с добавленными атрибутами для каждого из обработчиков.

func (*MultiHandler) WithGroup

func (mh *MultiHandler) WithGroup(name string) slog.Handler

WithGroup возвращает новый MultiHandler с добавленной группой для каждого из обработчиков.

type Option

type Option func(*MultiHandler)

func WithHandler added in v0.3.0

func WithHandler(handler slog.Handler) Option

WithHandler adds any custom slog.Handler. The handler is wrapped with HandlerMiddleware for context extraction.

func WithJSONHandler added in v0.3.0

func WithJSONHandler(w io.Writer, opts *slog.HandlerOptions) Option

WithJSONHandler adds slog.JSONHandler as the primary handler.

func WithOtelSlogOption

func WithOtelSlogOption(serviceName string, opts ...otelslog.Option) Option

WithOtelSlogOption adds OpenTelemetry slog bridge handler.

func WithRawHandler added in v0.3.0

func WithRawHandler(handler slog.Handler) Option

WithRawHandler adds any custom slog.Handler WITHOUT wrapping in HandlerMiddleware. Use this when you handle context extraction yourself.

func WithTextHandler added in v0.3.0

func WithTextHandler(w io.Writer, opts *slog.HandlerOptions) Option

WithTextHandler adds slog.TextHandler as the primary handler.

Jump to

Keyboard shortcuts

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