Skip to content

Instantly share code, notes, and snippets.

@epcim
Last active August 30, 2022 16:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epcim/e2954b71da37437a9e81ba5571d5876a to your computer and use it in GitHub Desktop.
Save epcim/e2954b71da37437a9e81ba5571d5876a to your computer and use it in GitHub Desktop.
bash snippet for systemd logging functions
#!/bin/bash
printf_stdin() { local stdin; read -d '' -u 0 stdin; printf "$@" "$stdin"; }
exec {log_fd}> >(systemd-cat -t $(basename $0))
log_emerg() { echo "EMERG: $1"|tee /dev/stderr| printf_stdin '<0>%s\n' "$1" >&"$log_fd"; }
log_alert() { echo "ALERT: $1"|tee /dev/stderr| printf_stdin '<1>%s\n' "$1" >&"$log_fd"; }
log_crit() { echo "CRIT: $1" |tee /dev/stderr| printf_stdin '<2>%s\n' "$1" >&"$log_fd"; }
log_err() { echo "ERROR: $1"|tee /dev/stderr| printf_stdin '<3>%s\n' "$1" >&"$log_fd"; }
log_warn() { echo "WARN: $1" |tee /dev/stderr| printf_stdin '<4>%s\n' "$1" >&"$log_fd"; }
log_notice() { echo "NOTE: $1" |tee /dev/stderr| printf_stdin '<5>%s\n' "$1" >&"$log_fd"; }
log_info() { echo "INFO: $1" |tee /dev/stderr| printf_stdin '<6>%s\n' "$1" >&"$log_fd"; }
log_debug() { echo "DEBUG: $1"|tee /dev/stderr| printf_stdin '<7>%s\n' "$1" >&"$log_fd"; }
log_error() { log_err "$1";}
log_warning(){ log_warn "$1";}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment