zlog

package
v0.0.0-...-fcd3ca4 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2025 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const TimeFormatUnix = "\x01"

TimeFormatUnix defines a time format that makes time fields to be serialized as Unix timestamp integers.

View Source
const TimeFormatUnixMs = "\x02"

TimeFormatUnixMs defines a time format that makes time fields to be serialized as Unix timestamp integers in milliseconds.

View Source
const TimeFormatUnixWithMs = "\x03"

TimeFormatUnixWithMs defines a time format that makes time fields to be serialized as Unix timestamp floats.

Variables

View Source
var (
	ProductionLogger = Logger{
		Level:      InfoLevel,
		Caller:     0,
		TimeField:  "",
		TimeFormat: "",
		Writer:     IOWriter{os.Stdout},
	}

	DevelopLogger = Logger{
		Level:      DebugLevel,
		Caller:     1,
		TimeField:  "",
		TimeFormat: "",
		Writer: &ConsoleWriter{
			ColorOutput:    true,
			QuoteString:    true,
			EndWithMessage: true,
			Writer:         IOWriter{os.Stdout},
		},
	}
)
View Source
var ErrAsyncWriterFull = errors.New("async writer is full")

Functions

func Fastrandn

func Fastrandn(n uint32) uint32

Fastrandn returns a pseudorandom uint32 in [0,n).

func Goid

func Goid() int64

Goid returns the current goroutine id. It exactly matches goroutine id of the stack trace.

func IsTerminal

func IsTerminal(fd uintptr) bool

IsTerminal returns whether the given file descriptor is a terminal.

func Printf

func Printf(format string, v ...any)

Printf sends a zlog entry without extra field. Arguments are handled in the manner of fmt.Printf.

func SetDefaultLogger

func SetDefaultLogger(l *Logger)

func SlogNewJSONHandler

func SlogNewJSONHandler(writer io.Writer, options *slog.HandlerOptions) slog.Handler

SlogNewJSONHandler returns a drop-in replacement of slog.NewJSONHandler.

Types

type AsyncWriter

type AsyncWriter struct {
	// Writer specifies the writer of output.
	Writer Writer

	// ChannelSize is the size of the data channel, the default size is 1.
	ChannelSize uint

	// DiscardOnFull determines whether to discard new entry when the channel is full.
	DiscardOnFull bool

	// DisableWritev disables the writev syscall if the Writer is a FileWriter.
	DisableWritev bool
	// contains filtered or unexported fields
}

AsyncWriter is a Writer that writes asynchronously.

func (*AsyncWriter) Close

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

Close implements io.Closer, and closes the underlying Writer.

func (*AsyncWriter) Write

func (w *AsyncWriter) Write(p []byte) (n int, err error)

Write implements io.Writer.

func (*AsyncWriter) WriteEntry

func (w *AsyncWriter) WriteEntry(e *Entry) (int, error)

WriteEntry implements Writer.

type CategorizedLogger

type CategorizedLogger struct {
	Logger
	Category string
}

type ConsoleWriter

type ConsoleWriter struct {
	// ColorOutput determines if used colorized output.
	ColorOutput bool

	// QuoteString determines if quoting string values.
	QuoteString bool

	// EndWithMessage determines if output message in the end.
	EndWithMessage bool

	// Formatter specifies an optional text formatter for creating a customized output,
	// If it is set, ColorOutput, QuoteString and EndWithMessage will be ignore.
	Formatter func(w io.Writer, args *FormatterArgs) (n int, err error)

	// Writer is the output destination. using os.Stderr if empty.
	Writer io.Writer
}

ConsoleWriter parses the JSON input and writes it in a colorized, human-friendly format to Writer. IMPORTANT: Don't use ConsoleWriter on critical path of a high concurrency and low latency application.

Default output format:

{Time} {Level} {Goid} {Caller} > {Message} {Key}={Value} {Key}={Value}

Note: The performance of ConsoleWriter is not good enough, because it will parses JSON input into structured records, then output in a specific order. Roughly 2x faster than logrus.TextFormatter, 0.8x fast as zap.ConsoleEncoder, and 5x faster than zerolog.ConsoleWriter.

func (*ConsoleWriter) Close

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

Close implements io.Closer, will closes the underlying Writer if not empty.

func (*ConsoleWriter) WriteEntry

