wslog

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2024 License: MIT Imports: 8 Imported by: 0

README

Wrapper for slog

Install

go get github.com/mpuzanov/wslog

Added methods

// NewEnv Creating a logger based on Env (local, dev, prod)  
func NewEnv(env string) *Logger

// New create logger  
func New(opts ...LoggerOption) *Logger

Examples

logger := wslog.New(
		wslog.WithLevel(cfg.Log.Level),
		wslog.WithIsJSON(cfg.Log.IsJSON),
		wslog.WithFileLog(cfg.Log.File),
		wslog.WithAddSource(cfg.Log.AddSource),
	)

or

logger := wslog.NewEnv(cfg.Env)
logger.Debug("debug", wslog.Any("cfg", cfg))

logger.Info(cfg.ServiceName,
		wslog.String("version", config.Version),
		wslog.String("time", config.Time),
		wslog.String("log_level", cfg.Log.Level),
	)

adds an slog attribute to the provided context


ctx := r.Context()
reqID := uuid.New().String()
ctx = wslog.AppendCtx(ctx, slog.String("reqID", reqID))

slog.InfoContext(ctx, "request",
	slog.String("user_ip", r.RemoteAddr),
	slog.String("path", r.URL.Path),
	slog.String("method", r.Method),
)

Documentation

Overview

Example
package main

import (
	"github.com/mpuzanov/wslog"
)

func main() {
	wslog.RemoveTime = true
	l := wslog.NewEnv("local")
	l.Info("hello")

}
Output:

level=INFO msg=hello

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// SetDefault ...
	SetDefault = slog.SetDefault

	// String алиасы типов
	String = slog.String
	// Bool ...
	Bool = slog.Bool
	// Float64 ...
	Float64 = slog.Float64
	// Any ...
	Any = slog.Any
	// Duration ...
	Duration = slog.Duration
	// Int ...
	Int = slog.Int
	// Int64 ...
	Int64 = slog.Int64

	// GroupValue ...
	GroupValue = slog.GroupValue
	// Group ...
	Group = slog.Group
)
View Source
var (

	// RemoveTime убрать time из логов
	RemoveTime bool
)

Functions

func AppendCtx added in v0.1.2

func AppendCtx(parent context.Context, attr slog.Attr) context.Context

AppendCtx adds an slog attribute to the provided context so that it will be included in any Record created with such context

Example
package main

import (
	"context"

	"github.com/mpuzanov/wslog"
)

func main() {
	wslog.RemoveTime = true
	l := wslog.New()
	ctx := wslog.AppendCtx(context.Background(), wslog.String("userID", "1"))
	l.InfoContext(ctx, "example1")
	ctx = wslog.AppendCtx(ctx, wslog.String("userID", "2"))
	l.InfoContext(ctx, "example2")
	ctx = wslog.AppendCtx(ctx, wslog.String("metod", "GET"))
	l.InfoContext(ctx, "example3")

}
Output:

level=INFO msg=example1 userID=1
level=INFO msg=example2 userID=2
level=INFO msg=example3 userID=2 metod=GET

func ContextWithLogger

func ContextWithLogger(ctx context.Context, l *slog.Logger) context.Context

ContextWithLogger adds logger to context

func GetLogLevel

func GetLogLevel() slog.Level

GetLogLevel ...

func SetLogLevel

func SetLogLevel(level string) slog.Level

SetLogLevel ...

Types

type Attr

type Attr = slog.Attr

Attr ...

func ErrAttr

func ErrAttr(err error) Attr

ErrAttr helper func

logger.Error("user msg error", wslog.ErrAttr(err))

type ContextHandler added in v0.1.2

type ContextHandler struct {
	slog.Handler
}

ContextHandler ...

func (ContextHandler) Handle added in v0.1.2

func (h ContextHandler) Handle(ctx context.Context, r slog.Record) error

Handle adds contextual attributes to the Record before calling the underlying handler

type Handler

type Handler = slog.Handler

Handler ...

type HandlerOptions

type HandlerOptions = slog.HandlerOptions

HandlerOptions ...

type Level

type Level = slog.Level

Level ...

type LogValuer

type LogValuer = slog.LogValuer

LogValuer ...

type Logger

type Logger = slog.Logger

Logger алиасы типов

func Default

func Default() *Logger

Default ...

func L

func L(ctx context.Context) *Logger

L loggerFromContext

func New

func New(opts ...LoggerOption) *Logger

New create logger

func NewEnv

func NewEnv(env string) *Logger

NewEnv создание логера на основе Env (local, dev, prod)

func WithAttrs

func WithAttrs(ctx context.Context, attrs ...Attr) *Logger

WithAttrs returns logger with attributes.

type LoggerOption

type LoggerOption func(options *options)

LoggerOption ...

func WithAddSource

func WithAddSource(addSource bool) LoggerOption

WithAddSource logger option sets the add source option, which will add source file and line number to the log record.

func WithFileLog

func WithFileLog(fileName string) LoggerOption

WithFileLog ...

func WithIsJSON

func WithIsJSON(IsJSON bool) LoggerOption

WithIsJSON ...

func WithLevel

func WithLevel(level string) LoggerOption

WithLevel ...

func WithOnlyFile

func WithOnlyFile(onlyFile bool) LoggerOption

WithOnlyFile ...

func WithWriter

func WithWriter(w io.Writer) LoggerOption

WithWriter ...

type Value

type Value = slog.Value

Value ...

Jump to

Keyboard shortcuts

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