Basset::Logger - Logger object. Writes things to files.


Basset documentation Contained in the Basset distribution.

Index


Code Index:

NAME

Top

Basset::Logger - Logger object. Writes things to files.

AUTHOR

Top

Jim Thomason, jim@jimandkoka.com

DESCRIPTION

Top

my $logger = Basset::Logger->new( 'handle' => '/tmp/weasels.log' );

$logger->log("Weasels in the hen house!");

$logger->close();

Create a logger object, then log data to it, then close it when you're done. Easy as pie. Works beautifully in conjunction with Basset::NotificationCenter.

You will need to put a types entry into your conf file for

 logger=Basset::Logger

(or whatever center you're using)

ATTRIBUTES

Top

handle

The place you log to. Either a string (which will be opened in append mode) or a globref.

 $logger->handle('/path/to/log.log');
 open (LOG, ">>/path/to/log.log");
 $logger->handle(\*LOG);

closed
stamped
log

logs the item to the logger's handle.

 $logger->log("one val", "two vals", "three vals");

prints out one per line, tab indented on subsequent lines.

 one val
 	two vals
 	three vals


Basset documentation Contained in the Basset distribution.
package Basset::Logger;

#Basset::Logger, copyright and (c) 2004, 2006 James A Thomason III
#Basset::Logger is distributed under the terms of the Perl Artistic License.


$VERSION = '1.01';

use Basset::Object;
our @ISA = Basset::Object->pkg_for_type('object');

use strict;
use warnings;

__PACKAGE__->add_attr(['handle', '_isa_file_accessor']);

__PACKAGE__->add_attr('closed');

__PACKAGE__->add_attr('stamped');


sub init {
	return shift->SUPER::init(
		'closed' => 0,
		'stamped' => 1,
		@_
	);
};


# _file_accessor is a dumbed down version of the one in Mail::Bulkmail.
#
# _file_accessor is an internal accessor for accessing external information. Said external information can be a
# path to a file or a globref containing an already openned file handle. It will open up path/to/file strings and 
# create an internal filehandle. it also makes sure that all filehandles are piping hot.

sub _isa_file_accessor {
	my $pkg = shift;
	my $attr = shift;
	my $prop = shift;
	
	return sub  {
		my $self	= shift;
		my $file	= shift;
	
		if (defined $file){
			if (! ref $file) {
				my $handle = $self->gen_handle();
				open ($handle, ">>" . $file)
					or return $self->error("Could not open file $file : $!", "BL-01");
				select((select($handle), $| = 1)[0]); 		#Make sure the file is piping hot!
				$self->closed(0);
				return $self->$prop($handle);
			}
			elsif (ref ($file) eq 'GLOB') {
				select((select($file), $| = 1)[0]); 		#Make sure the file is piping hot!
				$self->closed(0);
				return $self->$prop($file);
			}
			else {
				return $self->error("File error. I don't know what a $file is", "BL-03");
			};
		}
		else {
			return $self->$prop();
		};
	}

};

sub log {
	my $self	= shift;
	my $note	= shift or return $self->error("Cannot log w/o notification", "BL-07");
	
	return $self->error("Cannot log to closed handle", "BL-08") if $self->closed;
	
	my $args	= ref $note ? $note->{'args'} || [] : [$note];

	my $handle	= $self->handle or return;

	my $printed = 0;
	
	if (@$args) {
	
		if ($self->stamped) {
			my $stamp = localtime;
			print $handle "AT (", $stamp, "):\t";
		};
	
		foreach my $value (@$args) {
			my $tab = $printed++ ? "\t" : "";
			print $handle $tab, $value, "\n" if defined $value;
		};
	}
	
	return 1;

};



sub close {
	my $self = shift;

	my $handle	= $self->handle or return 1;
	
	close($handle) or return $self->error("Canot close handle : $!", "BL-06");
	
	$self->closed(1);
	
	return 1;
}


sub DESTROY {
	my $self = shift;
	
	$self->close unless $self->closed;
	
	#$self->SUPER::DESTROY(@_);
}

1;