func (w *ConsoleWriter) WriteEntry(e *Entry) (int, error)

WriteEntry implements Writer.

type Context

type Context []byte

Context represents contextual fields.

type Entry

type Entry struct {
	Level Level
	// contains filtered or unexported fields
}

Entry represents a zlog entry. It is instanced by one of the level method of Logger and finalized by the Msg or Msgf method.

func Debug

func Debug() (e *Entry)

Debug starts a new message with debug level.

func Error

func Error() (e *Entry)

Error starts a new message with error level.

func Fatal

func Fatal() (e *Entry)

Fatal starts a new message with fatal level.

func Info

func Info() (e *Entry)

Info starts a new message with info level.

func NewContext

func NewContext(dst []byte) (e *Entry)

NewContext starts a new contextual entry.

func Panic

func Panic() (e *Entry)

Panic starts a new message with panic level.

func Stat

func Stat() (e *Entry)

Stat starts a new message with panic level.

func Trace

func Trace() (e *Entry)

Trace starts a new message with trace level.

func Warn

func Warn() (e *Entry)

Warn starts a new message with warning level.

func (*Entry) AnErr

func (e *Entry) AnErr(key string, err error) *Entry

AnErr adds the field key with serialized err to the zlog context.

func (*Entry) Any

func (e *Entry) Any(key string, value any) *Entry

Any adds the field key with f as an any value to the entry.

func (*Entry) Bool

func (e *Entry) Bool(key string, b bool) *Entry

Bool append the val as a bool to the entry.

func (*Entry) Bools

func (e *Entry) Bools(key string, b []bool) *Entry

Bools adds the field key with val as a []bool to the entry.

func (*Entry) Byte

func (e *Entry) Byte(key string, val byte) *Entry

Byte adds the field key with val as a byte to the entry.

func (*Entry) Bytes

func (e *Entry) Bytes(key string, val []byte) *Entry

Bytes adds the field key with val as a string to the entry.

func (*Entry) BytesOrNil

func (e *Entry) BytesOrNil(key string, val []byte) *Entry

BytesOrNil adds the field key with val as a string or nil to the entry.

func (*Entry) Caller

func (e *Entry) Caller(depth int) *Entry

Caller adds the file:line of the "caller" key. If depth is negative, adds the full /path/to/file:line of the "caller" key.

func (*Entry) Context

func (e *Entry) Context(ctx Context) *Entry

Context sends the contextual fields to entry.

func (*Entry) Dict

func (e *Entry) Dict(key string, ctx Context) *Entry

Dict sends the contextual fields with key to entry.

func (*Entry) Discard

func (e *Entry) Discard() *Entry

Discard disables the entry so Msg(f) won't print it.

func (*Entry) Dur

func (e *Entry) Dur(key string, d time.Duration) *Entry

Dur adds the field key with duration d to the entry.

func (*Entry) Durs

func (e *Entry) Durs(key string, d []time.Duration) *Entry

Durs adds the field key with val as a []time.Duration to the entry.

func (*Entry) EmbedObject

func (e *Entry) EmbedObject(obj ObjectMarshaler) *Entry

EmbedObject marshals and Embeds an object that implement the ObjectMarshaler interface.

func (*Entry) Enabled

func (e *Entry) Enabled() bool

Enabled return false if the entry is going to be filtered out by zlog level.

func (*Entry) Encode

func (e *Entry) Encode(key string, val []byte, enc interface {
	AppendEncode(dst, src []byte) []byte
}) *Entry

Encode encodes bytes using enc.AppendEncode to the entry.

func (*Entry) Err

func (e *Entry) Err(err error) *Entry

Err adds the field "error" with serialized err to the entry.

func (*Entry) Errs

func (e *Entry) Errs(key string, errs []error) *Entry

Errs adds the field key with errs as an array of serialized errors to the entry.

func (*Entry) Fields

func (e *Entry) Fields(fields Fields) *Entry

Fields is a helper function to use a map to set fields using type assertion.

func (*Entry) Float32

func (e *Entry) Float32(key string, f float32) *Entry

Float32 adds the field key with f as a float32 to the entry.

func (*Entry) Float64

func (e *Entry) Float64(key string, f float64) *Entry

Float64 adds the field key with f as a float64 to the entry.

func (*Entry) Floats32

func (e *Entry) Floats32(key string, f []float32) *Entry

