Skip to content
Fabian Fuchs edited this page Oct 7, 2024 · 6 revisions

simbashlog

Matrix

A logging framework that is a "must-have" on Linux systems with Bash.

Simbashlog is powerful yet user-friendly.

Topics which are assigned to simbashlog

Use simbashlog

Bash

If you do not want to (or cannot) source simbashlog, then you can add something like this to your bash script after the simbashlog installation:

LOG_LEVEL=6
LOG_DIR="/var/log/"
NOTIFIER="simbashlog-example-notifier"

function log {
    local severity="$1"
    local message="$2"

    local log_level="$LOG_LEVEL"
    local log_dir="$LOG_DIR"
    local notifier="$NOTIFIER"

    local simbashlog_action="log"
    local severity_code

    # Set severity code
    case "$severity" in
    debug | 7)
        severity_code=7
        ;;
    info | 6)
        severity_code=6
        ;;
    notice | 5)
        severity_code=5
        ;;
    warn | 4)
        severity_code=4
        ;;
    error | 3)
        severity_code=3
        ;;
    crit | 2)
        severity_code=2
        ;;
    alert | 1)
        severity_code=1
        ;;
    emerg | 0)
        severity_code=0
        ;;
    *)
        # Default to debug if severity is unknown
        severity_code=7
        echo "Unknown severity: $severity"
        ;;
    esac

    local simbashlog_command=("simbashlog" "--action" "$simbashlog_action" "--severity" "$severity_code" "--message" "$message" "--log-level" "$log_level" "--log-dir" "$log_dir")

    # Add notifier if set
    if [ -n "$notifier" ]; then
        simbashlog_command+=("--notifier" "$notifier")
    fi

    # Execute simbashlog command
    "${simbashlog_command[@]}" ||
        {
            echo "Failed to execute: simbashlog"
            exit 1
        }

    # Exit if severity is error or higher
    if [[ "$severity_code" -lt 3 ]]; then
        exit 1
    fi
}
Clone this wiki locally