prepend timestamp to continiously updating log file

Hi,

I have a process which outputs to a log.

Below is the code snippet:

 
process   &> $LOGFILE&

The log file keeps on updating whenever a transaction is processed.
The log file has a time stamp added so every time I kill the process and start the process a new log file is created with latest time stamp.

Because the log file would be growing forever, I would like to prepend time stamp for each line in the log file. I used the below way but instead of writing the log every time a transaction is processed, the time stamp is added to each line in the log after the kill the process.

 
process  | sed "s/^/$(date)/" &> $LOGFILE&

Can you let me know how do I prepend time stamp inside the log in real time?

Thank you!

You don't... I can't think of an effective way to do this which doesn't involve fixing the application to do what you want in the first place.

If you must 'intercept' the log, make a new log, don't keep editing the old one in place. How many thousands of times will the application be killed to do it your way?

tail -f logfile | while read LINE
do
        echo $(date) "$LINE"
done > newlogfile

@Corona688 - Your solution works if the log file which you took as input is already created. But in this case, the log file will be updating always whenever a transaction is processed.

Also, per your suggestion, I need to create another log file which I do not want to.

I want to create only once when the process runs and output the detail of process to log with time stamp before each line.

FYI, the command is inside a shell script and all this is desired when the script is executed.

And, I will be killing the process may be once in a day.

I badly misunderstood what you were saying, my apologies.

My suggestion would still function with a little modification however.

process | while read LINE
do
        echo "$(date) $LINE"
done > outputfile

Anything involving pipes may buffer unexpectedly, however; collecting lines until the buffer fills and you get a bunch of lines at once. Behavior may depend on your system, try it and see.

Thanks Corona688

It worked. I'll do some tests and see if there are any issues with pipes.

1 Like