sajanagr has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

I am trying to create my own log in /var/log/httpd/test.log.

I have a Perl script which echos the process of the code (success/failure) in this file. But, first I am not able to echo into this particular file, though it does in any other file if done in /tmp. I checked for permissions, but it seems correct.

Second, is using ECHO command the right way to log my files? Please guide through creating a new log in an efficent way.

Replies are listed 'Best First'.
Re: To create a custom log
by moritz (Cardinal) on Aug 20, 2010 at 09:22 UTC
    Perl has no ECHO command. Try open and print instead.
    Perl 6 - links to (nearly) everything that is Perl 6.
      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.

        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.

        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

Re: To create a custom log
by pemungkah (Priest) on Aug 20, 2010 at 20:13 UTC
    I'd suggest going with Log::Log4perl. Handles logging much more like syslog, and is easy to use.
    use Log::Log4perl qw(:easy); Log::Log4perl->easy_init($ERROR); DEBUG "This doesn't go anywhere"; ERROR "This gets logged";
    Note the ability to set a log level (DEBUG is, I think, the default, but Log4perl has great docs). One hint: if you will be using Data::Dumper in a call to DEBUG, for instance, wrap it in an anonymous sub; that way Log4perl can skip doing the Dumper call altogether if DEBUG logging is off.
      Thank you all for your reply.

      But, the problem of logging is different now in my implementation.

      I have a server-client code based on Apache.

      I tried to create a custom log using the log module of Perl, but I didn't have permission to write to the file.

      While exploring, I did get Apache2::log module, which writes the error into error.log file of httpd properly.

      The problem now, is how can I have my own log file.

      Please help.