How to add a timestamp to each line of a logfile

logspipe

I have an external program which I can not edit. It writes its log to a file.

How do I add a timestamp to each line while the program writes to the logfile?
It does not write its output to stdout. The timestamp should at least include seconds. Preferably only using standard linux tools such as pipes, bash, cron etc.

After testing a few example scripts, I noticed that the program closes the logfile to reload its configuration, then it recreates the same file as a plain empty textfile. I guess this means one has to use another approach such as continously (cron) rechecking the file contents?

Best Answer

Can you create a fifo and configure your program to write its log in it?

If yes, create the fifo and write a simple shell script that reads from that and writes to log file pre-pending a time-stamp. Something like:

#!/bin/sh
FIFOFILE=/tmp/program_log.fifo
LOGFILE=/var/log/program.log

mkfifo $FIFOFILE
awk '{printf("%s - %s\n", systime(), $0);}' < $FIFOFILE > $LOGFILE

second version

If your program does log rotation and periodically deletes the fifo, you have to use another way.

You can use tail to monitor the program log file. Note that tail checks periodically the file so (if you are very unlucky) you could lose some log line.

#!/bin/sh
PROGRAMLOGFILE=/tmp/program.log
MARKEDLOGFILE=/var/log/program.log

tail -F $PROGRAMLOGFILE \
    | awk '{printf("%s - %s\n", systime(), $0);}' \
    > $MARKEDLOGFILE
Related Question