4

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?

3
  • @Nils You linked to the question, not a specific answer. Furthermore all those examples only appear to be working with output on stdout, which I do not have. This is getting close (needs adding a timestamp). Please also note that I updated the question.
    – dirty bit
    Commented Sep 30, 2011 at 20:23
  • I was talking about the adddate() answer. But yes - that is taking stdout. Is there any special reason why the program can't use the syslog mechanism?
    – Nils
    Commented Oct 1, 2011 at 19:55

2 Answers 2

4

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
2
  • I tried doing that but ran into a few problems; To test whether things work, I use tail -F foo.log (is that alright or does it cause more trouble?). The program sometimes reopens the logfile. It seems like the output from tail is buffered in some way. Furthermore the program seems to freeze when reopening the logfile (it does not react to input on stdin anymore).
    – dirty bit
    Commented Sep 30, 2011 at 19:00
  • In addition to my previous comment; See my revised question. The program recreates the file, which most likely causes the script as well as tail to fail, since there's no more fifo to read from.
    – dirty bit
    Commented Sep 30, 2011 at 20:28
1

Based on the syslog-idea in my comment:

tail -n1 -q -f --retry $YOURFILE 2>/dev/null |logger

This seems to work with CentOS 5. See you local man-pages for tail and logger.

In CentOS there is a warning about using "--retry" wich goes to stderr and has to be suppressed.

With logger you can send messages to syslog - which in turn will normally prepend a time stamp.

2
  • logger do not add time stamp. it is added by syslog after message is arrived.
    – Znik
    Commented Mar 5, 2019 at 12:52
  • @Znik "which" references "syslog" so what?
    – Nils
    Commented Mar 21, 2019 at 9:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .