1

How to prefix all logs with date time before they are written to the log file?

Let's say I am logging unexpected errors in crontab in the following way:

python somescript.py >> /tmp/output.log 2>&1

Is there an easy way to prefix (perhaps by piping through some third party command) all lines in the output.log with the date time so I would know when a given error happened?

Datageek
  • 161
  • 5
  • 1
    Maybe some of the answers from this will apply to you? http://serverfault.com/questions/80749/ping-replacement-that-shows-real-time – Zoredache Feb 16 '12 at 00:40

1 Answers1

2

One way is to use logger (1) (usually /bin/logger), and simply send everything to syslog:

python somescript.py | logger -t somescript.py

Syslog will automatically prepend the time/date to the log entry for you, and write this information to /var/log/messages (or whatever is specified in /etc/syslog.conf).

And this way, you have the advantage of all of the normal syslog/logrotation utilities.

Stefan Lasiewski
  • 23,667
  • 41
  • 132
  • 186
  • 2
    And if a central syslog server is being used, you have backups and a secure logging environment to boot. – Mei Feb 16 '12 at 02:02