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

I am working on a webapp containing several hundred scripts, many of which are no longer being used. I would like to record normal usage patterns to determine what to get rid of,and what to optomize. Toward this end I would like to generate some statistics and insert them into a logfile every time a program is run... such as program name, execution time, timestamp, etc.

I can get some of this from the apache logs, but there are a lot that are run from crontab and called from other scripts. Is there a way to turn on some form of logging in perl, or have it run a script before and after each program is executed?

Replies are listed 'Best First'.
Re: perl program execution log.
by chromatic (Archbishop) on Nov 16, 2005 at 22:50 UTC

    You could write a tiny module that writes the value of $0 to a central log file somewhere and add -MTraceProg to PERL5OPT. Just be sure that you set the environment variable appropriately for however you launch programs -- from cron, from Apache/mod_perl, or from the command line.

Re: perl program execution log.
by BrowserUk (Patriarch) on Nov 16, 2005 at 23:05 UTC

    If all your script use strict, you could add a pair of BEGIN and END blocks to the top of that module.

    As it is (usually) one of the first modules loaded, it would BEGIN very early and END very late and so give you pretty accurate timing info.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: perl program execution log.
by thor (Priest) on Nov 16, 2005 at 22:48 UTC
    The only thing that I can think of is to replace your perl executable with a shell script that logs to a common location and then executes the script that was passed in. I don't know how well that would work with shebang lines and it's not the most elegant solution, but it's a start.

    thor

    Feel the white light, the light within
    Be your own disciple, fan the sparks of will
    For all of us waiting, your kingdom will come

Re: perl program execution log.
by tirwhan (Abbot) on Nov 16, 2005 at 22:57 UTC

    A plethora of modules for logging exist, just search CPAN for "Log" and you'll find them (there are various pros and cons to each of them, which you'll be in the best position to assess).

    The easiest way to log from scripts which are run via cron is to just print logging messages to STDOUT or STDERR and redirect the messages to a log file. For example (crontab entry):

    0 * * * * /usr/local/bin/myscript >> /var/log/myscript.log 2>&1
    This will redirect all output (STDOUT and STDERR) to /var/log/myscript.log

    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan