Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
1  
Maybe some of the answers from this will apply to you? serverfault.com/questions/80749/… –  Zoredache Feb 16 '12 at 0:40
add comment

1 Answer

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.

share|improve this answer
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 2:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.