Adding Timestamps to Arbitrary Logs
I was recently looking at a log file generated by a cron job and was frustrated to discover that the log didn’t have timestamps. After a little thought, I figured out how to make sure all future logs from that cron job have timestamps.
In my case, the cron job was a Let’s Encrypt renewal:
/opt/letsencrypt/letsencrypt-auto renew >> /var/log/le-renew.log
Since I’m not in control of the output of letsencrypt-auto renew
, I need to insert the timestamps. Using a Bash while
loop solves this easily:
/opt/letsencrypt/letsencrypt-auto renew | while read -r l; do echo "$(date) $l"; done >> /var/log/le-renew.log
The while -r l
reads the output line-by-line into the variable $l
, then prints it out, prefixing with the date
command.
Then it pipes everything into the same log.
You’ll get output like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
If you wanted to customize the date format, you could do it like this:
1
|
|
In the cron definition it would look like this:
/opt/letsencrypt/letsencrypt-auto renew | while read -r l; do echo "$(date "+%Y-%m-%d %H:%M:%S") $l"; done >> /var/log/le-renew.log
If your cron command gets complicated enough, it might be worth moving it to a shell script that you invoke from the cron job.
1 2 3 4 5 |
|
That’s basically it…
You could easily prefix your logs with other things as well, but I’ll leave that as an exercise for the reader.