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
Tue Oct  3 19:01:03 EDT 2017 
Tue Oct  3 19:01:03 EDT 2017 -------------------------------------------------------------------------------
Tue Oct  3 19:01:03 EDT 2017 Processing /etc/letsencrypt/renewal/mimic.io.conf
Tue Oct  3 19:01:03 EDT 2017 -------------------------------------------------------------------------------
Tue Oct  3 19:01:03 EDT 2017 
Tue Oct  3 19:01:03 EDT 2017 -------------------------------------------------------------------------------
Tue Oct  3 19:01:03 EDT 2017 
Tue Oct  3 19:01:03 EDT 2017 The following certs are not due for renewal yet:
Tue Oct  3 19:01:03 EDT 2017 /etc/letsencrypt/live/mimic.io/fullchain.pem (skipped)
Tue Oct  3 19:01:03 EDT 2017 No renewals were attempted.
Tue Oct  3 19:01:03 EDT 2017 -------------------------------------------------------------------------------
Wed Oct  4 19:01:01 EDT 2017 Upgrading certbot-auto 0.18.2 to 0.19.0...
Wed Oct  4 19:01:01 EDT 2017 Replacing certbot-auto...

If you wanted to customize the date format, you could do it like this:

1
$(date "+%Y-%m-%d %H:%M:%S")

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
#!/bin/bash

/opt/letsencrypt/letsencrypt-auto renew | while read -r line; do
  echo "$(date "+%Y-%m-%d %H:%M:%S") $line"
done >> /var/log/le-renew.log

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.

I am now accepting new clients for part-time consulting and software development projects. Learn more

I haven't configured comments for this blog, but if you want to get in touch, you can find me on Twitter