The Bash Logging Module supports standard syslog log levels, providing a hierarchy of severity from most critical to least critical.
- Overview
- Available Log Levels
- Log Level Details
- Special Level: SENSITIVE
- Setting Log Levels
- Log Level Filtering
- Choosing the Right Level
- Best Practices
- Examples
- Related Documentation
Log levels allow you to control the verbosity of your logging output. When you set a log level, only messages at that level or higher (more severe) are displayed.
| Level | Numeric Value | Function | Syslog Priority | Description |
|---|---|---|---|---|
| EMERGENCY | 0 | log_emergency |
emerg | System is unusable |
| ALERT | 1 | log_alert |
alert | Action must be taken immediately |
| CRITICAL | 2 | log_critical |
crit | Critical conditions |
| ERROR | 3 | log_error |
err | Error conditions |
| WARN | 4 | log_warn |
warning | Warning conditions |
| NOTICE | 5 | log_notice |
notice | Normal but significant condition |
| INFO | 6 | log_info |
info | Informational messages |
| DEBUG | 7 | log_debug |
debug | Debug-level messages |
When to use: System is completely unusable or about to crash.
log_emergency "Critical system failure - cannot continue"Examples:
- Complete system failure
- Unrecoverable errors that require immediate shutdown
- Data corruption preventing any operation
When to use: Action must be taken immediately.
log_alert "Database connection lost - attempting recovery"Examples:
- Service outages requiring immediate attention
- Security breaches detected
- Critical resource exhaustion
When to use: Critical conditions that may lead to system failure.
log_critical "Disk space critically low: 1% remaining"Examples:
- Hardware failures
- Critical resource depletion
- Application component failures
When to use: Error conditions that prevent normal operation.
log_error "Failed to write to configuration file"Examples:
- File I/O errors
- Failed operations
- Invalid input or state
- Recoverable errors
When to use: Warning conditions that should be reviewed but don't prevent operation.
log_warn "API rate limit approaching threshold"Examples:
- Deprecated feature usage
- Configuration issues that can be worked around
- Resource usage approaching limits
- Potential future problems
When to use: Normal but significant conditions worth noting.
log_notice "Configuration reloaded successfully"Examples:
- Service startup/shutdown
- Configuration changes
- Significant but normal events
- Successful completion of important operations
When to use: General informational messages about normal operation.
log_info "Processing file: data.csv"Examples:
- Progress updates
- Status messages
- Regular operational events
- User actions
Note: INFO is the default log level.
When to use: Detailed debugging information for development and troubleshooting.
log_debug "Variable value: count=$count, max=$max"Examples:
- Variable values
- Function entry/exit
- Detailed state information
- Development diagnostics
Note: DEBUG messages are suppressed by default. Enable with --verbose or --level DEBUG.
The log_sensitive function provides a special logging level for sensitive information:
log_sensitive "API Key: $API_KEY"Behavior:
- Displayed only on console
- Never written to log files
- Never sent to system journal
- Logged at INFO level severity for display purposes
See Sensitive Data for more details.
# Default (INFO)
init_logger
# Debug mode
init_logger --verbose
init_logger --level DEBUG
# Specific level
init_logger --level WARN
init_logger --level ERROR
# Using numeric value
init_logger --level 4 # WARN# Change level during execution
set_log_level DEBUG
set_log_level WARN
set_log_level ERRORSee Runtime Configuration for more details.
When you set a log level, messages below that severity are suppressed:
# Set level to WARN
init_logger --level WARN
log_debug "Not shown"
log_info "Not shown"
log_notice "Not shown"
log_warn "SHOWN"
log_error "SHOWN"
log_critical "SHOWN"# See everything
init_logger --verbose # DEBUG level# See informational and above
init_logger --level INFO# See warnings and errors only
init_logger --level WARN# See errors only
init_logger --level ERROR- Use INFO for normal operation - Regular status updates and progress
- Use DEBUG liberally in code - Add debug messages during development, they're hidden by default
- Use WARN for potential problems - Issues that should be reviewed but don't require immediate action
- Use ERROR for failures - Operations that failed but allow continued execution
- Reserve CRITICAL and above for severe issues - System-threatening conditions
- Use SENSITIVE for secrets - Passwords, tokens, keys, and other sensitive data
#!/bin/bash
source /path/to/logging.sh
# Default - shows INFO and above
init_logger
log_debug "Detailed debug info" # Hidden
log_info "Normal operation" # Shown
log_warn "Potential issue" # Shown
log_error "Error occurred" # Shown#!/bin/bash
source /path/to/logging.sh
# Debug mode - shows everything
init_logger --verbose
log_debug "Detailed debug info" # Shown
log_info "Normal operation" # Shown
log_warn "Potential issue" # Shown
log_error "Error occurred" # Shown#!/bin/bash
source /path/to/logging.sh
# Production - only warnings and errors
init_logger --level WARN
log_debug "Detailed debug info" # Hidden
log_info "Normal operation" # Hidden
log_warn "Potential issue" # Shown
log_error "Error occurred" # Shown- Initialization - Setting log level at startup
- Runtime Configuration - Changing log level dynamically
- Sensitive Data - Handling sensitive information
- Examples - More usage examples