Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

logging and cron jobs

by InfiniteLoop (Hermit)
on Oct 03, 2005 at 14:48 UTC ( [id://496941]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings Monks,

In my application there are few custom scripts, which are run by the cron daemon. I plan to implement some sort of activity/error log for each of these scripts.

Currently, for all errors and any high level activity (such as: deleting unused files etc), I do a:
print STDERR <message>;
and in the crontab I redirect errors to a file.

My manager wants a neater solution. So I looked up cpan and found log4perl module. I have couple of questions on using it.
  • As I understand, the log4perl will open a file handle to a log file and write into it. Some of the scripts, are run frequently and there might be cases of same script running as more than one process. In case of multiple processes, I fear the log file will be corrupted. Are my fears unfounded ?
  • Also I would like your suggestion on other logger module(s).

Replies are listed 'Best First'.
Re: logging and cron jobs
by philcrow (Priest) on Oct 03, 2005 at 14:51 UTC
    We use the syslog on Linux. It takes care of opening and closing the logs, ensuring that data is not lost. The Perl module is Sys::Syslog. I doubt this will work on non-unix systems.

    Phil

Re: logging and cron jobs
by adrianh (Chancellor) on Oct 03, 2005 at 16:49 UTC
    As I understand, the log4perl will open a file handle to a log file and write into it. Some of the scripts, are run frequently and there might be cases of same script running as more than one process. In case of multiple processes, I fear the log file will be corrupted. Are my fears unfounded ?

    If you use Log::Log4perl::Appender::Synchronized then you shouldn't have any problems - it solves exactly this problem.

Re: logging and cron jobs
by Moron (Curate) on Oct 03, 2005 at 15:10 UTC
    It all depends on what a neater solution means. I would try to understand the functional complaint before seeking technical alternatives.

    In this case, the issue is likely to be the lack of modularity of your solution, in the sense that you should be writing a logging subroutine that meets your manager's needs. Try to complete the list of requirements first, such as whether it needs a logfile naming convention (also creating required separation) to include timestamping, process identity, stage identity, file identity or whatever is functionally required. Design your own logging subroutine accordingly and get your manager to agree to it before coding it - you might find there are also site-specific requirements for how the logging routine is installed in a (site-specific) module as well as other project standards that need to be maintained.

    -M

    Free your mind

Re: logging and cron jobs
by Catharsis (Initiate) on Oct 03, 2005 at 16:19 UTC

    in Log4perl you can do one log file per script by using a perl hook..

    So in your log4perl config do..

    log4perl.logger = DEBUG,LOGFILE 
    log4perl.appender.LOGFILE = Log::Log4perl::Appender::File
    log4perl.appender.LOGFILE.filename = sub { return '/tmp/log.'.$$.'.log' }
    log4perl.appender.LOGFILE.layout = Log::Log4perl::Layout::PatternLayout
    log4perl.appender.LOGFILE.layout.ConversionPattern =%d %c - %m%n

    And to print out WHERE thats logging to do..

    print Log::Log4perl->appender_by_name('LOGFILE')->filename();
Re: logging and cron jobs
by idsfa (Vicar) on Oct 03, 2005 at 15:15 UTC

    Ask your manager to define "neater".

    Separate activity logs for each run is very tidy and well organized. Shoehorning multiple, concurrent runs into a single log file is hard to follow even if you don't have corruption issues.

    Maybe you just need to have:

    * 0 * * * cat /tmplogdir/date/temp*.log >> /logdir/activity.log

    In the crontab as well


    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. -- Cyrus H. Gordon
Re: logging and cron jobs
by sgifford (Prior) on Oct 03, 2005 at 15:59 UTC
    One way to handle this is to create a seperate log file for every run of the program, using the time and PID to create a filename. To look through some or all of the logfiles, you can then use the shell's wildcards.

    For example, if you created logfiles named yourapp.yyyy-mm-dd-hh-mm-ss.pid, you could look at all the logs from 2005 with something like cat yourapp.2005-*.

    You could do this with a small wrapper program to manage the logfiles and run your program with STDERR connected to a pipe the wrapper program reads; that would require no changes to your current code.

Re: logging and cron jobs
by adrianh (Chancellor) on Oct 03, 2005 at 16:55 UTC
    Also I would like your suggestion on other logger module(s).

    If you're just logging to files TheDamian's Log::StdLog is nice solution.

Re: logging and cron jobs
by kprasanna_79 (Hermit) on Oct 03, 2005 at 16:50 UTC
    Hi infiniteloop,

    If you want to avoid STDERR word when ever you want redirect error to error file, you can tie a filehandle to a module where you can define print function.

    untie *STDOUT; $log = tie(*STDOUT, 'module_name', "logfile_name") Inside the module sub print { my ($class, @data) = @_; print $class->{handle} = @data; . . .

    If you want to redirect both STDOUT and STDERR to file, please redirect these to a file.You can set the log file in runtime also.

    open STDOUT, ">>$logfile"; open STDERR, ">&STDOUT";

    Tie this STDOUT to the module where you have implemented this print function.

    I think this resloves your issues.
    -Prasanna.K

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://496941]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-03-29 07:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found