Skip to content

Instantly share code, notes, and snippets.

@markizano
Created October 5, 2023 10:48
Show Gist options
  • Save markizano/f618f8a69c997839c42c5b940d7a3b58 to your computer and use it in GitHub Desktop.
Save markizano/f618f8a69c997839c42c5b940d7a3b58 to your computer and use it in GitHub Desktop.
Common functions to be used in scripts for formatted logging
#!/bin/bash
# This script not intended to run by itself, nor have exec permissions.
# This script intended only for sourcing from other scripts to reduce copy-paste code.
# Redirect STDOUT and STDERR to the system logger (syslog-ng).
function log () {
log_args='-i'
log_args="$log_args -u/dev/log"
log_args="$log_args -t $1"
test -t 1 -a -z "$NO_STDERR" && \
log_args="$log_args -s"
while read logline; do
echo "$logline" | logger $log_args
done
}
function log_debug_msg () {
echo -e "[ \e[36mDBUG\e[0m ]: $@"
}
function log_info_msg () {
echo -e "[ \e[34mINFO\e[0m ]: $@"
}
function log_error_msg () {
echo -e "[ \e[31mERR\e[0m ]: $@"
}
function log_warn_msg () {
echo -e "[ \e[33mWARN\e[0m ]: $@"
}
function log_ok_msg () {
echo -e "[ \e[32mOK\e[0m ]: $@"
}
function log_fail_msg () {
echo -e "[ \e[31mFAIL\e[0m ]: $@"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment