I've seen methods used similar to @terdon's. It's the beginnings of what higher level programming languages call loggers, and offer as full blown libraries, such as log4J (Java), log4Perl (Perl) etc.
You can get something similar using
set -x in Bash as you've mentioned but you can use it to turn up the debugging just a subset of commands by wrapping blocks of code with them like so.
$ set -x; cmd1; cmd2; set +x
Here's a one liner pattern you can use.
$ set -x; echo "hi" ;set +x
+ echo hi
+ set +x
You can wrap them like this for multiple commands in a script.
Most people are oblivious but Bash also has a log4* as well, Log4Bash. If you have more modest needs this might be worth the time to set it up.
log4bash is an attempt to have better logging for Bash scripts (i.e. make logging in Bash suck less).
Here are some examples of using log4bash.
log "This is regular log message... log and log_info do the same thing";
log_warning "Luke ... you turned off your targeting computer";
log_info "I have you now!";
log_success "You're all clear kid, now let's blow this thing and go home.";
log_error "One thing's for sure, we're all gonna be a lot thinner.";
# If you have figlet installed -- you'll see some big letters on the screen!
log_captains "What was in the captain's toilet?";
# If you have the "say" command (e.g. on a Mac)
log_speak "Resistance is futile";
If you want what I would classify as more of the full power of a log4* framework then I'd give Log4sh a try.
log4sh was originally developed to solve a logging problem I had in some of the production environments I have worked in where I either had too much logging, or not enough. Cron jobs in particular caused me the most headaches with their constant and annoying emails telling me that everything worked, or that nothing worked but not a detailed reason why. I now use log4sh in environments where logging from shell scripts is critical, but where I need more than just a simple "Hello, fix me!" type of logging message. If you like what you see, or have any suggestions on improvements, please feel free to drop me an email. If there is enough interest in the project, I will develop it further.
log4sh has been developed under the Bourne Again Shell (/bin/bash) on Linux, but great care has been taken to make sure it works under the default Bourne Shell of Solaris (/bin/sh) as this happens to be the primary production platform used by myself.
Log4sh supports several shells, not just Bash.
- Bourne Shell (sh)
- BASH - GNU Bourne Again SHell (bash)
- DASH (dash)
- Korn Shell (ksh)
- pdksh - the Public Domain Korn Shell (pdksh)
It's also been tested on several OSes, not just Linux.
- Cygwin (under Windows)
- FreeBSD (user supported)
- Linux (Gentoo, RedHat, Ubuntu)
- Mac OS X
- Solaris 8, 9, 10
Using a log4* framework will take some time to learn but it is worth it if you have more demanding needs from your logging. Log4sh makes use of a configuration file where you can define appenders and control the formatting for the output that will appear.
# log4sh example: Hello, world
# load log4sh (disabling properties file warning) and clear the default
LOG4SH_CONFIGURATION='none' . ./log4sh
# set the global logging level to INFO
# add and configure a FileAppender that outputs to STDERR, and activate the
appender_setType stderr FileAppender
appender_file_setFile stderr STDERR
# say Hello to the world
logger_info 'Hello, world'
Now when I run it:
INFO - Hello, world
NOTE: The above configures the appender as part of the code. If you like this can be extracted out into its own file,
Consult the excellent documentation for Log4sh if you need further details.