xlog

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: Apache-2.0 Imports: 39 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LevelDebug    = zerolog.DebugLevel
	LevelInfo     = zerolog.InfoLevel
	LevelWarn     = zerolog.WarnLevel
	LevelError    = zerolog.ErrorLevel
	LevelFatal    = zerolog.FatalLevel
	LevelPanic    = zerolog.PanicLevel
	LevelNone     = zerolog.NoLevel
	LevelSuppress = zerolog.Disabled
	LevelTrace    = zerolog.TraceLevel
)
View Source
const (
	LogRetentionDays = 28
	LogMaxSizeMB     = 16
	LogCompress      = false
)
View Source
const (
	DomainFieldName = "dom"
)

XLog uses the caller field in zerolog to record what we call "domains" which are lazily registrable pub/sub queue paths that uniquely identify what it is that the logger is currently logging about. E.g. pmesh myfrontend myfrontend.pid

Variables

View Source
var ErrQuotaExceeded = errors.New("quota exceeded")

Functions

func FileWriter

func FileWriter(name string) (wc io.WriteCloser)

func NewStackError

func NewStackError(msg string) error

func NewStackErrorf added in v0.2.2

func NewStackErrorf(fmt string, args ...any) error

func Print

func Print(v ...any)

Print sends a log event using debug level and no extra field. Arguments are handled in the manner of fmt.Print.

func PrintC

func PrintC(ctx context.Context, v ...any)

func Printf

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

Printf sends a log event using debug level and no extra field. Arguments are handled in the manner of fmt.Printf.

func PrintfC

func PrintfC(ctx context.Context, format string, v ...any)

func RegisterCollector

func RegisterCollector(c Collector)

func RemoveCollector

func RemoveCollector(c Collector)

func SetDefaultOutput

func SetDefaultOutput(w ...io.Writer)

Not safe for concurrent use.

func SetLoggerLevel

func SetLoggerLevel(level Level)

SetLoggerLevel sets the global logger level.

func Tail

func Tail(opts TailOptions, out io.Writer) error

func TailContext

func TailContext(ctx context.Context, opts TailOptions, out io.Writer) error

func TailHistory

func TailHistory(ctx context.Context, opts *TailOptions, out io.Writer) error

func ToSlog

func ToSlog(logger *Logger) *slog.Logger

Creates a new slog.Logger that writes to the logger.

func WrapStackError

func WrapStackError(err error) error

Types

type Collector

type Collector interface {
	Write(p []byte, level Level, domain string)
}

Collector is an interface for collecting log messages in real-time.

type Context

type Context = zerolog.Context

func With

func With() Context

With creates a child logger with the field added to its context.

type DefaultWriter

type DefaultWriter struct{}

func (DefaultWriter) Write

func (DefaultWriter) Write(p []byte) (n int, err error)

type Domain

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

Domain represents a domain.

func ContextDomain

func ContextDomain(c context.Context) *Domain

ContextDomain returns the domain from the context.

func (*Domain) Run

func (d *Domain) Run(e *Event, level Level, msg string)

Implement zerolog.Hook

func (*Domain) String

func (d *Domain) String() string

func (*Domain) Write

func (d *Domain) Write(p []byte) (n int, err error)

Implement zerolog.MultilevelWriter

func (*Domain) WriteLevel

func (d *Domain) WriteLevel(l Level, p []byte) (n int, err error)

type DomainFilter

type DomainFilter string

DomainFilter filters logs by domain

func (DomainFilter) Test

func (d DomainFilter) Test(line Line, flags StreamFlag) (bool, bool)

func (DomainFilter) TestFile

func (d DomainFilter) TestFile(fi *FileInfo) bool

func (DomainFilter) TestRaw

func (d DomainFilter) TestRaw(line string) bool

type Event

type Event = zerolog.Event

func Debug

func Debug() *Event

Debug starts a new message with debug level.

You must call Msg on the returned event in order to send the event.

func DebugC

func DebugC(ctx context.Context) *Event

func Err

func Err(err error) *Event

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

You must call Msg on the returned event in order to send the event.

func ErrC

func ErrC(ctx context.Context, err error) *Event

func ErrStack

func ErrStack(err any) *Event

ErrStack starts a new message with error level with err as a field if not nil or with info level if err is nil. The stack trace is attached to the event.

You must call Msg on the returned event in order to send the event.

func ErrStackC

func ErrStackC(ctx context.Context, err any) *Event

func Error

func Error() *Event

Error starts a new message with error level.

You must call Msg on the returned event in order to send the event.

func ErrorC

func ErrorC(ctx context.Context) *Event

func Fatal

func Fatal() *Event

Fatal starts a new message with fatal level. The os.Exit(1) function is called by the Msg method.

You must call Msg on the returned event in order to send the event.

