logger

package
v1.30.1 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	LogLevel   slog.LevelVar
	Logger     *slog.Logger = slog.Default()
	LogHandler slog.Handler
)

package globals available to callers

Functions

func CommandLine added in v1.30.0

func CommandLine(args []string) slog.Attr

CommandLine returns a slog.Attr representing the command line with the given name and arguments. It redacts sensitive information using the Redact function.

func Init added in v1.30.0

func Init(prefix string, options ...LoggerOption) *slog.Logger

Init is called to set-up logging with our chosen defaults. The default is to log to STDERR.

If logfile is passed and the first element is not empty, then use that as the log file unless it is either "-" (which means use STDOUT (not STDERR) or equal to the os.DevNull value, in which case is io.Discard.

The environment variable `CORDIAL_LOG_OUTPUT` can be used to override the logfile option. If `CORDIAL_LOG_OUTPUT` is set, then the logger.SetLogfile option is ignored.

The environment variable `CORDIAL_LOG_FORMAT` can be used to set the log format. If `CORDIAL_LOG_FORMAT` is set to "json", then the log format is JSON, otherwise the format is a human readable format, with colouring enabled if the output is a termial according to `term.IsTerminal()`.

func NewGlobalAuditLogger added in v1.30.0

func NewGlobalAuditLogger(global io.Writer)

NewGlobalAuditLogger TODO: Document its behavior, inputs, and effects.

func NewHandler added in v1.28.0

func NewHandler(options ...HandlerOption) (handler slog.Handler)

NewHandler creates a new instance of the custom slog handler with the provided options.

func QuotedCmdLine added in v1.30.0

func QuotedCmdLine(args []string) string

QuotedCmdLine returns the command line, made up of args, as a single string with arguments containing spaces quoted. It also redacts sensitive information using the Redact function.

func Redact added in v1.30.0

func Redact(s string) string

Redact redacts sensitive information from the given string using the redactRE regular expression.

func RedactJSON added in v1.30.0

func RedactJSON(j []byte) []byte

RedactJSON redacts sensitive information from the given JSON byte slice using a regular expression. It also redacts the text in the values using Redact, for example environment variables in NAME=VALUE format.

func RedactSlice added in v1.30.0

func RedactSlice(slice []string) []string

RedactSlice redacts sensitive information from each string in the given slice using the Redact function.

func ResetEventID added in v1.30.0

func ResetEventID()

ResetEventID generates a new unique audit event ID for the current program execution. The event ID is used for all handlers in a single audit logger.

This function should be called whenever a new audit event ID is required, for example, at the start of a new operation or transaction.

Types

type AuditLogger added in v1.30.0

type AuditLogger struct {
	*slog.Logger
}

AuditLogger TODO: Describe its purpose and intended use.

func Audit added in v1.30.0

func Audit() *AuditLogger

Audit TODO: Document its behavior, inputs, and effects.

func NewAuditLogger added in v1.30.0

func NewAuditLogger(options ...AuditOption) *AuditLogger

NewAuditLogger creates a new audit logger with the specified options. The logger writes to one or more destinations, depending on the options provided, using a multi-destination handler. A global audit file always receives all audit entries. If an optional writer is specifed using logger.AuditWriter, it will also receive the audit entries.

The audit logger will always log the username of the current user, and if it is different then the login name (from `/proc/self/loginuid` on Linux).

func (*AuditLogger) Event added in v1.30.0

func (a *AuditLogger) Event(event string, args ...any)

Event logs an audit event with the specified event name and optional arguments. If the AuditLogger is nil, the event will not be logged.

type AuditOption added in v1.30.0

type AuditOption func(*auditOptions)

AuditOption TODO: Describe its purpose and intended use.

func AuditWriter added in v1.30.0

func AuditWriter(w io.Writer) AuditOption

AuditWriter sets the writer for the audit logger. By default, it writes to os.Stderr.

func GlobalWriter added in v1.30.0

func GlobalWriter(w io.Writer) AuditOption

GlobalWriter TODO: Document its behavior, inputs, and effects.

type Handler added in v1.28.0

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

Handler TODO: Describe its purpose and intended use.

func (*Handler) Enabled added in v1.28.0

func (h *Handler) Enabled(_ context.Context, level slog.Level) bool

Enabled TODO: Document its behavior, inputs, and effects.

func (*Handler) Handle added in v1.28.0

func (h *Handler) Handle(_ context.Context, r slog.Record) error

Handle TODO: Document its behavior, inputs, and effects.

func (*Handler) WithAttrs added in v1.28.0

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

WithAttrs TODO: Document its behavior, inputs, and effects.

func (*Handler) WithGroup added in v1.28.0

func (h *Handler) WithGroup(name string) slog.Handler

WithGroup TODO: Document its behavior, inputs, and effects.

type HandlerOption added in v1.30.0

type HandlerOption func(*handlerOpts)

HandlerOption TODO: Describe its purpose and intended use.

func Delimiter added in v1.28.0

func Delimiter(delimiter string) HandlerOption

Delimiter sets the delimiter between attributes in the output. The default is a single dot.

func JSON added in v1.28.0

func JSON() HandlerOption

JSON configures the handler to output JSON instead of a human-readable format.

func Leveler added in v1.28.0

func Leveler(level *slog.LevelVar) HandlerOption

Leveler sets the log level for the handler using a slog.Leveler. This allows the log level to be changed at runtime. The default is a pointer to slog.LevelInfo.

func TimeFormat added in v1.28.0

func TimeFormat(format string) HandlerOption

TimeFormat sets the time format for time attributes. The default is "2006-01-02T15:04:05.000Z07:00". The format should be a valid Go time format string.

func TrimSourcePathTo added in v1.30.0

func TrimSourcePathTo(anchor string) HandlerOption

TrimSourcePathTo sets the anchor in the source path to trim up to, automatically including a trailing '/'. e.g. if the source path is "/home/user/project/pkg/file.go" and the anchor is "project", the resulting source path in the log will be "pkg/file.go".

func Writer added in v1.28.0

func Writer(w io.Writer) HandlerOption

Writer sets the output writer for the handler. The default is os.Stderr.

type LoggerOption added in v1.30.0

type LoggerOption func(*loggerOpts)

LoggerOption TODO: Describe its purpose and intended use.

func Format added in v1.30.0

func Format(format string) LoggerOption

Format TODO: Document its behavior, inputs, and effects.

func RotateOnStart added in v1.30.0

func RotateOnStart(rotate bool) LoggerOption

RotateOnStart TODO: Document its behavior, inputs, and effects.

func SetLogLevel added in v1.30.0

func SetLogLevel(level slog.Level) LoggerOption

SetLogLevel takes a slog debug level to use as a default

func SetLogfile added in v1.30.0

func SetLogfile(logfile string) LoggerOption

SetLogfile TODO: Document its behavior, inputs, and effects.

func SetWriter added in v1.30.0

func SetWriter(w io.Writer) LoggerOption

SetWriter set the log writer to the configured lumberjack/timeberjack settings but only if the lj.Filename field is not empty, otherwise it is ignored.

Jump to

Keyboard shortcuts

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