logger

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: Apache-2.0 Imports: 14 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Logging Package

Will try to write this so we can swap out logging packages if necessary. Will start with zap (Uber open source).

Will be good to pass central logger as a configuration item for each service.

Documentation

Overview

Logging package for unified JSON logging. Will look like this: {"time": xxx, "level": xxx, "component": xxx, "message": xxx, "key": xxx}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

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

Event wraps a *zerolog.Event with a small chainable, typed-field API so callers can attach structured fields instead of interpolating them into the message string, e.g. l.WarnE().Str("email", email).Str("clientIP", ip). Msg("login: invalid credentials"). The terminal Msg call emits the log line.

func (*Event) Any

func (ev *Event) Any(key string, val interface{}) *Event

Any adds an arbitrary value under key, reflected into structured fields. Use it for the %v/%+v cases where the value is not an error and has no dedicated typed method above.

func (*Event) Bool

func (ev *Event) Bool(key string, val bool) *Event

func (*Event) Dur

func (ev *Event) Dur(key string, d time.Duration) *Event

Dur adds a duration in milliseconds under key+"Ms".

func (*Event) Err

func (ev *Event) Err(err error) *Event

Err adds the error under the standard "error" field (no-op for a nil error).

func (*Event) Fields

func (ev *Event) Fields(fields map[string]interface{}) *Event

Fields adds an arbitrary set of fields.

func (*Event) Float64

func (ev *Event) Float64(key string, v float64) *Event

func (*Event) Int

func (ev *Event) Int(key string, val int) *Event

func (*Event) Int64

func (ev *Event) Int64(key string, v int64) *Event

func (*Event) Msg

func (ev *Event) Msg(msg string)

Msg emits the event with the given message. The Event must not be reused.

func (*Event) Str

func (ev *Event) Str(key, val string) *Event

func (*Event) Strs

func (ev *Event) Strs(key string, vals []string) *Event

func (*Event) Time

func (ev *Event) Time(key string, t time.Time) *Event

func (*Event) Uint64

func (ev *Event) Uint64(key string, v uint64) *Event

type Logger

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

func NewLogger

func NewLogger(application string, component string) *Logger

Will start with console output and go from there

func NewLoggerWithHook

func NewLoggerWithHook(application string, component string, hook zerolog.Hook) *Logger

NewLoggerWithHook creates a logger with a hook pre-attached.

func (*Logger) AttachHook

func (l *Logger) AttachHook(hook zerolog.Hook) *Logger

AttachHook adds a hook to this Logger in place and returns the same Logger. This mutates the existing Logger so that any code holding a reference (e.g., middleware closures) sees the updated logger with the hook.

func (*Logger) AttachPublisher

func (l *Logger) AttachPublisher(pub NATSPublisher, service string, maxPerSec int64) *Logger

AttachPublisher configures this Logger's capture writer to publish every log event (with its structured fields) to NATS under the given service name, rate-limited to maxPerSec entries per second. It returns the same Logger. This is the structured replacement for AttachHook on the NATS log path: the capture writer is part of the MultiLevelWriter created in NewLogger, so it survives SetLevel and needs no re-attach when the level changes.

func (*Logger) Debug

func (l *Logger) Debug(msg string)

func (*Logger) DebugE

func (l *Logger) DebugE() *Event

DebugE/InfoE/WarnE/ErrorE start a structured event at the given level.

func (*Logger) Error

func (l *Logger) Error(msg string)

func (*Logger) ErrorE

func (l *Logger) ErrorE() *Event

func (*Logger) Fatal

func (l *Logger) Fatal(msg string)

func (*Logger) Info

func (l *Logger) Info(msg string)

func (*Logger) InfoE

func (l *Logger) InfoE() *Event

func (*Logger) LogRequest

func (l *Logger) LogRequest(next http.Handler) http.Handler

LogRequest is HTTP middleware that emits a structured access log for each request with typed fields (method, uri, status, clientIP, latencyMs, requestID) instead of an interpolated string. It honors an inbound X-Request-Id header and echoes the request id back on the response so it can be correlated across services and in the admin Logs UI.

func (*Logger) Panic

func (l *Logger) Panic(msg string)

func (*Logger) SetLevel

func (l *Logger) SetLevel(level zerolog.Level)

SetLevel changes the minimum log level at runtime.

func (*Logger) Warn

func (l *Logger) Warn(msg string)

func (*Logger) WarnE

func (l *Logger) WarnE() *Event

func (*Logger) ZeroLogger

func (l *Logger) ZeroLogger() zerolog.Logger

ZeroLogger returns the underlying zerolog.Logger for packages that need it

type LoggerConfig

type LoggerConfig struct {
	LogLevel *zerolog.Level
	Outputs  []interface{}
}

type NATSHook

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

NATSHook is a zerolog Hook that publishes log events to NATS.

func NewNATSHook

func NewNATSHook(opts *NATSHookOptions) *NATSHook

NewNATSHook creates a new zerolog hook that publishes to NATS.

func (*NATSHook) Run

func (h *NATSHook) Run(e *zerolog.Event, level zerolog.Level, msg string)

Run implements zerolog.Hook. It is called for every log event.

type NATSHookOptions

type NATSHookOptions struct {
	Publisher NATSPublisher
	Service   string
	Component string
	MaxPerSec int64 // Max log entries per second (default 100)
}

NATSHookOptions configures the NATS log hook.

type NATSPublisher

type NATSPublisher interface {
	PublishRaw(subject string, data []byte) error
	IsConnected() bool
}

NATSPublisher is an interface for publishing log entries to NATS. This avoids a direct dependency on the messenger package.

Jump to

Keyboard shortcuts

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