Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Alex, short for Alexander, is a project I'm working on which involves a lot of batch processing jobs. The Boss wanted the logging information to go to the MySQL database instead of plain files, so I could make a pretty web interface for him to monitor the progress of various backend processes. (Yeah, I could have made a pretty web interface for flat files also, but such is The Boss.) Many log systems have different levels to indicate the severity of a message (or whether to even log it at all.) This system started out that way, but soon the "levels" really changed into "categories" which were not necessarily good or bad, so The Boss could run SQL queries against the log table to get information about a particular category of messages. I decided to experiment with closures and auto-generation of methods, and put all the categories into the database, so The Boss could add new ones himself. Upon instantiation, my log object would build a method for each category it found in the database. It works perfectly, but I'd like to hear any comments as this is the first time I've deployed something with dynamic methods on a large scale.

I'm sure there are also some CPAN modules that do this kind of thing, but I was in the mood for experimentation and it only took a few minutes to write. (And it worked right the first time, always a nice feeling.)

package Alex::Log; use strict; use warnings; use lib '/usr/local/alex/lib'; use Carp qw(croak); use Alex::DBI; sub new { my $class = shift; my %args = @_; croak "No program name given to constructor. ( program => 'foo' required )" unless exists $args{program}; my $pname = $args{program}; my $dbh = Alex::DBI->new; my ( $pid ) = $dbh->selectrow_array( "SELECT id FROM Log_Programs WHERE name = '$pname'" ); croak "Program $pname not in database" unless $pid; my $levels = $dbh->selectall_arrayref( "SELECT id, name FROM Log_Levels" ); foreach my $lev( @$levels ) { # install dynamic method my $lid = $lev->[0]; my $name = $lev->[1]; no strict 'refs'; *{ "Alex::Log::$name" } = sub { my $self = shift; my $msg = shift; # truncate msg if needed. if ( length( $msg ) > 2048 ) { $msg = substr $msg, 0, 2048; } $msg = "[PID:$$] " . $msg; my $sth = $dbh->prepare( "INSERT INTO Logs ( log_level_id, log_program_id, message ) VALUES ( ?, ?, ? )" ); $sth->execute( $lid, $pid, $msg ); }; } return bless { }, $class; } 1;

Thanks for looking.


In reply to RFC: Alex::Log by friedo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2024-03-28 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found