sinit

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2025 License: MIT Imports: 20 Imported by: 0

README

sinit

Common log config and initialization

For a full example run:

export SPLUNK_URL=xxx
export SPLUNK_TOKEN=xxx
export AWS_REGION=xxx
export AWS_SECRET=xxx
export AWS_KEY=xxx
export SENTRY_DSN=xxx
go run github.com/osbuild/logging/internal/example_sinit/

pgx logging

This package provides a function which returns a wrapper that can be used for pgx SQL driver logging:

pgxConfig.Tracer = sinit.PgxTracer(slog.Default())

Documentation

Overview

Common log configuration, initialization and flushing functions for osbuild projects.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTimeoutDuringClose = errors.New("timeout during close")
	ErrSentryTimeout      = errors.New("sentry timeout during close")
	ErrNotInitialized     = errors.New("logging not initialized, call InitializeLogging first")
)
View Source
var (
	ErrAlreadyInitialized   = errors.New("logging already initialized, call Close clean")
	ErrInvalidURL           = errors.New("invalid URL")
	ErrMissingURL           = errors.New("missing URL")
	ErrSentryInitialization = errors.New("sentry initialization error")
)

Functions

func Close added in v0.0.5

func Close(timeout time.Duration) error

Close flushes all pending logs to the configured outputs and closes all destinations so InitializeLogging can be called again. Blocks not more than specified timeout. This is useful in environments like Kubernetes where the application might be terminated and we want to ensure logs are flushed.

Returns ErrTimeoutDuringClose if timeout was reached together with multiple errors from the handlers that failed to close within the timeout, or any other errors that occurred during the close process.

func Flush

func Flush() error

Flush flushes all pending logs to the configured outputs. Depending on the logging configuration, it issues flush commands to various systems which behave differently:

CloudWatch and Splunk handlers issue a flush command that has no guarantee of completion, meaning logs may not be flushed immediately. No blocking is performed.

Sentry SDK flushes logs with blocking up to 2 seconds.

Calling Flush without previously calling InitializeLogging will return ErrNotInitialized.

Do not use this function during application exit, use Close() instead to ensure all logs are flushed properly.

func InitializeLogging

func InitializeLogging(ctx context.Context, config LoggingConfig) error

InitializeLogging initializes the logging system with the provided configuration. Use Close to ensure all logs are written before exiting. Subsequent calls to InitializeLogging will lead to ErrAlreadyInitialized.

func PgxTracer added in v0.0.4

func PgxTracer(logger *slog.Logger) pgx.QueryTracer

PgxTracer returns a wrapper for PGX logger which logs into the initialized slog. Function InitializeLogging must be called first. Usage:

pgxConfig.Tracer = sinit.PgxTracer(slog.Default())

func StdLogger

func StdLogger() *log.Logger

StdLogger returns a standard library legacy logger that writes to configured outputs. This is only useful for passing to libraries that require a legacy Go standard logger.

If logging was not initialized, it returns a logger that writes to io.Discard.

Types

type CloudWatchConfig

type CloudWatchConfig struct {
	// Enabled is a flag to enable this output.
	Enabled bool

	// Logging level for this output. Strings "debug", "info", "warn", "error", "fatal", "panic" are accepted.
	// Keep in mind that log/slog has only 4 levels: Debug, Info, Warn, Error. Default value is "debug".
	Level string

	// AWSRegion is the AWS region.
	AWSRegion string

	// AWSKey is the AWS access key.
	AWSKey string

	// AWSSecret is the AWS secret key.
	AWSSecret string

	// AWSSession is an optional AWS session token.
	AWSSession string

	// AWSLogGroup is the AWS CloudWatch log group.
	AWSLogGroup string

	// AWSLogStream is the AWS CloudWatch log stream.
	AWSLogStream string
}

CloudWatchConfig is the configuration for the CloudWatch output.

type JournalConfig

type JournalConfig struct {
	// Enabled is a flag to enable this output.
	Enabled bool

	// Logging level for this output. Strings "debug", "info", "warn", "error", "fatal", "panic" are accepted.
	// Keep in mind that log/slog has only 4 levels: Debug, Info, Warn, Error. Default value is "debug".
	Level string
}

JournalConfig is the configuration for the system journal.

type LoggingConfig

type LoggingConfig struct {
	StdoutConfig StdoutConfig

	JournalConfig JournalConfig

	SplunkConfig SplunkConfig

	CloudWatchConfig CloudWatchConfig

	SentryConfig SentryConfig

	TracingConfig TracingConfig

	LogrusConfig LogrusConfig
}

LoggingConfig is the configuration for the logging system.

type LogrusConfig added in v0.0.5

type LogrusConfig struct {
	// Enabled is a flag to enable logrus proxy.
	Enabled bool

	// ExitOnFatal is a flag to enable exiting the process on fatal log entries.
	// If set to true, the process will exit with status code 1 on fatal log entries as
	// wel as panic log entries.
	ExitOnFatal bool
}

LogrusConfig is the configuration for the logrus proxy.

type SentryConfig

type SentryConfig struct {
	// Enabled is a flag to enable Sentry.
	Enabled bool

	// DSN is the Sentry DSN.
	DSN string
}

SentryConfig is the configuration for the Sentry output. Only log entries with error level are sent to Sentry.

type SplunkConfig

type SplunkConfig struct {
	// Enabled is a flag to enable this output.
	Enabled bool

	// Logging level for this output. Strings "debug", "info", "warn", "error", "fatal", "panic" are accepted.
	// Keep in mind that log/slog has only 4 levels: Debug, Info, Warn, Error. Default value is "debug".
	Level string

	// URL is the Splunk HEC URL.
	URL string

	// Token is the Splunk HEC token.
	Token string

	// Source is the Splunk HEC source.
	Source string

	// Hostname is the Splunk HEC hostname.
	Hostname string
}

SplunkConfig is the configuration for the Splunk output.

type StdoutConfig

type StdoutConfig struct {
	// Enabled is a flag to enable this output.
	Enabled bool

	// Logging level for this output. Strings "debug", "info", "warn", "error", "fatal", "panic" are accepted.
	// Keep in mind that log/slog has only 4 levels: Debug, Info, Warn, Error. Default value is "debug".
	Level string

	// Format is the log format to use for stdout logging. Possible values are "json" and "text".
	Format string
}

StdoutConfig is the configuration for the standard output.

type TracingConfig

type TracingConfig struct {
	// Enabled is a flag to enable tracing
	Enabled bool

	// CustomAttrs is a list of custom static attributes to add to every log entry. To add
	// dynamic attributes, use ContextCallback that can access context.
	CustomAttrs []slog.Attr

	// ContextCallback is an optional callback function that is called for each log entry
	// to add additional attributes to the log entry.
	ContextCallback strc.MultiCallback
}

TracingConfig is the configuration for strc.

Jump to

Keyboard shortcuts

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