Floats32 adds the field key with f as a []float32 to the entry.

func (*Entry) Floats64

func (e *Entry) Floats64(key string, f []float64) *Entry

Floats64 adds the field key with f as a []float64 to the entry.

func (*Entry) Func

func (e *Entry) Func(f func(e *Entry)) *Entry

Func allows an anonymous func to run only if the entry is enabled.

func (*Entry) GoStringer

func (e *Entry) GoStringer(key string, val fmt.GoStringer) *Entry

GoStringer adds the field key with val.GoStringer() to the entry.

func (*Entry) Hex

func (e *Entry) Hex(key string, val []byte) *Entry

Hex adds the field key with val as a hex string to the entry.

func (*Entry) IPAddr

func (e *Entry) IPAddr(key string, ip net.IP) *Entry

IPAddr adds IPv4 or IPv6 Address to the entry.

func (*Entry) IPPrefix

func (e *Entry) IPPrefix(key string, pfx net.IPNet) *Entry

IPPrefix adds IPv4 or IPv6 Prefix (address and mask) to the entry.

func (*Entry) Int

func (e *Entry) Int(key string, i int) *Entry

Int adds the field key with i as a int to the entry.

func (*Entry) Int16

func (e *Entry) Int16(key string, i int16) *Entry

Int16 adds the field key with i as a int16 to the entry.

func (*Entry) Int32

func (e *Entry) Int32(key string, i int32) *Entry

Int32 adds the field key with i as a int32 to the entry.

func (*Entry) Int64

func (e *Entry) Int64(key string, i int64) *Entry

Int64 adds the field key with i as a int64 to the entry.

func (*Entry) Int8

func (e *Entry) Int8(key string, i int8) *Entry

Int8 adds the field key with i as a int8 to the entry.

func (*Entry) Interface

func (e *Entry) Interface(key string, i any) *Entry

Interface adds the field key with i marshaled using reflection.

func (*Entry) Ints

func (e *Entry) Ints(key string, a []int) *Entry

Ints adds the field key with i as a []int to the entry.

func (*Entry) Ints16

func (e *Entry) Ints16(key string, a []int16) *Entry

Ints16 adds the field key with i as a []int16 to the entry.

func (*Entry) Ints32

func (e *Entry) Ints32(key string, a []int32) *Entry

Ints32 adds the field key with i as a []int32 to the entry.

func (*Entry) Ints64

func (e *Entry) Ints64(key string, a []int64) *Entry

Ints64 adds the field key with i as a []int64 to the entry.

func (*Entry) Ints8

func (e *Entry) Ints8(key string, a []int8) *Entry

Ints8 adds the field key with i as a []int8 to the entry.

func (*Entry) KeysAndValues

func (e *Entry) KeysAndValues(keysAndValues ...any) *Entry

KeysAndValues sends keysAndValues to Entry

func (*Entry) MACAddr

func (e *Entry) MACAddr(key string, ha net.HardwareAddr) *Entry

MACAddr adds MAC address to the entry.

func (*Entry) Msg

func (e *Entry) Msg(msg string)

Msg sends the entry with msg added as the message field if not empty.

func (*Entry) Msgf

func (e *Entry) Msgf(format string, v ...any)

Msgf sends the entry with formatted msg added as the message field if not empty.

func (*Entry) Msgs

func (e *Entry) Msgs(args ...any)

Msgs sends the entry with msgs added as the message field if not empty.

func (*Entry) NetIPAddr

func (e *Entry) NetIPAddr(key string, ip netip.Addr) *Entry

NetIPAddr adds IPv4 or IPv6 Address to the entry.

func (*Entry) NetIPAddrPort

func (e *Entry) NetIPAddrPort(key string, ipPort netip.AddrPort) *Entry

NetIPAddrPort adds IPv4 or IPv6 with Port Address to the entry.

func (*Entry) NetIPAddrs

func (e *Entry) NetIPAddrs(key string, ips []netip.Addr) *Entry

NetIPAddrs adds IPv4 or IPv6 Addresses to the entry.

func (*Entry) NetIPPrefix

func (e *Entry) NetIPPrefix(key string, pfx netip.Prefix) *Entry

NetIPPrefix adds IPv4 or IPv6 Prefix (address and mask) to the entry.

func (*Entry) Object

func (e *Entry) Object(key string, obj ObjectMarshaler) *Entry

