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?
One way is to use logger (1) (usually
/bin/logger
), and simply send everything to syslog: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.