Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Basic logging commands for Linux shell scripts
## Simple logging mechanism for Bash
## Author: Michael Wayne Goodman <>
## Thanks: Jul for the idea to add a datestring. See:
## Thanks: @gffhcks for noting that inf() and debug() should be swapped,
## and that critical() used $2 instead of $1
## License: Public domain; do as you wish
exec 3>&2 # logging stream (file descriptor 3) defaults to STDERR
verbosity=3 # default to show warnings
notify() { log $silent_lvl "NOTE: $1"; } # Always prints
critical() { log $crt_lvl "CRITICAL: $1"; }
error() { log $err_lvl "ERROR: $1"; }
warn() { log $wrn_lvl "WARNING: $1"; }
inf() { log $inf_lvl "INFO: $1"; } # "info" is already a command
debug() { log $dbg_lvl "DEBUG: $1"; }
log() {
if [ $verbosity -ge $1 ]; then
datestring=`date +'%Y-%m-%d %H:%M:%S'`
# Expand escaped characters, wrap at 70 chars, indent wrapped lines
echo -e "$datestring $2" | fold -w70 -s | sed '2~1s/^/ /' >&3

This comment has been minimized.

Copy link
Owner Author

goodmami commented Sep 13, 2013

To make the above working example, append the following to the bottom of the file:


usage() {
    echo "Usage:"
    echo "  $0 [OPTIONS]"
    echo "Options:"
    echo "  -h      : display this help message"
    echo "  -q      : decrease verbosity level (can be repeated: -qq, -qqq)"
    echo "  -v      : increase verbosity level (can be repeated: -vv, -vvv)"
    echo "  -l FILE : redirect logging to FILE instead of STDERR"

while getopts "hqvl:" opt; do
    case "$opt" in
       h) usage; exit 0 ;;
       q) (( verbosity = verbosity - 1 )) ;;
       v) (( verbosity = verbosity + 1 )) ;;
       l) exec 3>>$OPTARG ;;
       *) error "Invalid options: $1"; usage; exit 1 ;;
shift $((OPTIND-1))

notify "This logging system uses the standard verbosity level mechanism to choose which messages to print. Command line arguments customize this value, as well as where logging messages should be directed (from the default of STDERR). Long messages will be split at spaces to wrap at a character limit, and wrapped lines are indented. Wrapping and indenting can be modified in the code."

inf "Inspecting argument list: $args"

if [ ! "$args" ]; then
    warn "No arguments given"
    for arg in $args; do
        debug "$arg"

This comment has been minimized.

Copy link

gffhcks commented Sep 21, 2015

Shouldn't "debug" be at a higher level of verbosity than "info"?

Also, why does $2 get used instead of $1 for critical()?


This comment has been minimized.

Copy link
Owner Author

goodmami commented Jul 22, 2016

@gffhcks, sorry to be so late to respond. I didn't get any notification for your comment.

And thanks for pointing these out. Debug and Info are in the unconventional order because I didn't closely check with what other logging frameworks do. I'm not sure why I used $2 in critical(); that's just a bug. I'll fix both of these.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.