Object marshals an object that implement the ObjectMarshaler interface.

func (*Entry) Objects

func (e *Entry) Objects(key string, objects any) *Entry

Objects marshals a slice of objects that implement the ObjectMarshaler interface.

func (*Entry) RawJSON

func (e *Entry) RawJSON(key string, b []byte) *Entry

RawJSON adds already encoded JSON to the zlog line under key.

func (*Entry) RawJSONStr

func (e *Entry) RawJSONStr(key string, s string) *Entry

RawJSONStr adds already encoded JSON String to the zlog line under key.

func (*Entry) Stack

func (e *Entry) Stack() *Entry

Stack enables stack trace printing for the error passed to Err().

func (*Entry) Str

func (e *Entry) Str(key string, val string) *Entry

Str adds the field key with val as a string to the entry.

func (*Entry) StrInt

func (e *Entry) StrInt(key string, val int64) *Entry

StrInt adds the field key with integer val as a string to the entry.

func (*Entry) Stringer

func (e *Entry) Stringer(key string, val fmt.Stringer) *Entry

Stringer adds the field key with val.String() to the entry.

func (*Entry) Strs

func (e *Entry) Strs(key string, vals []string) *Entry

Strs adds the field key with vals as a []string to the entry.

func (*Entry) Time

func (e *Entry) Time(key string, t time.Time) *Entry

Time append t formated as string using time.RFC3339Nano.

func (*Entry) TimeDiff

func (e *Entry) TimeDiff(key string, t time.Time, start time.Time) *Entry

TimeDiff adds the field key with positive duration between time t and start. If time t is not greater than start, duration will be 0. Duration format follows the same principle as Dur().

func (*Entry) TimeFormat

func (e *Entry) TimeFormat(key string, timefmt string, t time.Time) *Entry

TimeFormat append t formated as string using timefmt.

func (*Entry) Times

func (e *Entry) Times(key string, a []time.Time) *Entry

Times append a formated as string array using time.RFC3339Nano.

func (*Entry) TimesFormat

func (e *Entry) TimesFormat(key string, timefmt string, a []time.Time) *Entry

TimesFormat append a formated as string array using timefmt.

func (*Entry) Type

func (e *Entry) Type(key string, v any) *Entry

Type adds type of the key using reflection to the entry.

func (*Entry) Uint

func (e *Entry) Uint(key string, i uint) *Entry

Uint adds the field key with i as a uint to the entry.

func (*Entry) Uint16

func (e *Entry) Uint16(key string, i uint16) *Entry

Uint16 adds the field key with i as a uint16 to the entry.

func (*Entry) Uint32

func (e *Entry) Uint32(key string, i uint32) *Entry

Uint32 adds the field key with i as a uint32 to the entry.

func (*Entry) Uint64

func (e *Entry) Uint64(key string, i uint64) *Entry

Uint64 adds the field key with i as a uint64 to the entry.

func (*Entry) Uint8

func (e *Entry) Uint8(key string, i uint8) *Entry

Uint8 adds the field key with i as a uint8 to the entry.

func (*Entry) Uints

func (e *Entry) Uints(key string, a []uint) *Entry

Uints adds the field key with i as a []uint to the entry.

func (*Entry) Uints16

func (e *Entry) Uints16(key string, a []uint16) *Entry

Uints16 adds the field key with i as a []uint16 to the entry.

func (*Entry) Uints32

func (e *Entry) Uints32(key string, a []uint32) *Entry

Uints32 adds the field key with i as a []uint32 to the entry.

func (*Entry) Uints64

func (e *Entry) Uints64(key string, a []uint64) *Entry

Uints64 adds the field key with i as a []uint64 to the entry.

func (*Entry) Uints8

func (e *Entry) Uints8(key string, a []uint8) *Entry

Uints8 adds the field key with i as a []uint8 to the entry.

func (*Entry) Value

func (e *Entry) Value() Context

Value builds the contextual fields.

type Fields

type Fields map[string]any

Fields type, used to pass to `Fields`.

type FileWriter

