zlog

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithValue added in v0.2.0

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

ContextWithValue 基于父context,创建一个带 trace_id 的上下文,受父context生命周期影响,非必要不使用

func Debug

func Debug() *zerolog.Event

func Error

func Error() *zerolog.Event

func Info

func Info() *zerolog.Event

func NewLogLogger

func NewLogLogger(mode string, level string, options ...Option)

func NewTraceContext added in v0.2.0

func NewTraceContext(ctx context.Context) context.Context

NewTraceContext 创建一个新的trace context

func NewTraceID added in v0.4.0

func NewTraceID() string

NewTraceID 生成一个新的 trace_id

func TraceIDFromContext added in v0.5.0

func TraceIDFromContext(ctx context.Context) string

TraceIDFromContext 从上下文中获取 trace_id

func Warn

func Warn() *zerolog.Event

Types

type Level

type Level int

A Level is the importance or severity of a log event. The higher the level, the more important or severe the event.

const (
	LevelDebug Level = -4
	LevelInfo  Level = 0
	LevelWarn  Level = 4
	LevelError Level = 8
)

Names for common levels.

Level numbers are inherently arbitrary, but we picked them to satisfy three constraints. Any system can map them to another numbering scheme if it wishes.

First, we wanted the default level to be Info, Since Levels are ints, Info is the default value for int, zero.

Second, we wanted to make it easy to use levels to specify logger verbosity. Since a larger level means a more severe event, a logger that accepts events with smaller (or more negative) level means a more verbose logger. Logger verbosity is thus the negation of event severity, and the default verbosity of 0 accepts all events at least as severe as INFO.

Third, we wanted some room between levels to accommodate schemes with named levels between ours. For example, Google Cloud Logging defines a Notice level between Info and Warn. Since there are only a few of these intermediate levels, the gap between the numbers need not be large. Our gap of 4 matches OpenTelemetry's mapping. Subtracting 9 from an OpenTelemetry level in the DEBUG, INFO, WARN and ERROR ranges converts it to the corresponding slog Level range. OpenTelemetry also has the names TRACE and FATAL, which slog does not. But those OpenTelemetry levels can still be represented as slog Levels by using the appropriate integers.

func (Level) Level

func (l Level) Level() Level

Level returns the receiver. It implements Leveler.

func (Level) MarshalJSON

func (l Level) MarshalJSON() ([]byte, error)

MarshalJSON implements encoding/json.Marshaler by quoting the output of Level.String.

func (Level) MarshalText

func (l Level) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler by calling Level.String.

func (Level) String

func (l Level) String() string

String returns a name for the level. If the level has a name, then that name in uppercase is returned. If the level is between named values, then an integer is appended to the uppercased name. Examples:

LevelWarn.String() => "WARN"
(LevelInfo+2).String() => "INFO+2"

func (*Level) UnmarshalJSON

func (l *Level) UnmarshalJSON(data []byte) error

UnmarshalJSON implements encoding/json.Unmarshaler It accepts any string produced by Level.MarshalJSON, ignoring case. It also accepts numeric offsets that would result in a different string on output. For example, "Error-8" would marshal as "INFO".

func (*Level) UnmarshalText

func (l *Level) UnmarshalText(data []byte) error

UnmarshalText implements encoding.TextUnmarshaler. It accepts any string produced by Level.MarshalText, ignoring case. It also accepts numeric offsets that would result in a different string on output. For example, "Error-8" would marshal as "INFO".

type LevelVar

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

A LevelVar is a Level variable, to allow a [Handler] level to change dynamically. It implements Leveler as well as a Set method, and it is safe for use by multiple goroutines. The zero LevelVar corresponds to LevelInfo.

func (*LevelVar) Level

func (v *LevelVar) Level() Level

Level returns v's level.

func (*LevelVar) MarshalText

func (v *LevelVar) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler by calling Level.MarshalText.

func (*LevelVar) Set

func (v *LevelVar) Set(l Level)

Set sets v's level to l.

func (*LevelVar) String

func (v *LevelVar) String() string

func (*LevelVar) UnmarshalText

func (v *LevelVar) UnmarshalText(data []byte) error

UnmarshalText implements encoding.TextUnmarshaler by calling Level.UnmarshalText.

type Leveler

type Leveler interface {
	Level() Level
}

A Leveler provides a Level value.

As Level itself implements Leveler, clients typically supply a Level value wherever a Leveler is needed, such as in [HandlerOptions]. Clients who need to vary the level dynamically can provide a more complex Leveler implementation such as *LevelVar.

type LogMode

type LogMode int
const (
	STDOUT LogMode = iota
	FILE
	NATS
)

type Logger

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

type Option

type Option func(*Options)

func FileAttr

func FileAttr(name string, maxSize int, maxAge int, compress bool) Option

FileAttr 使用文件输出日志的配置

func NATSAttr

func NATSAttr(conn *nats.Conn, subject string) Option

NATSAttr 使用NATS输出日志的配置

type Options

type Options struct {
	Mode  LogMode
	Level Level

	FileWriterOption zwriter.FileWriterOption
	NATSWriterOption zwriter.NATSWriterOption
}

type TraceHook

type TraceHook struct{}

TraceHook 是一个自定义 Hook,用于为每个日志条目添加 trace_id

func (*TraceHook) Run

func (h *TraceHook) Run(e *zerolog.Event, _ zerolog.Level, _ string)

Run 实现 zerolog.Hook 接口,允许修改日志条目

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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