in reply to Re: To create a custom log
in thread To create a custom log

Hi

I using system command 'echo' here. I didn't use open and print since I have to log multiple times at different functions I have. So, I will have to open, write my log and close it again multiple times. Is it advisable?

I thought echo would fulfill the same purpose..!!

Correct me if i am wrong.

Replies are listed 'Best First'.
Re^3: To create a custom log
by moritz (Cardinal) on Aug 20, 2010 at 09:32 UTC
    You can also leave the files open between multiple logging calls.

    Or you can use one of the many logging modules.

    One problem with system & echo is that you don't get a good feedback if the command went wrong.

    Perl 6 - links to (nearly) everything that is Perl 6.
      Is it safe to open the file for a long time until the entire process is completed?

      I am new in logging and never used any of the logging modules.

      What do you suggest the best approach is ?

        it's save, if you know what are you doing :-) (and few background, like buffering).
        And if you are not sure, you can use locks.
Re^3: To create a custom log
by roboticus (Chancellor) on Aug 20, 2010 at 14:53 UTC

    sajanagr:

    No, don't use the echo command from the shell. It's nearly always a bad idea. Just go ahead and do it with a logger package or with print statements.

    Anyway, I'd suggest that you go ahead and write your script to just print your logging information to the standard output. Then you can redirect the output to a file after you have it debugged. You can redirect it either by:

    • Redirect STDOUT at the beginning of your perl program, so the print statements will all point to the file, like so:
      #!/usr/bin/perl use strict; use warnings; print "before IO redirection\n"; close STDOUT; open STDOUT, ">", "/log/foo" or die $!; print "after IO redirection\n";
    • Execute the command from a shell, and tell the shell to do the redirection for you. You can do this even if you schedule the job with cron, Task Manager, etc. Just schedule a wrapper script like this:
      perl the_script.pl >/log/foo

    ...roboticus