type FileWriter struct {
	// Filename is the file to write logs to.  Backup zlog files will be retained
	// in the same directory.
	Filename string

	// MaxSize is the maximum size in bytes of the zlog file before it gets rotated.
	MaxSize int64

	// MaxBackups is the maximum number of old zlog files to retain.  The default
	// is to retain all old zlog files
	MaxBackups int

	// FileMode represents the file's mode and permission bits.  The default
	// mode is 0644
	FileMode os.FileMode

	// TimeFormat specifies the time format of filename, uses `2006-01-02T15-04-05` as default format.
	// If set with `TimeFormatUnix`, `TimeFormatUnixMs`, times are formated as UNIX timestamp.
	TimeFormat string

	// LocalTime determines if the time used for formatting the timestamps in
	// zlog files is the computer's local time.  The default is to use UTC time.
	LocalTime bool

	// HostName determines if the hostname used for formatting in zlog files.
	HostName bool

	// ProcessID determines if the pid used for formatting in zlog files.
	ProcessID bool

	// EnsureFolder ensures the file directory creation before writing.
	EnsureFolder bool

	// Header specifies an optional header function of zlog file after rotation,
	Header func(fileinfo os.FileInfo) []byte

	// Cleaner specifies an optional cleanup function of zlog backups after rotation,
	// if not set, the default behavior is to delete more than MaxBackups zlog files.
	Cleaner func(filename string, maxBackups int, matches []os.FileInfo)
	// contains filtered or unexported fields
}

FileWriter is an Writer that writes to the specified filename.

Backups use the zlog file name given to FileWriter, in the form `name.timestamp.ext` where name is the filename without the extension, timestamp is the time at which the zlog was rotated formatted with the time.Time format of `2006-01-02T15-04-05` and the extension is the original extension. For example, if your FileWriter.Filename is `/var/zlog/foo/server.zlog`, a backup created at 6:30pm on Nov 11 2016 would use the filename `/var/zlog/foo/server.2016-11-04T18-30-00.zlog`

Cleaning Up Old Log Files

Whenever a new logfile gets created, old zlog files may be deleted. The most recent files according to filesystem modified time will be retained, up to a number equal to MaxBackups (or all of them if MaxBackups is 0). Note that the time encoded in the timestamp is the rotation time, which may differ from the last time that file was written to.

func (*FileWriter) Close

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

Close implements io.Closer, and closes the current logfile.

func (*FileWriter) Rotate

func (w *FileWriter) Rotate() (err error)

Rotate causes Logger to close the existing zlog file and immediately create a new one. This is a helper function for applications that want to initiate rotations outside of the normal rotation rules, such as in response to SIGHUP. After rotating, this initiates compression and removal of old zlog files according to the configuration.

func (*FileWriter) Write

func (w *FileWriter) Write(p []byte) (n int, err error)

Write implements io.Writer. If a write would cause the zlog file to be larger than MaxSize, the file is closed, rotate to include a timestamp of the current time, and update symlink with zlog name file to the new file.

func (*FileWriter) WriteEntry

func (w *FileWriter) WriteEntry(e *Entry) (n int, err error)

WriteEntry implements Writer. If a write would cause the zlog file to be larger than MaxSize, the file is closed, rotate to include a timestamp of the current time, and update symlink with zlog name file to the new file.

func (*FileWriter) WriteV

func (w *FileWriter) WriteV(iovs []syscall.Iovec) (n uintptr, err error)

type FormatterArgs

type FormatterArgs struct {
	Time       string // "2019-07-10T05:35:54.277Z"
	Level      string // "info"
	Caller     string // "prog.go:42"
	CallerFunc string // "main.main"
	Goid       string // "123"
	Stack      string // "<stack string>"
	Message    string // "a structure message"
	KeyValues  []struct {
		Key       string // "foo"
		Value     string // "bar"
		ValueType byte   // 's'
	}
}

FormatterArgs is a parsed struct from json input

func (*FormatterArgs) Get

func (args *FormatterArgs) Get(key string) (value string)

Get gets the value associated with the given key.

type IOWriteCloser

type IOWriteCloser struct {
	io.WriteCloser
}

IOWriteCloser wraps an io.IOWriteCloser to Writer.

func (IOWriteCloser) Close

func (w IOWriteCloser) Close() (err error)

Close implements Writer.

func (IOWriteCloser) WriteEntry

func (w IOWriteCloser) WriteEntry(e *Entry) (n int, err error)

WriteEntry implements Writer.

type IOWriter

type IOWriter struct {
	io.Writer
}

IOWriter wraps an io.Writer to Writer.

