logger

package
v0.0.0-...-9da5cd7 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2025 License: GPL-3.0 Imports: 9 Imported by: 990

Documentation

Overview

The logger package implements logging facilities for snapd. When built with the structuredlogging build tag, it offers the ability to use structured JSON for log entries and to turn on trace logging. To activate JSON logging, the SNAPD_JSON_LOGGING environment variable should be set at the time of logger creation. Trace logging can be activated by setting the SNAPD_TRACE env variable.

When built without the structuredlogging build tag, the logger package offers only the simple logger and will not activate trace logging even if the SNAPD_TRACE env variable is set.

Index

Constants

View Source
const (
	// DefaultFlags are passed to the default console log.Logger
	DefaultFlags = log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile
)

Variables

View Source
var NullLogger = nullLogger{}

NullLogger is a logger that does nothing

Functions

func BootSetup

func BootSetup() error

BootSetup creates a logger meant to be used when running from initramfs, where we want to consider the quiet kernel option.

func Debug

func Debug(msg string)

Debug records something in the debug log

func Debugf

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

Debugf records something in the debug log

func MockDebugLogger

func MockDebugLogger() (buf *bytes.Buffer, restore func())

MockDebugLogger replaces the existing logger with a buffer and returns the log buffer and a restore function. The logger records debug messages.

func MockLogger

func MockLogger() (buf *bytes.Buffer, restore func())

MockLogger replaces the existing logger with a buffer and returns the log buffer and a restore function.

func NoGuardDebugf

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

NoGuardDebugf records something in the debug log

func Notice

func Notice(msg string)

Notice notifies the user of something

func Noticef

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

Noticef notifies the user of something

func Panicf

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

Panicf notifies the user and then panics

func SetLogger

func SetLogger(l Logger)

SetLogger sets the global logger to the given one

func SimpleSetup

func SimpleSetup(opts *LoggerOptions)

SimpleSetup creates the default (console) logger

func StartupStageTimestamp

func StartupStageTimestamp(stage string)

StartupStageTimestamp produce snap startup timings message.

func Trace

func Trace(msg string, attrs ...any)

Trace records something in the trace log

func WithLoggerLock

func WithLoggerLock(f func())

WithLoggerLock invokes f with the global logger lock, useful for tests involving goroutines with MockLogger.

Types

type Log

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

func (*Log) Debug

func (l *Log) Debug(msg string)

Debug only prints if SNAPD_DEBUG is set

func (*Log) NoGuardDebug

func (l *Log) NoGuardDebug(msg string)

NoGuardDebug always prints the message, w/o gating it based on environment variables or other configurations.

func (*Log) Notice

func (l *Log) Notice(msg string)

Notice alerts the user about something, as well as putting in syslog

func (*Log) Trace

func (l *Log) Trace(string, ...any)

Trace only prints if SNAPD_TRACE is set and the structured logger is used

type Logger

type Logger interface {
	// Notice is for messages that the user should see
	Notice(msg string)
	// Debug is for messages that the user should be able to find if they're debugging something
	Debug(msg string)
	// NoGuardDebug is for messages that we always want to print (e.g., configurations
	// were checked by the caller, etc)
	NoGuardDebug(msg string)
	// Trace is for messages useful for tracing execution
	Trace(msg string, attrs ...any)
}

A Logger is a fairly minimal logging tool.

func New

func New(w io.Writer, flag int, opts *LoggerOptions) Logger

New creates a log.Logger using the given io.Writer and flag, using the options from opts.

type LoggerOptions

type LoggerOptions struct {
	// ForceDebug can be set if we want debug traces even if not directly
	// enabled by environment or kernel command line.
	ForceDebug bool
}

Jump to

Keyboard shortcuts

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