Documentation ¶
Overview ¶
wlog is a wrapped logger
Index ¶
- Variables
- func FormatKindDuration(groups []string, a slog.Attr) (slog.Attr, bool)
- func FormatKindTime(groups []string, a slog.Attr) (slog.Attr, bool)
- func FormatTypeFunc(groups []string, a slog.Attr) (slog.Attr, bool)
- func FormatTypeStdError(groups []string, a slog.Attr) (slog.Attr, bool)
- func JsonValue(asString bool, data any) slog.Value
- func NewHandler(w io.Writer, conf *Config) slog.Handler
- func NewRotateWriter(filename string, bufSize int) (io.WriteCloser, error)
- func PtrValue[T any](val *T) slog.Value
- type Config
- type FormatFunc
- type Logger
- func LoggerFactory(filename string, conf *Config) (logger *Logger, w io.WriteCloser, err error)
- func NewDiscardLogger() *Logger
- func NewLogger(lvl *slog.LevelVar, handlers ...slog.Handler) *Logger
- func NewStderrLogger(conf *Config) *Logger
- func NewStderrLoggerWhenDebug() *Logger
- func NewStderrLoggerWhenNormal(source bool) *Logger
- func (l *Logger) CtxGetLogger(ctx context.Context) (logger *slog.Logger)
- func (l *Logger) CtxWithLogger(ctx context.Context, v *slog.Logger) context.Context
- func (l *Logger) Level() slog.Level
- func (l *Logger) PointToNew(new *Logger)
- func (l *Logger) SetLevel(lvl slog.Level)
- func (l *Logger) SetStdDefaultLevel()
- func (l *Logger) SetStdDefaultLogger()
- func (l *Logger) Slog() *slog.Logger
- func (l *Logger) WithAttribute(with func(*slog.Logger) *slog.Logger)
- type RotateWriter
Constants ¶
This section is empty.
Variables ¶
var DefaultFormats = []FormatFunc{ FormatKeySource(), FormatKindTime, FormatKindDuration, FormatTypeFunc, FormatTypeStdError, }
Functions ¶
func JsonValue ¶
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 NewRotateWriter ¶
func NewRotateWriter(filename string, bufSize int) (io.WriteCloser, error)
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 (*Config) SetFormats ¶
func (conf *Config) SetFormats(formats ...FormatFunc) *Config
func (*Config) SetJsonFormat ¶
func (*Config) SetLevelVar ¶
func (*Config) SetNoColor ¶
type FormatFunc ¶
func FormatKeySource ¶
func FormatKeySource() FormatFunc
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
func LoggerFactory ¶
func NewDiscardLogger ¶
func NewDiscardLogger() *Logger
func NewStderrLogger ¶
func NewStderrLoggerWhenDebug ¶
func NewStderrLoggerWhenDebug() *Logger
func (*Logger) CtxGetLogger ¶
func (*Logger) CtxWithLogger ¶
func (*Logger) PointToNew ¶
PointToNew 通過改變指標的指向, 讓所有引用此指標的其他元件獲得最新的狀態
以下是命名方式的分類
改變指標本身: Replace, Set
改變指標的指向: Redirect, PointTo
func (*Logger) SetStdDefaultLevel ¶
func (l *Logger) SetStdDefaultLevel()
func (*Logger) SetStdDefaultLogger ¶
func (l *Logger) SetStdDefaultLogger()
SetStdDefaultLogger 將標準庫的預設值, 以我方的物件為基準, 控制 logger 行為
type RotateWriter ¶
func (*RotateWriter) Close ¶
func (w *RotateWriter) Close() (err error)