Documentation
¶
Overview ¶
Common log configuration, initialization and flushing functions for osbuild projects.
Index ¶
- Variables
- func Close(timeout time.Duration) error
- func Flush() error
- func InitializeLogging(ctx context.Context, config LoggingConfig) error
- func PgxTracer(logger *slog.Logger) pgx.QueryTracer
- func StdLogger() *log.Logger
- type CloudWatchConfig
- type JournalConfig
- type LoggingConfig
- type LogrusConfig
- type SentryConfig
- type SplunkConfig
- type StdoutConfig
- type TracingConfig
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func Close ¶ added in v0.0.5
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.
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.