dynamic timestamps in log

dynamic timestamps in log

Post by thomasriis » Fri, 23 Jun 2006 18:32:13



Hi guys,

I've got a rather big shell script going now.

A part of it contains a while loop, which can run up to five times,
depending on various elements.

At the top of the script I have defined three variables, that gives me
a good looking and logical log, they are:

log=/somefile.log

info="INFO------`date +%d/%m/%y' '%T` >"
warn="WARN------`date +%d/%m/%y' '%T` >"
err="ERROR-----`date +%d/%m/%y' '%T` >"

When I later on in the scripts append info to the log, I e.g. write:

echo "$info There is a new file today, continuing\r" >>$log

THE PROBLEM is, that all log entries gets the same timestamp. Even in
the while loop, where the script sleeps for five minutes up to six
times.

How do I make a "dynamic" timestamp? I have tried to make a function,
but no gain. I would still like to write my output to the log, as
showed above.

Thanks.

 
 
 

dynamic timestamps in log

Post by Stephane Chazela » Fri, 23 Jun 2006 19:13:45



[...]
Quote:> info="INFO------`date +%d/%m/%y' '%T` >"
> warn="WARN------`date +%d/%m/%y' '%T` >"
> err="ERROR-----`date +%d/%m/%y' '%T` >"

> When I later on in the scripts append info to the log, I e.g. write:

> echo "$info There is a new file today, continuing\r" >>$log

> THE PROBLEM is, that all log entries gets the same timestamp. Even in
> the while loop, where the script sleeps for five minutes up to six
> times.

[...]

use functions:

info() {
  printf 'INFO----%s > %s\n' "$(
    date '+%Y-%m-%d %T')" "$*" >> "$log"

Quote:}

(the above assumes the first character of $IFS is a " " or that
$IFS is unset).

--
Stephane

 
 
 

dynamic timestamps in log

Post by thomasriis » Fri, 23 Jun 2006 19:31:36


Quote:> use functions:

> info() {
>   printf 'INFO----%s > %s\n' "$(
>     date '+%Y-%m-%d %T')" "$*" >> "$log"
> }

It works. But that means I have to change all my echo's to the log...
 
 
 

dynamic timestamps in log

Post by Stephane Chazela » Fri, 23 Jun 2006 19:45:01



Quote:>> use functions:

>> info() {
>>   printf 'INFO----%s > %s\n' "$(
>>     date '+%Y-%m-%d %T')" "$*" >> "$log"
>> }

> It works. But that means I have to change all my echo's to the log...

Yes, use your editor search and replace facility.

:%s/echo "\$info/info "/g

in vi.

--
Stephane

 
 
 

dynamic timestamps in log

Post by thomasriis » Fri, 23 Jun 2006 21:05:24




> >> use functions:

> >> info() {
> >>   printf 'INFO----%s > %s\n' "$(
> >>     date '+%Y-%m-%d %T')" "$*" >> "$log"
> >> }

> > It works. But that means I have to change all my echo's to the log...

> Yes, use your editor search and replace facility.

> :%s/echo "\$info/info "/g

> in vi.

> --
> Stephane

Excactly what I did. Thanks.
 
 
 

dynamic timestamps in log

Post by kenneth kah » Sat, 24 Jun 2006 00:58:50


If you change

    info="INFO------`date +%d/%m/%y' '%T` >"

to

    info='INFO------`date +%d/%m/%y" "%T` \>'

You can then use

    eval echo "$info There is a new file today, continuing\\\r" >>$log

and this will do what you want.

Kenneth Kahn

 
 
 

dynamic timestamps in log

Post by thomasriis » Sat, 24 Jun 2006 16:48:32



> If you change

>     info="INFO------`date +%d/%m/%y' '%T` >"

> to

>     info='INFO------`date +%d/%m/%y" "%T` \>'

> You can then use

>     eval echo "$info There is a new file today, continuing\\\r" >>$log

> and this will do what you want.

> Kenneth Kahn


- that's very cool! Thanks so much!
 
 
 

dynamic timestamps in log

Post by Dan Merce » Sun, 25 Jun 2006 08:05:22


: Hi guys,
:
: I've got a rather big shell script going now.
:
: A part of it contains a while loop, which can run up to five times,
: depending on various elements.
:
: At the top of the script I have defined three variables, that gives me
: a good looking and logical log, they are:
:
: log=/somefile.log
:
: info="INFO------`date +%d/%m/%y' '%T` >"
: warn="WARN------`date +%d/%m/%y' '%T` >"
: err="ERROR-----`date +%d/%m/%y' '%T` >"
:
: When I later on in the scripts append info to the log, I e.g. write:
:
: echo "$info There is a new file today, continuing\r" >>$log
:
: THE PROBLEM is, that all log entries gets the same timestamp. Even in
: the while loop, where the script sleeps for five minutes up to six
: times.
:
: How do I make a "dynamic" timestamp? I have tried to make a function,
: but no gain. I would still like to write my output to the log, as
: showed above.
:
: Thanks.
:

In ksh93 you use a discipline function:

typeset info warn err

function info.get {
.sh.value=$(date '+INFO------%d/%m/%y %T >')"
# date can hold any arbitrary text including newlines (%n)

Quote:}

Anytime $info is evaluated then the function will run\

Dan Mercer

 
 
 

1. Need HELP to Log User Log-ins form the internet

Hi,

I've setup a FreeBSD 4.1.1-STABLE box to connect a network to the internet
with natd and ipfw firewall.
I've also setup the FreeBSD box to let teleworkers log in with FTP and
telnet.

Now I would like to log FTP and telnet Log-in's from teleworkers who connect
to the machine from the internet.
I woul like to see the time and IP numer from which users Log-in.

I have looked at the /var/log/messages file but this only shows SU Login's.

All help is greatly appriciated!

Luke

2. Colorado Jumbo125 questions

3. dir timestamps vs dir/file timestamps

4. `reboot` doesn't

5. Syntax for apending a timestamp to a cron job log?

6. Reason to go with Linux

7. HELP ! timestamp in log file ???????

8. INN expires and FreeBSD 2.1R

9. named does not log correct timestamps

10. Adding timestamps to log files

11. a logging shell script with timestamps

12. Log with timestamp?

13. Logging with timestamp