3 =head1 NAME $RCSfile: Logger.pm,v $
5 Object oriented interface to handling logfiles
13 Andrew DeFaria <Andrew@ClearSCM.com>
21 Fri Mar 12 10:17:44 PST 2004
25 $Date: 2012/01/06 22:00:09 $
31 Perl module for consistent creation and writing to logfiles
39 $log->msg ("This message might appear on STDOUT");
40 $log->log ("Stuff this message into the logfile");
42 if (!$log->logcmd ("ls /non-existant-dir")) {
43 $log->err ("Unable to proceed", 1);
47 to => "Andrew\@ClearSCM.com",
48 subject => "Logger test",
49 heading => "Results of Logging"
54 Logger creates a log object that provides easy methods to log messages, errors,
55 commands, etc. to log files. Logfiles can be created as being transient in that
56 they will automatically disappear (unless you call the err method). You can
57 capture the output of commands into log files and even have them autoamatically
58 timestamped. Finally you can have logfiles automatically mailed.
62 The following routines are exported:
84 my ($error_color, $warning_color, $command_color, $highlight_color, $normal) = "";
89 # Extract relative path and basename from script name.
90 $me = $FindBin::Script;
92 # Remove .pl for Perl scripts that have that extension
97 my ($class, %parms) = @_;
103 Construct a new Logger object. The following OO style arguments are
108 =for html <blockquote>
114 Name of the leaf portion of the log file. Default is the name of the
115 script with ".log" appended to the logfile name. So if the calling
116 script was called "getdb" the default log file would be called
117 "getdb.log" (Default: Script name).
121 Path to create the logfile in (Default: Current working directory)
125 One of "temp" or "perm". Logfiles that are of disposition temp will be
126 deleted when the process ends unless any calls have been made to the
127 err method (Default: perm)
131 If set to 0 then no timestamps will be used. If set to 1 then all
132 lines logged will be preceeded with a timestamp (Default: 0)
136 If defined the logfile will be appended to (Default: Overwrite)
140 If defined an alternate extension to use for the log file (e.g. log.html)
144 =for html </blockquote>
148 =for html <blockquote>
156 =for html </blockquote>
162 my $name = $parms{name} ? $parms{name} : $me;
163 my $path = $parms{path} ? $parms{path} : $cwd;
164 my $disposition = $parms{disposition} ? $parms{disposition} : 'perm';
165 my $timestamped = $parms{timestamped} ? $parms{timestamped} : 'FALSE';
166 my $append = $parms{append} ? '>>' : '>';
169 if (defined $parms{extension}) {
170 $name .= ".$parms{extension}" unless $parms{extension} eq '';
173 open $logfile, $append, "$path/$name"
174 or error "Unable to open logfile $path/$name - $!", 1;
176 # Set unbuffered output
177 $logfile->autoflush ();
179 set_verbose if $ENV{VERBOSE};
180 set_debug if $ENV{DEBUG};
186 timestamped => $parms {timestamped},
187 disposition => $disposition,
194 my ($self, $filename) = @_;
198 =head3 append ($filename)
200 Appends $filename to the end of the current logfile
204 =for html <blockquote>
210 Filename to append to the logfile
214 =for html </blockquote>
218 =for html <blockquote>
226 =for html </blockquote>
230 open my $file, '<', $filename
249 Returns the leaf portion of logfile name.
253 =for html <blockquote>
261 =for html </blockquote>
265 =for html <blockquote>
269 =item Leaf node of log file name
273 =for html </blockquote>
277 return $self->{name};
287 Returns the full pathname to the logfile
291 =for html <blockquote>
299 =for html </blockquote>
303 =for html <blockquote>
307 =item Full pathname to the logfile
311 =for html </blockquote>
315 return "$self->{path}/$self->{name}";
319 my ($self, $msg, $nolinefeed) = @_;
323 =head3 msg ($msg, $nolinefeed)
325 Similar to log except verbose (See Display.pm) is used to possibly
326 additionally write the $msg to STDOUT.
330 =for html <blockquote>
340 If defined no linefeed is displayed at the end of the message.
344 =for html </blockquote>
348 =for html <blockquote>
356 =for html </blockquote>
360 $self->log ($msg, $nolinefeed);
362 verbose $msg, undef, $nolinefeed;
368 my ($self, $msg, $nolinefeed) = @_;
372 =head3 disp ($msg, $nolinefeed)
374 Similar to log except display (See Display.pm) is used to write the $msg to
375 STDOUT and to the log file.
379 =for html <blockquote>
389 If defined no linefeed is displayed at the end of the message.
393 =for html </blockquote>
397 =for html <blockquote>
405 =for html </blockquote>
409 $self->log ($msg, $nolinefeed);
411 display $msg, undef, $nolinefeed;
416 sub incrementErr (;$) {
417 my ($self, $increment) = @_;
421 =head3 incrementErr ($msg, $errno)
423 Increments the error count by $increment
427 =for html <blockquote>
433 Amount to increment (Default: 1)
437 =for html </blockquote>
441 =for html <blockquote>
449 =for html </blockquote>
455 $self->{errors} += $increment;
461 my ($self, $msg, $errno) = @_;
465 =head3 err ($msg, $errno)
467 Writes an error message to the log file. Error messages are prepended
468 with "ERROR" and optionally "#$errno" (if $errno is specified),
469 followed by the message. If $errno was specified then the string " -
470 terminating" is appended to the message. Otherwise the number of
471 errors in the log are incremented and used to determine the logfile's
472 disposition at close time.
476 =for html <blockquote>
486 Error number to display (also causes termination).
490 =for html </blockquote>
494 =for html <blockquote>
502 =for html </blockquote>
506 display_error ($msg, $errno);
509 $msg = "ERROR #$errno: $msg - terminating";
511 $msg = "ERROR: $msg";
518 exit $errno if $errno;
524 my ($self, %parms) = @_;
528 =head3 maillog (<parms>)
530 Mails the current logfile. "Parms" are the same as the parameters
531 described for Mail.pm.
535 =for html <blockquote>
541 Supports all parameters that Mail::mail supports.
545 =for html </blockquote>
549 =for html <blockquote>
557 =for html </blockquote>
561 my $from = $parms{from};
564 my $subject = $parms{subject};
565 my $heading = $parms{heading};
566 my $footing = $parms{footing};
567 my $mode = $parms{mode};
572 my $log_filename = "$self->{path}/$self->{name}";
574 open my $logfile, '<', $log_filename
575 or error "Unable to open logfile $log_filename", 1;
577 if ($mode eq 'html') {
578 $heading .= '<b>Logfile:</b> '
579 . "$self->{path}/$self->{name}"
581 $footing = '</pre><hr>'
597 or error "Unable to close logfile $log_filename", 1;
603 my ($self, $msg, $nolinefeed) = @_;
607 =head3 log ($msg, $nolinefeed)
609 Writes $msg to the log file. Note this is a "silent" log in that $msg
610 is simply written to the logfile and not possibly also echoed to
611 STDOUT (See the msg method).
615 =for html <blockquote>
621 Message to write to log file
625 If defined no linefeed is displayed at the end of the message.
629 =for html </blockquote>
633 =for html <blockquote>
641 =for html </blockquote>
645 $msg = "$me: " . YMDHM . ": $msg" if $self->{timestamped};
647 display $msg, $self->{handle}, $nolinefeed;
653 my ($self, $cmd) = @_;
659 Execute the command in $cmd storing all output into the logfile
661 =for html <blockquote>
667 The command $cmd is executed with the results logged to the logfile.
671 =for html </blockquote>
675 =for html <blockquote>
679 =item Scalar representing the exit status of $cmd and an array of the commands output.
683 =for html </blockquote>
687 display "\$ $cmd", $self->{handle} if get_debug;
689 my $status = open my $output, '-|', "$cmd 2>&1";
701 display $_, $self->{handle};
702 display $_ if get_debug;
706 or error "Unable to close output ($!)", 1;
708 return ($?, @output);
718 Returns an array of lines from the current logfile.
722 =for html <blockquote>
730 =for html </blockquote>
734 =for html <blockquote>
738 =item Array of lines from the logfile
742 =for html </blockquote>
746 return ReadFile "$self->{path}/$self->{name}";
750 my ($self, $msg, $warnno) = @_;
754 =head3 warn ($msg, $warnno)
756 Similar to error but logs the message as a warning. Increments the
757 warnings count in the object thus also affecting its disposition at
758 close time. Does not terminate the process if $warnno is specified.
762 =for html <blockquote>
768 Message to write to the logfile
772 Warning number to put in the warn message (if specified)
776 =for html </blockquote>
780 =for html <blockquote>
788 =for html </blockquote>
792 warning $msg, $warnno;
795 $msg = "WARNING #$warnno: $msg";
797 $msg = "WARNING: $msg";
813 Returns the number of errors encountered
817 =for html <blockquote>
825 =for html </blockquote>
829 =for html <blockquote>
837 =for html </blockquote>
841 return $self->{errors};
851 Returns the number of warnings encountered
855 =for html <blockquote>
863 =for html </blockquote>
867 =for html <blockquote>
875 =for html </blockquote>
879 return $self->{warnings};
885 close ($self->{handle});
887 if ($self->{disposition} eq 'temp') {
888 if ($self->{errors} == 0 and
889 $self->{warnings} == 0) {
890 unlink $self->fullname;
901 =head2 CONFIGURATION AND ENVIRONMENT
903 DEBUG: If set then $debug in this module is set.
905 VERBOSE: If set then $verbose in this module is set.
915 =head3 ClearSCM Perl Modules
917 =for html <p><a href="/php/scm_man.php?file=lib/DateUtils.pm">DateUtils</a></p>
919 =for html <p><a href="/php/scm_man.php?file=lib/Display.pm">Display</a></p>
921 =for html <p><a href="/php/scm_man.php?file=lib/Mail.pm">Mail</a></p>
923 =for html <p><a href="/php/scm_man.php?file=lib/OSDep.pm">OSDep</a></p>
925 =for html <p><a href="/php/scm_man.php?file=lib/Utils.pm">Utils</a></p>
927 =head2 INCOMPATABILITIES
931 =head2 BUGS AND LIMITATIONS
933 There are no known bugs in this module.
935 Please report problems to Andrew DeFaria <Andrew@ClearSCM.com>.
937 =head2 LICENSE AND COPYRIGHT
939 This Perl Module is freely available; you can redistribute it and/or
940 modify it under the terms of the GNU General Public License as
941 published by the Free Software Foundation; either version 2 of the
942 License, or (at your option) any later version.
944 This Perl Module is distributed in the hope that it will be useful,
945 but WITHOUT ANY WARRANTY; without even the implied warranty of
946 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
947 General Public License (L<http://www.gnu.org/copyleft/gpl.html>) for more
950 You should have received a copy of the GNU General Public License
951 along with this Perl Module; if not, write to the Free Software Foundation,
952 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.