log

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2024 License: ISC Imports: 15 Imported by: 3

README

📄 log

A ready-to-use stubborn log library encapsulates the log module, providing functions such as log output, log level.

✨ Features

  • 可选择 log/slog, zap 作为日志库
  • 支持本地日志文件轮转

📦 Usage

go get -u github.com/tsingshaner/go-pkg/log
🌿 Use log/slog

example/log/slog

⚡ Use zap

Use zap you need add zap to your project dependencies.

go get -u go.uber.org/zap

example/log/zap

🎨 Pretty log

If you want a pretty log in the console for development, you can install a cli tool prettylog.

go install github.com/tsingshaner/go-pkg/prettylog/cmd/prettylog

Then you can use it like this:

go run main.go | prettylog

📄 License

ISC License © 2024-Present qingshaner

Documentation

Index

Constants

View Source
const (
	Trace = "trace"
	Debug = "debug"
	Info  = "info"
	Warn  = "warn"
	Error = "error"
	Fatal = "fatal"
	Panic = "panic"
)
View Source
const (
	SlogLevelSilent = slog.Level(LevelSilent)
	SlogLevelTrace  = slog.Level(LevelTrace)
	SlogLevelDebug  = slog.Level(LevelDebug)
	SlogLevelInfo   = slog.Level(LevelInfo)
	SlogLevelWarn   = slog.Level(LevelWarn)
	SlogLevelError  = slog.Level(LevelError)
	SlogLevelFatal  = slog.Level(LevelFatal)
	SlogLevelAll    = slog.Level(LevelAll)
)
View Source
const (
	ZapLevelSilent = zapcore.Level(LevelSilent)
	ZapLevelTrace  = zapcore.Level(LevelTrace)
	ZapLevelDebug  = zapcore.Level(LevelDebug)
	ZapLevelInfo   = zapcore.Level(LevelInfo)
	ZapLevelWarn   = zapcore.Level(LevelWarn)
	ZapLevelError  = zapcore.Level(LevelError)
	ZapLevelFatal  = zapcore.Level(LevelFatal)
	ZapLevelAll    = zapcore.Level(LevelAll)
)

Variables

This section is empty.

Functions

func NewFileWriter

func NewFileWriter(fns ...func(*FileConfig)) (*rotatefile.Writer, error)

func NewSlog

func NewSlog(
	w io.Writer,
	slogOpts *SlogHandlerOptions,
	fns ...util.WithFn[Options],
) (Logger[slog.Attr, slog.Level], LevelToggler)

NewSlog base on go std lib log/slog

func NewZapCoreWithFilter

func NewZapCoreWithFilter(
	enc zapcore.Encoder, ws zapcore.WriteSyncer, filter zap.LevelEnablerFunc,
) zapcore.Core

func NewZapJSONEncoder

func NewZapJSONEncoder() zapcore.Encoder

func SlogLevelEncoder

func SlogLevelEncoder(level slog.Level) slog.Value

func ZapLevelEncoder

func ZapLevelEncoder(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder)

Types

type Attr

type Attr interface {
	slog.Attr | zap.Field
}

type ChildLoggerOptions added in v0.1.0

type ChildLoggerOptions struct {
	// all support
	AddSource bool
	// all support
	SkipCaller int
	// only zap support
	StackTrace Level
}

type FileClearConfig

type FileClearConfig = rotatefile.CConfig

type FileConfig

type FileConfig = rotatefile.Config

type Level

type Level = uint8
const (
	LevelSilent Level = 0
	LevelTrace  Level = 1 << 0
	LevelDebug  Level = 1 << 1
	LevelInfo   Level = 1 << 2
	LevelWarn   Level = 1 << 3
	LevelError  Level = 1 << 4
	LevelFatal  Level = 1 << 5
	LevelAll    Level = LevelTrace | LevelDebug | LevelInfo | LevelWarn | LevelError | LevelFatal
)

type LevelEffect

