sglog

package module
v0.0.0-...-230bd74 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2025 License: Apache-2.0 Imports: 16 Imported by: 4

Documentation

Overview

Package sglog provides a logging handler for the log/slog package that writes log messages to multiple files based on their severity, similar to Google's glog package.

This package is inspired by Google's glog package for Go but introduces several differences and new features, such as log file reuse. Most of the code is adapted from glog, with modifications to support the slog ecosystem and additional functionality.

Differences from Google's glog

  • Log messages are not buffered inline with the application control flow.
  • The standard log/slog package does not define a Fatal level, so FATAL messages and log files are not supported.
  • Global flags from glog are replaced with an Options struct for configuration.
  • Unlike glog, this package does not add a footer message when rotating log files.
  • When log file reuse is enabled, log file names may not precisely reflect the log file creation time, though timestamps in file names remain in chronological order.

Log File Reuse

Google's glog creates a new log file each time a process restarts, which can exhaust filesystem inodes if the process crashes repeatedly. The sglog package mitigates this by enabling log file reuse with a configurable timeout (e.g., one log file per hour).

Log files are still rotated when they reach the configured maximum size limit.

VModule Usage

In addition to log levels, logging can be selectively enabled or disabled using vmodule attributes, similar to glog's vmodule feature. Users must define a reusable attribute for each module (typically at global scope) and use it with the slog.With function to log module-specific messages.

Example:

var network = sglog.VModule("network", slog.LevelDebug)
...
slog.With(network).Info("Network event", ...)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetVModuleLevel

func SetVModuleLevel(a slog.Attr, l slog.Level) bool

SetVModuleLevel changes a vmodule attribute's level dynamically. Returns false if input attribute is not a vmodule attribute.

func VModule

func VModule(name string, level slog.Level) slog.Attr

VModule creates a module log level control attribute.

VModule attributes contain a private log level that can be used to turn on/off debug logging level for log messages related to a module.

Users can change a module's log level dynamically without effecting other module's log level.

func VModuleLevel

func VModuleLevel(a slog.Attr) (slog.Level, bool)

VModuleLevel retrieves a vmodule attribute's level. Returns (0, false) if input attribute is not a vmodule attribute.

Types

type Backend

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

func NewBackend

func NewBackend(opts *Options) *Backend

NewBackend creates a slog backend.

func (*Backend) Close

func (v *Backend) Close()

Close flushes the logs and waits for the background goroutine to finish.

func (*Backend) Handler

func (v *Backend) Handler() slog.Handler

Handler returns slog.Handler for the log backend.

func (*Backend) SetLevel

func (v *Backend) SetLevel(level slog.Level) slog.Level

SetLevel updates the default log level.

type Options

type Options struct {
	// Name holds the program name to use with the log files.
	Name string

	// LogDirs if non-empty create/write log files in one of these directories.
	LogDirs []string

	// LogLinkDir if non-empty, adds symbolic links in this directory to the log
	// files.
	LogLinkDir string

	// LogFileMaxSize is the maximum size of a log file in bytes. Header and
	// footer messages are not accounted in this so final log file size can be
	// larger than this limit by a few hundred bytes.
	LogFileMaxSize uint64

	// LogFileMode is the log file mode/permissions.
	LogFileMode os.FileMode

	// LogFileHeader when true writes the file header at the start of each log
	// file.
	LogFileHeader bool

	// LogFileReuseDuration is the maximum duration to reuse/reopen an existing
	// log file as long as it doesn't cross the maximum log file size.
	LogFileReuseDuration time.Duration

	// LogMessageMaxLen is the limit on length of a formatted log message,
	// including the standard line prefix and trailing newline. Messages longer
	// than this value are truncated.
	LogMessageMaxLen int
}

Jump to

Keyboard shortcuts

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