EntityModel-Log

 view release on metacpan or  search on metacpan

lib/EntityModel/Log.pm  view on Meta::CPAN

package EntityModel::Log;
# ABSTRACT: Logging class used by EntityModel
use strict;
use warnings;
use parent qw{Exporter};

our $VERSION = '0.006';

=head1 NAME

EntityModel::Log - simple logging support for L<EntityModel>

=head1 VERSION

version 0.006

=head1 SYNOPSIS

 use EntityModel::Log ':all';
 # Log everything down to level 0 (debug)
 EntityModel::Log->instance->min_level(0);

 # STDERR by default, or Test::More::note if you have it loaded
 logDebug("Test something");
 logInfo("Object [%s] found", $obj->name);
 logError("Fatal problem");
 logInfo(sub { my $str = heavy_operation(); return 'Failed: %s', $str });

 logInfo("Stack trace - note that it must have at least one parameter (%s): %S", 'like this');
 logInfo("No stack trace without parameters despite %S");

 my $log = EntityModel::Log->instance;
 $log->debug("OO-style debug");
 $log->info("OO-style info");
 $log->warning("OO-style warning");
 $log->error("OO-style error");

=head1 DESCRIPTION

Yet another logging class. Provides a procedural and OO interface as usual - intended for use
with L<EntityModel> only, if you're looking for a general logging framework try one of the
other options in the L</SEE ALSO> section.

=cut

# Need to be able to switch off logging in UNITCHECK stages, since that segfaults perl5.10.1 and possibly other versions
our $DISABLE = 0;

use Time::HiRes qw{time};
use POSIX qw{strftime};
use Exporter;
use List::Util qw{min max};
use Scalar::Util qw{blessed};
use IO::Handle;
use File::Basename ();
use Data::Dump ();
use Data::Dump::Filtered ();

our %EXPORT_TAGS = ( 'all' => [qw/&logDebug &logInfo &logWarning &logError/] );
our @EXPORT_OK = ( @{$EXPORT_TAGS{'all'}} );

# Internal singleton instance
my $instance;

=head2 instance

Returns a handle to the main instance of L<EntityModel::Log>.

=cut

sub instance { my $class = shift; $instance ||= $class->new }

=head1 PROCEDURAL METHODS

=cut

my @LogType = (
	'Debug',
	'Info',
	'Warning',
	'Error',
	'Fatal',
);

=head2 _raise_error_on_global_instance

Raise the given (code, message, ...) log event on the L<EntityModel::Log> global instance.

=cut

sub _raise_error_on_global_instance { __PACKAGE__->instance->raise(@_); }

=head2 logDebug

Raise a debug message. Expect a high volume of these during normal operation
so a production server would typically have these disabled.

=cut

sub logDebug { unshift @_, 0; goto &_raise_error_on_global_instance; }

=head2 logInfo

Raise an informational message, which we'd like to track for stats
reasons - indicates normal operations rather than an error condition.



( run in 2.058 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )