Technotes

Technotes for future me

Logging in bash scripts

Support different logging levels natively in your scripts so that your code will be more stable and maintainable

BAD

#!/bin/bash -l
...
# for debug only, comment out when OK
echo $a
do_something $a

# echo $? # sometimes does not work?

GOOD

Nothing to invent, there are already a few blog posts around about the best practices for log messages. I’ve borrowed this from:

http://www.ludovicocaldara.net/dba/bash-tips-4-use-logging-levels/
http://www.ludovicocaldara.net/dba/bash-tips-5-output-logfile/

My favorite (it wasn’t, now it is…) solution is to automatically open a pipe that will receive from the standard output and redirect to the logfile. With this solution, I can programmatically define my logfile name inside the script (based on the script name and input parameters for example) and forget about redirecting the output everytime that I run a command.

If you couple the verbosity level with input parameters you can have something quite clever (e.g. -s for silent, -V for verbose, -G for debug). The -Z parameter can be used to intentionally avoid logging. The edumpvar is handy to have the status of several variables at once:

Examples

#!/bin/bash
source please_log.sh

Log_Open
echo "whatever I execute here will be logged to $LOGFILE"
Log_Close

See the end of this article for the please_log.sh gist

default

log.sh

2020-03-24 23:02:28 - WARNING - this is a warning
2020-03-24 23:02:28 - ERROR --- this is an error
2020-03-24 23:02:28 - FATAL --- CRITICAL MESSAGE!
2020-03-24 23:02:28 - Logging to /home/blaataap/temp/log/log_whatever_I_want_20200324_230228.log

silent

log.sh -s

whatever I execute here will be logged to /home/blaataap/temp/log/log_whatever_I_want_20200324_230247.log

verbose

log.sh -V

2020-03-24 23:02:13 - WARNING - this is a warning
2020-03-24 23:02:13 - ERROR --- this is an error
2020-03-24 23:02:13 - INFO ---- this is an information
2020-03-24 23:02:13 - FATAL --- CRITICAL MESSAGE!
2020-03-24 23:02:13 - Logging to /home/blaataap/temp/log/log_whatever_I_want_20200324_230213.log
whatever I execute here will be logged to /home/blaataap/temp/log/log_whatever_I_want_20200324_230213.log

debug

log.sh -G

2020-03-24 23:10:54 - DEBUG --- -G specified: Debug mode
2020-03-24 23:10:54 - WARNING - this is a warning
2020-03-24 23:10:54 - ERROR --- this is an error
2020-03-24 23:10:54 - INFO ---- this is an information
2020-03-24 23:10:54 - DEBUG --- debugging
2020-03-24 23:10:54 - FATAL --- CRITICAL MESSAGE!
2020-03-24 23:10:54 - DEBUG --- HOSTNAME=ThinkPad-T580
2020-03-24 23:10:54 - Logging to /home/blaataap/temp/log/log_whatever_I_want_20200324_231054.log
whatever I execute here will be logged to /home/blaataap/temp/log/log_whatever_I_want_20200324_231054.log

no logfile

log.sh -Z

2020-03-24 23:17:05 - WARNING - this is a warning
2020-03-24 23:17:05 - ERROR --- this is an error
2020-03-24 23:17:05 - FATAL --- CRITICAL MESSAGE!
whatever I execute here will be logged to
Last updated on 31 Jan 2021
Published on 24 Mar 2020
Edit on GitHub