func (IOWriter) WriteEntry

func (w IOWriter) WriteEntry(e *Entry) (n int, err error)

WriteEntry implements Writer.

type JournalWriter

type JournalWriter struct {
	// JournalSocket specifies socket name, using `/run/systemd/journal/socket` if empty.
	JournalSocket string
	// contains filtered or unexported fields
}

JournalWriter is an Writer that writes logs to journald.

func (*JournalWriter) Close

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

Close implements io.Closer.

func (*JournalWriter) WriteEntry

func (w *JournalWriter) WriteEntry(e *Entry) (n int, err error)

WriteEntry implements Writer.

type Level

type Level uint32

Level defines zlog levels.

const (
	// DebugLevel defines debug zlog level.
	DebugLevel Level = 1
	// TraceLevel defines trace zlog level.
	TraceLevel Level = 2
	// InfoLevel defines info zlog level.
	InfoLevel Level = 3
	// WarnLevel defines warn zlog level.
	WarnLevel Level = 4
	// ErrorLevel defines error zlog level.
	ErrorLevel Level = 5
	// FatalLevel defines fatal zlog level.
	FatalLevel Level = 6
	// PanicLevel defines panic zlog level.
	PanicLevel Level = 7
)

func ParseLevel

func ParseLevel(s string) (level Level)

ParseLevel converts a level string into a zlog Level value.

func (Level) String

func (l Level) String() (s string)

String return lowe case string of Level

type LogfmtFormatter

type LogfmtFormatter struct {
	TimeField string
}

func (LogfmtFormatter) Formatter

func (f LogfmtFormatter) Formatter(out io.Writer, args *FormatterArgs) (n int, err error)

type Logger

type Logger struct {
	// Level defines zlog levels.
	Level Level

	// Caller determines if adds the file:line of the "caller" key.
	// If Caller is negative, adds the full /path/to/file:line of the "caller" key.
	Caller int

	// TimeField defines the time field name in output.  It uses "time" in if empty.
	TimeField string

	// TimeFormat specifies the time format in output. It uses time.RFC3339 with milliseconds if empty.
	// Strongly recommended to leave TimeFormat empty for optimal built-in zlog formatting performance.
	// If set with `TimeFormatUnix`, `TimeFormatUnixMs`, times are formated as UNIX timestamp.
	TimeFormat string

	// TimeLocation specifics that the location which TimeFormat used. It uses time.Local if empty.
	TimeLocation *time.Location

	// Context specifies an optional context of zlog.
	Context Context

	// Writer specifies the writer of output. It uses a wrapped os.Stderr Writer in if empty.
	Writer Writer
}

A Logger represents an active logging object that generates lines of JSON output to an io.Writer.

func GetDefaultLogger

func GetDefaultLogger() *Logger

func (*Logger) Categorized

func (l *Logger) Categorized(name string) *CategorizedLogger

Categorized returns a cloned zlog for category `name`.

func (*Logger) Debug

func (l *Logger) Debug() (e *Entry)

Debug starts a new message with debug level.

func (*Logger) Err

func (l *Logger) Err(err error) (e *Entry)

Err starts a new message with error level with err as a field if not nil or with info level if err is nil.

func (*Logger) Error

func (l *Logger) Error() (e *Entry)

Error starts a new message with error level.

func (*Logger) Fatal

func (l *Logger) Fatal() (e *Entry)

Fatal starts a new message with fatal level.

func (*Logger) Info

func (l *Logger) Info() (e *Entry)

Info starts a new message with info level.

func (*Logger) Log

func (l *Logger) Log() (e *Entry)

Log starts a new message with no level.

func (*Logger) Panic

func (l *Logger) Panic() (e *Entry)

Panic starts a new message with panic level.

func (*Logger) Printf

func (l *Logger) Printf(format string, v ...any)

Printf sends a zlog entry without extra field. Arguments are handled in the manner of fmt.Printf.

func (*Logger) SetLevel

func (l *Logger) SetLevel(level Level)

SetLevel changes zlog default level.

func (*Logger) Slog

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

Slog wraps the Logger to provide *slog.Logger

func (*Logger) Std

func (l *Logger) Std(prefix string, flag int) *stdLog.Logger

Std wraps the Logger to provide *stdLog.Logger

func (*Logger) Trace

func (l *Logger) Trace() (e *Entry)

