A common arrangement is to have various logging levels set up, and then emit different amounts of logging depending on the logging level.
debug () { ${debug-:} "$0: $@" >&2; }
info () { ${info-:} "$0: $@" >&2; }
warn () { ${warn-:} "$0: $@" >&2; }
error () { ${error-:} "$0: $@" >&2; }
case $1 in
--debug) debug=echo; info=echo; warn=echo; error=1;;
--info) info=echo; warn=echo; error=1;;
--warn) warn=echo; error=echo;;
--error) error=echo;;
-*) echo "$0: unknown option '$1' -- aborting" >&2
exit 127;;
esac
shift
debug here we go
info starting
echo "Hello world"
warn the end is nigh
echo "Hi there"
error we die here
This is just a dead simple demo; you would probably want for it to be somewhat more secure and versatile, and have sensible default like running with --warn
(which implies --error
) if no level is explicitly specified.
The "comments" are no longer comments, and the output goes to standard error in this implementation; but as you can see, this also lets you put in "comments" which are actually useful when you are debugging, not just while reading the code.
You can make the commands (debug
, warn
etc) look more like comments by renaming them, perhaps to _dbg_
, _warn_
etc.