Skip to content

Logging System

The script has a structured logging system with colored terminal output and optional file logging.

Configuration Variables

Variable Default Description
VERBOSE 0 Set to 1 for debug output
LOG_FILE openvpn-install.log Path to log file. Empty string disables file logging
OUTPUT_FORMAT table table or json. JSON mode suppresses most log output
FORCE_COLOR Set to 1 to force colored output even when not a TTY

Log Levels

Function Prefix Color Behavior
log_info() [INFO] Blue General information. Suppressed in JSON mode
log_warn() [WARN] Yellow Warnings. Suppressed in JSON mode
log_error() [ERROR] Red Errors. Always shown, goes to stderr
log_fatal() [ERROR] Red Fatal error — logs and calls exit 1
log_success() [OK] Green Success messages. Suppressed in JSON mode
log_debug() [DEBUG] Gray Only shown when VERBOSE=1
log_prompt() Cyan User prompts. Skipped in non-interactive mode
log_header() Bold blue Section headers with === borders

All log functions also write to the log file (without colors, with timestamps) via _log_to_file().

Command Execution

Two helper functions wrap command execution with logging:

run_cmd()

Runs a command, logs it, and optionally suppresses output:

run_cmd apt-get install -y openvpn
  • Shows the command being run in gray text
  • Logs the command with [CMD] prefix
  • Returns the command's exit code
  • In verbose mode, shows stdout/stderr; otherwise captures them

run_cmd_fatal()

Same as run_cmd(), but calls log_fatal() (exits the script) if the command fails.

run_cmd_fatal systemctl start openvpn-server@server