Trace starts a new message with trace level.

func (*Logger) Warn

func (l *Logger) Warn() (e *Entry)

Warn starts a new message with warning level.

func (*Logger) WithCaller

func (l *Logger) WithCaller(depth int) *Logger

WithCaller returns a new Logger instance with caller information enabled. The depth parameter specifies how many stack frames to skip (0 = current function, 1 = caller, etc.).

func (*Logger) WithLevel

func (l *Logger) WithLevel(level Level) (e *Entry)

WithLevel starts a new message with level.

func (*Logger) WithName

func (l *Logger) WithName(name string) *Logger

WithName returns a new Logger instance with the specified name field added to the context.

func (*Logger) WithValues

func (l *Logger) WithValues(keysAndValues ...any) *Logger

WithValues returns a new Logger instance with the specified key-value pairs added to the context.

type MultiEntryWriter

type MultiEntryWriter []Writer

MultiEntryWriter is an array Writer that zlog to different writers

func (*MultiEntryWriter) Close

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

Close implements io.Closer, and closes the underlying MultiEntryWriter.

func (*MultiEntryWriter) WriteEntry

func (w *MultiEntryWriter) WriteEntry(e *Entry) (n int, err error)

WriteEntry implements entryWriter.

type MultiIOWriter

type MultiIOWriter []io.Writer

MultiIOWriter is an array io.Writer that zlog to different writers

func (*MultiIOWriter) Close

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

Close implements io.Closer, and closes the underlying MultiIOWriter.

func (*MultiIOWriter) WriteEntry

func (w *MultiIOWriter) WriteEntry(e *Entry) (n int, err error)

WriteEntry implements entryWriter.

type MultiLevelWriter

type MultiLevelWriter struct {
	// InfoWriter specifies all the level logs writes to
	InfoWriter Writer

	// WarnWriter specifies the level greater than or equal to WarnLevel writes to
	WarnWriter Writer

	// WarnWriter specifies the level greater than or equal to ErrorLevel writes to
	ErrorWriter Writer

	// ConsoleWriter specifies the console writer
	ConsoleWriter Writer

	// ConsoleLevel specifies the level greater than or equal to it also writes to
	ConsoleLevel Level
}

MultiLevelWriter is an Writer that zlog to different writers by different levels

func (*MultiLevelWriter) Close

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

Close implements io.Closer, and closes the underlying LeveledWriter.

func (*MultiLevelWriter) WriteEntry

func (w *MultiLevelWriter) WriteEntry(e *Entry) (n int, err error)

WriteEntry implements entryWriter.

type MultiWriter

type MultiWriter = MultiLevelWriter

MultiWriter is an alias for MultiLevelWriter

type ObjectMarshaler

type ObjectMarshaler interface {
	MarshalObject(e *Entry)
}

ObjectMarshaler provides a strongly-typed and encoding-agnostic interface to be implemented by types used with Entry's Object methods.

type SyslogWriter

type SyslogWriter struct {
	// Network specifies network of the syslog server
	Network string

	// Address specifies address of the syslog server
	Address string

	// Hostname specifies hostname of the syslog message
	Hostname string

	// Tag specifies tag of the syslog message
	Tag string

	// Marker specifies prefix of the syslog message, e.g. `@cee:`
	Marker string

	// Dial specifies the dial function for creating TCP/TLS connections.
	Dial func(network, addr string) (net.Conn, error)
	// contains filtered or unexported fields
}

SyslogWriter is an Writer that writes logs to a syslog server..

func (*SyslogWriter) Close

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

Close closes a connection to the syslog server.

func (*SyslogWriter) WriteEntry

func (w *SyslogWriter) WriteEntry(e *Entry) (n int, err error)

WriteEntry implements Writer, sends logs with priority to the syslog server.

type Writer

type Writer interface {
	WriteEntry(*Entry) (int, error)
}

Writer defines an entry writer interface.

type WriterFunc

type WriterFunc func(*Entry) (int, error)

The WriterFunc type is an adapter to allow the use of ordinary functions as zlog writers. If f is a function with the appropriate signature, WriterFunc(f) is a Writer that calls f.

func (WriterFunc) WriteEntry

func (f WriterFunc) WriteEntry(e *Entry) (int, error)

WriteEntry calls f(e).

Jump to

Keyboard shortcuts

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