type LevelEffect Level

func (LevelEffect) OnWrite

func (l LevelEffect) OnWrite(_ *zapcore.CheckedEntry, _ []zapcore.Field)

type LevelToggler

type LevelToggler func(Level)

func NewSlogHandler

func NewSlogHandler(w io.Writer, opts *SlogHandlerOptions) (slog.Handler, LevelToggler)

func NewZapCore

func NewZapCore(
	enc zapcore.Encoder, ws zapcore.WriteSyncer, l Level,
) (zapcore.Core, LevelToggler)

func NewZapLevelFilter

func NewZapLevelFilter(levelBitMask Level) (zap.LevelEnablerFunc, LevelToggler)

type LogLevel

type LogLevel[T constraints.Signed] struct {
	// contains filtered or unexported fields
}

LogLevel is a wrapper for dynamically changing log level.

type Logger

type Logger[T Attr, Level constraints.Signed] interface {
	LoggerFeature[T, Level]

	Log(ctx context.Context, level Level, msg string, attrs ...T)
	Trace(msg string, args ...T)
	Debug(msg string, args ...T)
	Info(msg string, args ...T)
	Warn(msg string, args ...T)
	Error(msg string, args ...T)
	Fatal(msg string, args ...T)
}

type LoggerFeature

type LoggerFeature[T Attr, Level constraints.Signed] interface {
	// Child
	// - [zh] 创建一个新的记录器,均附带指定的属性。
	// - [en] creates a new logger, all with the specified attributes.
	Child(...T) Logger[T, Level]
	Enabled(Level) bool
	// Named { "name": "name1.name2.name3" }
	// - [zh] 创建一个新的具有给定名称的记录器,连接到父级后。
	// - [en] creates a new logger with the given name, connected to the parent after.
	Named(name string) Logger[T, Level]
	// WithGroup { "level": "info", "name": { "key": "value" } }
	// - [zh] 创建子 logger, 之后的属性添加到 name 字段下。
	// - [en] create a child logger, the following attributes are added to the name field.
	WithGroup(name string) Logger[T, Level]
	// WithOptions
	// - [zh] 创建子 logger, 可独立修改 source 等信息, 部分支持
	// - [en] create a child logger, you can independently modify source and other information, partially supported
	WithOptions(*ChildLoggerOptions) Logger[T, Level]
	// Sync only for zap if you used
	// see [zap.Logger.Sync](https://pkg.go.dev/go.uber.org/zap#Logger.Sync)
	Sync() error
}

type Options

type Options struct {
	// AddSource is whether to add source code information to the log, key is 'src'.
	AddSource bool
	// SkipCaller is the number of stack frames to skip to find the caller.
	SkipCaller int
	// StackTrace is the level of enable stack trace to log.
	StackTrace slog.Level
}

type Slog

type Slog = Logger[slog.Attr, slog.Level]

func NewZapLog

func NewZapLog(core zapcore.Core, options ...zap.Option) Slog

type SlogHandler

type SlogHandler struct {
	Handler slog.Handler
	Level   *LogLevel[slog.Level]
}

func (*SlogHandler) Enabled

func (sh *SlogHandler) Enabled(_ context.Context, level slog.Level) bool

func (*SlogHandler) Handle

func (sh *SlogHandler) Handle(ctx context.Context, r slog.Record) error

func (*SlogHandler) WithAttrs

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

func (*SlogHandler) WithGroup

func (sh *SlogHandler) WithGroup(name string) slog.Handler

type SlogHandlerOptions

type SlogHandlerOptions struct {
	// ReplaceAttr see slog.HandlerOptions.ReplaceAttr
	ReplaceAttr func(groups []string, a slog.Attr) slog.Attr
	// Level see slog.HandlerOptions.Level
	Level slog.Leveler
}

type Writer

type Writer interface {
	io.Writer
}

func NewWriter

func NewWriter(writers ...io.Writer) Writer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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