Documentation
¶
Overview ¶
Package cclogger implements a simple log wrapper for the standard log package.
cclogger provides a simple way of logging with different levels (debug, info, warn, error, critical). It integrates with systemd's journaling system by using standard systemd log level prefixes.
Log Levels ¶
The package supports the following log levels in order of increasing severity:
- debug: Detailed information for development and troubleshooting
- info: General informational messages about application progress
- warn: Warning messages for important but non-critical issues
- err/fatal: Error messages for failures that may allow continued execution
- crit: Critical errors that typically result in program termination
Basic Usage ¶
Initialize the logger with a log level and optional timestamp:
cclogger.Init("info", false) // info level, no timestamps (systemd adds them)
Log messages using level-specific functions:
cclogger.Debug("Detailed debug information")
cclogger.Info("Application started")
cclogger.Warn("Configuration value missing, using default")
cclogger.Error("Failed to connect to database")
cclogger.Fatal("Critical error, exiting") // exits with code 1
Use formatted variants for structured output:
cclogger.Infof("Processing %d items", count)
cclogger.Errorf("Failed to open file: %v", err)
Component Logging ¶
For component-specific logging, use the Component* variants:
cclogger.ComponentInfo("scheduler", "Job submitted successfully")
cclogger.ComponentError("database", "Connection pool exhausted")
Thread Safety ¶
All logging functions are thread-safe as they use the standard library's log.Logger, which is safe for concurrent use.
Systemd Integration ¶
Log messages use systemd's priority prefixes (<7> for debug, <6> for info, etc.) which allows systemd-journald to properly categorize log entries. When running under systemd, timestamps can be omitted (logdate=false) as journald adds them.
Index ¶
- Variables
- func Abort(v ...any)
- func Abortf(format string, v ...any)
- func ComponentDebug(component string, v ...any)
- func ComponentError(component string, v ...any)
- func ComponentInfo(component string, v ...any)
- func ComponentPrint(component string, v ...any)
- func ComponentWarn(component string, v ...any)
- func Debug(v ...any)
- func Debugf(format string, v ...any)
- func Error(v ...any)
- func Errorf(format string, v ...any)
- func Exit(v ...any)
- func Exitf(format string, v ...any)
- func Fatal(v ...any)
- func Fatalf(format string, v ...any)
- func Info(v ...any)
- func Infof(format string, v ...any)
- func Init(lvl string, logdate bool)
- func Loglevel() string
- func Panic(v ...any)
- func Panicf(format string, v ...any)
- func Print(v ...any)
- func Printf(format string, v ...any)
- func SetOutputFile(lvl string, logfile string)
- func Warn(v ...any)
- func Warnf(format string, v ...any)
Constants ¶
This section is empty.
Variables ¶
var ( // DebugWriter is the output destination for debug-level logs DebugWriter io.Writer = os.Stderr // InfoWriter is the output destination for info-level logs InfoWriter io.Writer = os.Stderr // WarnWriter is the output destination for warning-level logs WarnWriter io.Writer = os.Stderr // ErrWriter is the output destination for error-level logs ErrWriter io.Writer = os.Stderr // CritWriter is the output destination for critical-level logs CritWriter io.Writer = os.Stderr )
Package-level writers for each log level. These can be configured to direct output to different destinations. By default, all output goes to stderr. When Init() is called with a log level, writers for levels below the threshold are set to io.Discard to suppress output.
var ( // DebugPrefix is the prefix for debug-level log messages (systemd priority 7) DebugPrefix string = "<7>[DEBUG] " // InfoPrefix is the prefix for info-level log messages (systemd priority 6) InfoPrefix string = "<6>[INFO] " // WarnPrefix is the prefix for warning-level log messages (systemd priority 4) WarnPrefix string = "<4>[WARNING] " // ErrPrefix is the prefix for error-level log messages (systemd priority 3) ErrPrefix string = "<3>[ERROR] " // CritPrefix is the prefix for critical-level log messages (systemd priority 2) CritPrefix string = "<2>[CRITICAL] " )
Log level prefixes using systemd priority levels. The numeric prefix (<N>) corresponds to syslog/systemd severity levels:
<7> = Debug, <6> = Informational, <4> = Warning, <3> = Error, <2> = Critical
See: https://www.freedesktop.org/software/systemd/man/sd-daemon.html
var ( // DebugLog is the logger instance for debug-level messages DebugLog *log.Logger = log.New(DebugWriter, DebugPrefix, log.LstdFlags) // InfoLog is the logger instance for info-level messages InfoLog *log.Logger = log.New(InfoWriter, InfoPrefix, log.LstdFlags|log.Lshortfile) // WarnLog is the logger instance for warning-level messages WarnLog *log.Logger = log.New(WarnWriter, WarnPrefix, log.LstdFlags|log.Lshortfile) // ErrLog is the logger instance for error-level messages ErrLog *log.Logger = log.New(ErrWriter, ErrPrefix, log.LstdFlags|log.Llongfile) // CritLog is the logger instance for critical-level messages CritLog *log.Logger = log.New(CritWriter, CritPrefix, log.LstdFlags|log.Llongfile) )
Package-level logger instances for each log level. These are configured by Init() with appropriate flags and output destinations. Debug logs show no file info, Info/Warn show short file:line, Error/Crit show full path.
Functions ¶
func Abort ¶
func Abort(v ...any)
Abort logs to STDOUT without string formatting; application exits with error code 1. Used for terminating with message after to be expected errors, e.g. wrong arguments or during init().
func Abortf ¶
Abortf logs to STDOUT with string formatting; application exits with error code 1. Used for terminating with message after to be expected errors, e.g. wrong arguments or during init().
func ComponentDebug ¶
ComponentDebug logs to DEBUG writer with a component prefix; application continues.
func ComponentError ¶
ComponentError logs to ERROR writer with a component prefix; application continues.
func ComponentInfo ¶ added in v0.2.0
ComponentInfo logs to INFO writer with a component prefix; application continues.
func ComponentPrint ¶
ComponentPrint logs to INFO writer with a component prefix; application continues.
func ComponentWarn ¶ added in v0.2.0
ComponentWarn logs to WARNING writer with a component prefix; application continues.
func Debug ¶
func Debug(v ...any)
Debug logs to DEBUG writer without string formatting; application continues. Used for logging additional information, primarily for development.
func Debugf ¶
Debugf logs to DEBUG writer with string formatting; application continues. Used for logging additional information, primarily for development.
func Error ¶
func Error(v ...any)
Error logs to ERROR writer without string formatting; application continues. Used for logging errors, but code still can return default(s) or nil.
func Errorf ¶
Errorf logs to ERROR writer with string formatting; application continues. Used for logging errors, but code still can return default(s) or nil.
func Exit ¶
func Exit(v ...any)
Exit logs to STDOUT without string formatting; application exits with error code 0. Used for exiting succesfully with message after expected outcome, e.g. successful single-call application runs.
func Exitf ¶
Exitf logs to STDOUT with string formatting; application exits with error code 0. Used for exiting succesfully with message after expected outcome, e.g. successful single-call application runs.
func Fatal ¶
func Fatal(v ...any)
Fatal writes to CRITICAL writer without string formatting; application exits with error code 1. Used for terminating on unexpected errors with date and code location.
func Fatalf ¶
Fatalf logs to CRITICAL writer with string formatting; application exits with error code 1. Used for terminating on unexpected errors with date and code location.
func Info ¶
func Info(v ...any)
Info logs to INFO writer without string formatting; application continues. Used for logging additional information, e.g. notable returns or common fail-cases.
func Infof ¶
Infof log to INFO writer with string formatting; application continues. Used for logging additional information, e.g. notable returns or common fail-cases.
func Init ¶
Init initializes cclogger with the specified log level and timestamp configuration.
The lvl parameter accepts the following values:
- "debug": Show all log messages
- "info": Show info, warn, err, and crit messages (suppress debug)
- "warn": Show warn, err, and crit messages (suppress info and debug)
- "err" or "fatal": Show err and crit messages (suppress warn, info, and debug)
- "crit": Show only crit messages (suppress all others)
If an invalid level is provided, all levels will be enabled (debug mode) and a warning will be logged.
The logdate parameter controls timestamp inclusion:
- false: No timestamps (recommended when running under systemd, as journald adds timestamps)
- true: Include date and time in log output
Example:
cclogger.Init("info", false) // Show info and above, no timestamps
func Panic ¶
func Panic(v ...any)
Panic logs to PANIC function without string formatting; application exits with panic. Used for terminating on unexpected errors with stacktrace.
func Panicf ¶
Panicf logs to PANIC function with string formatting; application exits with panic. Used for terminating on unexpected errors with stacktrace.
func Print ¶
func Print(v ...any)
Print logs to STDOUT without string formatting; application continues. Used for special cases not requiring log information like date or location.
func Printf ¶
Printf logs to STDOUT with string formatting; application continues. Used for special cases not requiring log information like date or location.
func SetOutputFile ¶ added in v0.2.0
SetOutputFile redirects log output to a file for the specified level and all lower levels.
The lvl parameter determines which loggers write to the file:
- "debug": Only debug logs go to file
- "info": Info and debug logs go to file
- "warn": Warn, info, and debug logs go to file
- "err" or "fatal": Error, warn, info, and debug logs go to file
- "crit": All logs go to file
The file is opened in append mode and created if it doesn't exist. The file remains open for the lifetime of the loggers.
WARNING: This function does not close the file after setting it as output. The file will remain open until the program exits. This is intentional to allow continued logging to the file.
Example:
cclogger.SetOutputFile("warn", "/var/log/myapp.log")
// Now warn, info, and debug logs write to the file
// Error and critical logs still go to their default writers (stderr)
Types ¶
This section is empty.