func FatalC

func FatalC(ctx context.Context) *Event

func Info

func Info() *Event

Info starts a new message with info level.

You must call Msg on the returned event in order to send the event.

func InfoC

func InfoC(ctx context.Context) *Event

func Log

func Log() *Event

Log starts a new message with no level. Setting GlobalLevel to Disabled will still disable events produced by this method.

You must call Msg on the returned event in order to send the event.

func LogC

func LogC(ctx context.Context) *Event

func Panic

func Panic() *Event

Panic starts a new message with panic level. The message is also sent to the panic function.

You must call Msg on the returned event in order to send the event.

func PanicC

func PanicC(ctx context.Context) *Event

func Trace

func Trace() *Event

Trace starts a new message with trace level.

You must call Msg on the returned event in order to send the event.

func TraceC

func TraceC(ctx context.Context) *Event

func Warn

func Warn() *Event

Warn starts a new message with warn level.

You must call Msg on the returned event in order to send the event.

func WarnC

func WarnC(ctx context.Context) *Event

func WithLevel

func WithLevel(level Level) *Event

WithLevel starts a new message with level.

You must call Msg on the returned event in order to send the event.

func WithLevelC

func WithLevelC(ctx context.Context, level Level) *Event

type EventEnhancer

type EventEnhancer interface {
	MarshalZerologObject(e *Event)
}

func EnhanceRequest

func EnhanceRequest(r *http.Request) EventEnhancer

type FileInfo

type FileInfo struct {
	File    fs.DirEntry // File info
	Name    string      // Name of the log file
	LastUse time.Time   // Upper bound (if inactive or compressed)
	Kind    FileKind    // Kind of log file
}

func ReadDir

func ReadDir() ([]FileInfo, error)

ReadDir reads the log directory and returns a list of log files.

type FileKind

type FileKind uint8
const (
	ActiveLog     FileKind = iota // Active log     | hi.log
	InactiveLog                   // Inactive log   | hi-2024-02-29T00-00-23.275.log
	CompressedLog                 // Compressed log | hi-2024-02-29T00-22-01.211.log.gz
)

type Filter

type Filter interface {
	// TestRaw tests a raw log line for being a candidate for parsing
	// This is used to skip parsing lines that are known to be irrelevant
	TestRaw(string) (include bool)
	// Test tests a parsed log line for inclusion
	Test(line Line, flags StreamFlag) (include bool, stop bool)
	// Tests file info for inclusion
	TestFile(*FileInfo) (include bool)
}

type Hook

type Hook = zerolog.Hook

type Level

type Level = zerolog.Level

type LevelFilter

type LevelFilter Level

LevelFilter filters logs by minimum level

func (LevelFilter) Test

func (f LevelFilter) Test(line Line, flags StreamFlag) (bool, bool)

func (LevelFilter) TestFile

func (f LevelFilter) TestFile(*FileInfo) bool

func (LevelFilter) TestRaw

func (f LevelFilter) TestRaw(line string) bool

type LevelWriter

type LevelWriter = zerolog.LevelWriter

func NewConsoleWriter

func NewConsoleWriter(f io.Writer) LevelWriter

func StderrWriter

func StderrWriter() LevelWriter

func StdoutWriter

func StdoutWriter() LevelWriter

type Line

type Line struct {
	*fastjson.Value
	Raw []byte
}

func ParseLine

func ParseLine(line []byte) (ll Line, err error)

func (Line) Domain

func (ll Line) Domain() string

func (Line) Level

func (ll Line) Level() Level

func (Line) Time

func (ll Line) Time() time.Time

type Logger

type Logger = zerolog.Logger

func Ctx

func Ctx(ctx context.Context) *Logger

Ctx returns the Logger associated with the ctx. If no logger is associated, a disabled logger is returned.

func Default

func Default() *Logger

func NewDomain

func NewDomain(name string, w ...io.Writer) (l *Logger)

NewDomain creates a new domain and a logger.

func Output

func Output(w io.Writer) Logger

Output duplicates the global logger and sets w as its output.

type MultiFilter

type MultiFilter []Filter

MultiFilter is a collection of filters

func (MultiFilter) Append

func (f MultiFilter) Append(list ...Filter) MultiFilter

func (MultiFilter) As

func (f MultiFilter) As(out any) bool

func (MultiFilter) Test

func (f MultiFilter) Test(line Line, flags StreamFlag) (bool, bool)

func (MultiFilter) TestFile

func (f MultiFilter) TestFile(fi *FileInfo) bool

func (MultiFilter) TestRaw

func (f MultiFilter) TestRaw(line string) bool

type MuxWriter

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

Mux writer is a writer that buffers lines and writes them in order of their timestamps. It is used to write logs from multiple sources to a single output, while preserving the order of the logs. Additionally, it inserts sending host information into the caller data.

