wlog

package
v0.0.0-...-54934fd Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2025 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

wlog is a wrapped logger

Index

Constants

This section is empty.

Variables

Functions

func FormatKindDuration

func FormatKindDuration(groups []string, a slog.Attr) (slog.Attr, bool)

func FormatKindTime

func FormatKindTime(groups []string, a slog.Attr) (slog.Attr, bool)

func FormatTypeFunc

func FormatTypeFunc(groups []string, a slog.Attr) (slog.Attr, bool)

func FormatTypeStdError

func FormatTypeStdError(groups []string, a slog.Attr) (slog.Attr, bool)

func JsonValue

func JsonValue(asString bool, data any) slog.Value

JsonValue 將資料轉換為 slog.Value,並根據 asString 決定輸出形式。

參數:

  • asString: 將資料表示為 string 形式
  • data: 只支援 struct, []byte, map, slice, 不支援基本型別

Note:

除非重寫 Handler, 目前找不到一個簡單的方法, 同時滿足 JsonHandler 以及 TextHandler 都是完美的 json 字串.

asString 參數決定資料輸出的形式,根據不同情境需要謹慎選擇:

1. when asString = true

  • config.SetJsonFormat(true),資料會被包裹在引號中 {"time":"2025-01-10T23:15:45+08:00","level":"INFO","msg":"","struct":"{\"name\":\"caesar\"}","no":9487}

  • config.SetJsonFormat(false),資料會被包裹在引號中 2025-01-10T23:15:10+08:00 INF struct="{\"name\":\"caesar\"}" no=9487

2. when asString = false

  • config.SetJsonFormat(true),資料保持良好的結構化表現 {"time":"2025-01-10T23:16:07+08:00","level":"INFO","msg":"","struct":{"name":"caesar"},"no":9487}

  • config.SetJsonFormat(false),幾乎不可讀,應避免使用 2025-01-10T23:16:32+08:00 INF struct="[123 34 110 97 109 101 34 58 34 99 97 101 115 97 114 34 125]" no=9487

func NewHandler

func NewHandler(w io.Writer, conf *Config) slog.Handler

func NewRotateWriter

func NewRotateWriter(filename string, bufSize int) (io.WriteCloser, error)

func PtrValue

func PtrValue[T any](val *T) slog.Value

Types

type Config

type Config struct {
	// Debug = -4
	// Info  = 0
	// Warn  = 4
	// Error = 8
	Level *int `yaml:"Level"`

	AddSource  *bool `yaml:"AddSource"`
	JsonFormat *bool `yaml:"JsonFormat"`
	NoColor    *bool `yaml:"NoColor"`

	Formats  []FormatFunc   `yaml:"-" json:"-"`
	LevelVar *slog.LevelVar `yaml:"-" json:"-"`
}

func (*Config) SetAddSource

func (conf *Config) SetAddSource(add bool) *Config

func (*Config) SetFormats

func (conf *Config) SetFormats(formats ...FormatFunc) *Config

func (*Config) SetJsonFormat

func (conf *Config) SetJsonFormat(json bool) *Config

func (*Config) SetLevelVar

func (conf *Config) SetLevelVar(level int) *Config

func (*Config) SetNoColor

func (conf *Config) SetNoColor(noColor bool) *Config

type FormatFunc

type FormatFunc func(groups []string, a slog.Attr) (slog.Attr, bool)

func FormatKeySource

func FormatKeySource() FormatFunc

type Logger

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

func LoggerFactory

func LoggerFactory(filename string, conf *Config) (logger *Logger, w io.WriteCloser, err error)

func NewDiscardLogger

func NewDiscardLogger() *Logger

func NewLogger

func NewLogger(lvl *slog.LevelVar, handlers ...slog.Handler) *Logger

func NewStderrLogger

func NewStderrLogger(conf *Config) *Logger

func NewStderrLoggerWhenDebug

func NewStderrLoggerWhenDebug() *Logger

func NewStderrLoggerWhenNormal

func NewStderrLoggerWhenNormal(source bool) *Logger

func (*Logger) CtxGetLogger

func (l *Logger) CtxGetLogger(ctx context.Context) (logger *slog.Logger)

func (*Logger) CtxWithLogger

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

func (*Logger) Level

func (l *Logger) Level() slog.Level

func (*Logger) PointToNew

func (l *Logger) PointToNew(new *Logger)

PointToNew 通過改變指標的指向, 讓所有引用此指標的其他元件獲得最新的狀態

以下是命名方式的分類

改變指標本身: Replace, Set

改變指標的指向: Redirect, PointTo

func (*Logger) SetLevel

func (l *Logger) SetLevel(lvl slog.Level)

func (*Logger) SetStdDefaultLevel

func (l *Logger) SetStdDefaultLevel()

func (*Logger) SetStdDefaultLogger

func (l *Logger) SetStdDefaultLogger()

SetStdDefaultLogger 將標準庫的預設值, 以我方的物件為基準, 控制 logger 行為

func (*Logger) Slog

func (l *Logger) Slog() *slog.Logger

func (*Logger) WithAttribute

func (l *Logger) WithAttribute(with func(*slog.Logger) *slog.Logger)

type RotateWriter

type RotateWriter struct {
	Logger *slog.Logger
	// contains filtered or unexported fields
}

func (*RotateWriter) Close

func (w *RotateWriter) Close() (err error)

func (*RotateWriter) Write

func (w *RotateWriter) Write(p []byte) (nn int, err error)

Jump to

Keyboard shortcuts

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