How do I add additional information to output redirected to a log file?

2

Currently I have something like this:

./player_script |grep "videoname" >> log.log &

The output of player_script shows when a video starts and ends, grep then takes only certain video file names and puts them into log.log.

So log.log will look something like this:

video <videoname> is playing now
video <videoname> ended
video <videoname> is playing now
video <videoname> ended

... and so on.

What I need is the timestamp of when the file starts and ends on each new line, in this format:

Feb 28 15:35:32 video <videoname> is playing now

I also need the date on the log file: log2012-02-29.log.

This is being run on Solaris 5.8 & 5.9.

reyes

Posted 2012-02-29T00:40:04.533

Reputation: 117

Let me see if I understand the first part. You want to add to the output of log.log a timestamp of when each video start and stops? If so, that's something that the video player script will have to do. That being said, perhaps you can post your player_script and we can take a look under the hood. – Carlos – 2012-02-29T01:48:24.753

Answers

3

To answer your 2nd part, it's pretty easy. Instead of log.log, use the following:

log`date '+%Y-%m-%d'`.log

Pay particular attention that there are 'backticks' that wrap the date command. That tells the shell to execute that first and plaster the results onto your command line to make it a part of your filename.

Carlos

Posted 2012-02-29T00:40:04.533

Reputation: 799

0

For part one (a bit of a shot in the dark since I only have access to Solaris 11), a sh script solution:

./player_script | grep "videoname" | while read line;
     do echo `date '+%Y-%m-%d:%H:%M:%s'` $line;
     done >> log`date '+%Y-%m-%d'`.log

Note that it's easier to sort multiple log lines by date if you use a format such as +%Y-%m-%d...

You'll also have to be careful that player_script or grep doesn't buffer data if you want accurate timestamps on your log entries.

Andrew Henle

Posted 2012-02-29T00:40:04.533

Reputation: 200