I've been working on some shell scripts to automate tasks like backups and emailing myself logs. The problem is, the logs end up being a bit chaotic and hard to read. I'm printing headers before certain commands to indicate what the script is doing, but I still find the output unattractive and unclear. For instance, my logs look like this:
Thu Jan 9 00:30:01 CST 2025 *****
Thu Jan 9 00:30:01 CST 2025 Waking up backup drive /dev/sdc. Giving it 90 seconds.
Thu Jan 9 00:30:01 CST 2025 *****
1+0 records in
1+0 records out
4096 bytes (4.1 kB, 4.0 KiB) copied, 0.000155533 s, 26.3 MB/s
Thu Jan 9 00:31:31 CST 2025 *****
Thu Jan 9 00:31:31 CST 2025 Backup disk size:
Thu Jan 9 00:31:31 CST 2025 *****
Filesystem 1M-blocks Used Available Use% Mounted on
/dev/sdc1 1906798 1370995 535803 72% /mnt/disks/backup1
I'm thinking about using special characters like ">>>>" or "*****" as prefixes for status updates to make them stand out more. I also considered managing stdin and stderr through file descriptors, but that seems complicated with multiple log files. Does anyone have tips or best practices for making log output more visually appealing and easy to read?
4 Answers
The structure of your log really depends on your specific needs. If you're worried about STDERR and STDOUT, try setting up functions to format the output. For example, you could make a `my_log` function to prepend formatted timestamps to your messages. Just call it like so:
my_log "Script started!"
This way, every log entry will have a clear timestamp, which might help with readability. Also, consider redirecting all output like this:
exec > "${LOGDIR}/${LOG}" 2>&1
I usually don't stress about readability much, just as long as I can grep through it later. If you're in a similar boat, consider adding some unique identifiers in your logs to make filtering easier!
If you're using systemd, think about creating a unit file for your script. That way, logs will automatically be formatted nicely in journalctl. If you're sticking with shell scripts, the `logger` command can add nice timestamps for you, which could improve your output a bit.
I'm not actually using `logger`. I'm looking for ways to make the status info in my script more readable without switching logging methods—just want some good formatting ideas!
I've implemented a system that uses clearly defined prefixes for success and errors. For successful commands, I print a checkmark, and for failures, I show a cross mark. Here's an example of how it looks:
Sun Jan 12 08:58:28 CST 2025 ***** My backup script started
Sun Jan 12 08:58:28 CST 2025 >>> Waking up backup drive /dev/sdc ✅
Sun Jan 12 08:58:38 CST 2025 >>> Backup disk size: 72%
Sun Jan 12 08:58:38 CST 2025 >>> Backing up main data ❌
Just adding visual cues like these can greatly enhance clarity in logs.
Thanks for the suggestion! I might just implement that kind of function. I'm more interested in how to make the actual log messages pop visually, like using specific characters or layouts.