Ambrosia

 view release on metacpan or  search on metacpan

lib/Ambrosia/Logger.pm  view on Meta::CPAN

}

sub error
{
    __info_ex(shift, 'ERROR: ', map { ref $_ && blessed($_) && $_->isa('Ambrosia::error::Exception::Error') ? "$_" : $_ } @_);
}

sub log
{
    my ($self, @msg) = @_;
    my($sec, $min, $hour) = (localtime)[0..2];
    @msg = ('EMPTY') unless @msg;
    @msg = map { defined $_ ? $_ : 'undef' } @msg;
    __tolog($self, sprintf("%02d:%02d:%02d (op = %s) %s\n", $hour, $min, $sec, $self->{_op}, join (' ', @msg)));
}

sub __tolog
{
    my $log = $_[0]->{_log};
    print $log $_[1];
}

sub __log_time
{
    my $self = shift;
    my $msg = shift;
    my $key = shift;
    if ( $msg )
    {
        if ( $self->{_time}->{$key} )
        {
            $self->log( $msg, " -::- ^^^^^^^^^^^^^^^^^^^ $key |", sprintf("%.4f", time - $self->{_time}->{$key} ) );
            delete $self->{_time}->{$key};
            return;
        }
        else
        {
            $self->log( $msg, " -::- vvvvvvvvvvvvvvvvvvv $key" );
        }
    }
    $self->{_time}->{$key} = time if $key;
}


sub __debug
{
    my ($self, @msg) = @_;
    my $p = __PACKAGE__;
    my $x = 0;
    my ($package, $line, $subroutine);
    my @callers;
 
    while ( do { package DB; ($package, $line, $subroutine) = (caller($x++))[0, 2, 3] } )
    {
        my @arg = $subroutine !~ /^$p\:\:/ ? @DB::args : ('...');
        unshift @callers, "\t$subroutine"
            . ( $subroutine ne '(eval)' ? ('( '.(join ", ", @arg).' )'):'')
            . ' At ' . $package
            . ' line ' . $line;
    }
    push @msg, "\nstack frames = [\n", (join "\n", @callers), "\n]";
    $self->log_info_ex(@msg);
}

sub __info_ex
{
    local $Data::Dumper::Indent = 1;
    shift->log( map { ref $_ ? Dumper($_) : $_ } @_);
}

sub __info
{
    shift->log( map { ref $_ ? ref $_ : $_ } @_);
}

sub DESTROY
{}

1;

__END__

=head1 NAME

Ambrosia::Logger - a class for create global object for logging.

=head1 VERSION

version 0.010

=head1 SYNOPSIS

    use Ambrosia::Logger;
    BEGIN {
        instance Ambrosia::Logger('myApplication', DEBUG => 1, INFO_EX => 1, INFO => 1, -prefix => 'GoogleCoupon_', -dir => $logger_path);
        Ambrosia::Logger::assign 'myApplication';
    }

    logger->log('is just message', 'other message' );
    logger->log_info('is simple info', ... );
    logger->log_info_ex('is dump of structures info', {foo=>1}, [{bar=>1},{baz=>2}] );
    logger->error('message about errors');
    logger->debug('write with the message and the stack of calls');

=head1 DESCRIPTION

C<Ambrosia::Logger> is a class for create global object for logging.
Implement the pattern B<Singleton>.

=head2 instance

Instances the named object of type C<Ambrosia::Logger> in the pool.
This method not exported. Use as constructor: C<instance Ambrosia::Logger(.....)>

=head2 logger

Returns the global object of type C<Ambrosia::Logger>.
C<logger(name)> - the name is optional param. Call with name if you not assign current process to logger yet.

=head2 assign



( run in 0.543 second using v1.01-cache-2.11-cpan-e1769b4cff6 )