func ToMuxWriter

func ToMuxWriter(out io.Writer) *MuxWriter

func (*MuxWriter) Flush

func (o *MuxWriter) Flush()

func (*MuxWriter) SubWriter

func (o *MuxWriter) SubWriter(host string) io.Writer

func (*MuxWriter) Write

func (o *MuxWriter) Write(p []byte) (n int, err error)

func (*MuxWriter) WriteAs

func (o *MuxWriter) WriteAs(p []byte, host string) (n int, err error)

type Parser

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

func NewFileParser

func NewFileParser(path string, fl StreamFlag) (*Parser, error)

NewFileParser creates a new parser for the file at path. The file is opened in read-only mode. If the file is a gzip file, it is automatically decompressed. In which case, the returned parser will always be a head parser.

func NewParser

func NewParser(r io.ReadCloser, fl StreamFlag) (p *Parser, err error)

func (*Parser) Close

func (r *Parser) Close() error

func (*Parser) Filters

func (r *Parser) Filters() []Filter

func (*Parser) Flags

func (r *Parser) Flags() StreamFlag

func (*Parser) IsTail

func (r *Parser) IsTail() bool

func (*Parser) Next

func (r *Parser) Next() (v Line, err error)

func (*Parser) NextContext

func (r *Parser) NextContext(ctx context.Context) (v Line, err error)

func (*Parser) WithFilter

func (r *Parser) WithFilter(f ...Filter) *Parser

func (*Parser) WithQuota

func (r *Parser) WithQuota(q *ParserQuota) *Parser

type ParserQuota

type ParserQuota struct {
	Io    atomic.Int64
	Lines atomic.Int64
}

func NewParserQuota

func NewParserQuota(maxIo, maxLines int64) (q *ParserQuota)

func (*ParserQuota) ConsumeIo

func (r *ParserQuota) ConsumeIo(n int) bool

func (*ParserQuota) ConsumeLine

func (r *ParserQuota) ConsumeLine() bool

type Sampler

type Sampler = zerolog.Sampler

type SearchFilter

type SearchFilter string

SearchFilter filters logs by substring

func (SearchFilter) Test

func (f SearchFilter) Test(line Line, flags StreamFlag) (bool, bool)

func (SearchFilter) TestFile

func (f SearchFilter) TestFile(*FileInfo) bool

func (SearchFilter) TestRaw

func (f SearchFilter) TestRaw(line string) bool

type StreamFlag

type StreamFlag int
const (
	StreamTail     StreamFlag = 1 << 0
	StreamHead     StreamFlag = 1 << 1
	StreamGzip     StreamFlag = 1 << 2
	StreamBufferOk StreamFlag = 1 << 3 // Buffer is ok to use if we can't seek
)

type TailOptions

type TailOptions struct {
	Domain    string    `json:"domain,omitempty"`     // Domain to filter logs in
	MinLevel  Level     `json:"level,omitempty"`      // Level filter
	After     time.Time `json:"after,omitempty"`      // Time filter
	Before    time.Time `json:"before,omitempty"`     // Time filter
	Search    string    `json:"substr,omitempty"`     // Substring filter
	LineLimit int64     `json:"line_limit,omitempty"` // Max lines to emit from history
	IoLimit   int64     `json:"io_limit,omitempty"`   // Max bytes to read in history
	Follow    bool      `json:"follow,omitempty"`     // Follow logs in real time
	Hostname  string    `json:"hostname,omitempty"`   // Hostname to filter logs in (in public format). Not used in local implementation
	Viral     bool      `json:"viral,omitempty"`      // Viral logs, will spread to all nodes. Not used in local implementation
}

func (TailOptions) Filter

func (o TailOptions) Filter() Filter

func (TailOptions) WithRay

func (o TailOptions) WithRay(rayStr string) (opts TailOptions, err error)

type TextAdapter

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

func ToTextWriter

func ToTextWriter(logger *Logger, level Level) (w io.Writer, te *TextAdapter)

Creates a new writer that writes valid JSON objects to the logger.

func (*TextAdapter) Flush

func (w *TextAdapter) Flush() error

func (*TextAdapter) Write

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

func (*TextAdapter) WriteLevel

func (w *TextAdapter) WriteLevel(lv Level, p []byte) (n int, err error)

type TimeFilter

type TimeFilter struct {
	After, Before time.Time
}

TimeFilter filters logs by time

func (TimeFilter) Test

func (f TimeFilter) Test(line Line, flags StreamFlag) (ok bool, stop bool)

func (TimeFilter) TestFile

func (f TimeFilter) TestFile(fi *FileInfo) bool

func (TimeFilter) TestRaw

func (f TimeFilter) TestRaw(line string) bool

Jump to

Keyboard shortcuts

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