Email VPS log files every day

Handwritten on 06 May 2019

Email VPS log files every day

When you shipped your app to production you want to stay informed about potential errors that occured. I had this need, I wanted to start my working days by reviewing only errors in my application log. Really, there are plenty logging solutions that provide all kind of useful monitoring and measuring. Joris Zwart said it should be doable with a oneliner.

Yes, it can be:

grep -E '\[error|warn\]' -i /var/log/app/application.`date -d "1 day ago" '+%Y%m%d'`.log

What it does:

  • grep only errors and warnings
  • from your logging directory
  • but only log files of yesterday: date -d "1 day ago" '+%Y%m%d'

The result of this grep command you can pipe to mail or POST-it somewhere. Full example if you want to email it:

grep -E '\[error|warn\]' -i /var/log/app/application.`date -d "1 day ago" '+%Y%m%d'`.log | mail -s "`date -d "1 day ago" '+%Y%m%d'` Goodmorning: Warning and Error logs" -r "from@appdomain.com" "hello@adis.me"

Bonus points: You can paste this shell script inside your daily cron jobs: /etc/cron.daily. This way you will get a daily mail with errors. Enjoy 👍🏻

log terminal

© 2019 Handcrafted in Nijmegen