Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
#!perl -w # Log.pm -- a simple logger which uses DBI and DBD::CSV to # handle all the file locking. All log entries are kept in # a text file, to make reading it much easier than if they # had been put into a real database. # copyright September 23, 2001 by Michael S. Joyce # michaeljoyce@telus.net # This code may be used and distributed under the same terms # as Perl. # TODO: # add an option to make database connections # happen with each call to logger(), instead of # maintaining the connection for the lifetime of # the program use DBI; use strict; package Logger; use vars qw($tbldefault $dbdefault); # default name for the log file $tbldefault="errlog"; # default directory for the log files $dbdefault='E:\\\\log'; # check to see if a log file exists, # and create it if it doesn't. sub tblexists { my $dbh = shift; my $tblname = shift; # $dbh->tables() is a new method in DBI version 1.09 unless (scalar grep {$_ eq $tblname} $dbh->tables()) { my $crtstr=<<ENDQ; CREATE TABLE $tblname( date CHAR(24), filename CHAR(24), lineno CHAR(8), message VARCHAR(256) ) ENDQ $dbh->do($crtstr) or die "creating table: $!\n$dbh->errstr"; } } # initialize the logger # put a statement handle and database handle # into self, as well as other potentially useful information sub initialize { my $self = shift; $self->{'tblname'}=shift || $tbldefault; $self->{'dbname'}=shift || $dbdefault; $self->{'constr'}="DBI:CSV:f_dir=$self->{'dbname'}"; my $dbh = DBI->connect($self->{'constr'}) or die "No Database Connection Made: $!\n $DBI::errstr\n"; $self->{'dbhandle'}=$dbh; tblexists($dbh, $self->{'tblname'}); my $instr="INSERT INTO $self->{'tblname'} VALUES (?,?,?,?)"; my $sth=$dbh->prepare($instr); $self->{'sthandle'}=$sth; } # simple constructor # most of the work is done in initialize() sub new { my $class = shift; my $self = {}; bless $self, $class; $self->initialize(@_); return $self; } # member function to log a message sub logger { my $self = shift; my $sth = $self->{'sthandle'}; my $time=localtime(); my $message = shift; my ($package, $filename, $line) = caller; $sth->execute($time, $filename, "line $line", $message); } # Destructor to cleanup database connections sub DESTROY { my $self = shift; my $dbh = $self->{'dbhandle'}; $dbh->disconnect(); } 1;

In reply to Perl module to handle simple logging by coolmichael

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 rifling through the Monastery: (4)
As of 2024-04-19 17:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found