Log-Mini/lib/Log/Mini.pm

90 lines
2 KiB
Perl
Raw Normal View History

2019-02-21 22:30:49 +03:00
package Log::Mini;
use strict;
use warnings;
2023-03-17 18:29:38 +03:00
use Module::Load qw/load/;
2019-02-21 22:30:49 +03:00
2023-03-17 18:29:38 +03:00
require Carp;
2023-03-17 18:29:38 +03:00
our $VERSION = "0.3.0";
sub new
{
shift;
2023-03-17 18:29:38 +03:00
my ($type, @args) = @_;
@args = () unless @args;
2019-02-21 22:30:49 +03:00
2019-02-22 10:50:23 +03:00
$type = 'stderr' unless defined $type;
2023-03-17 18:29:38 +03:00
if ($type eq 'file') {
unshift(@args, $type);
2019-02-21 22:30:49 +03:00
}
2023-03-17 18:29:38 +03:00
my $module_name = sprintf('Log::Mini::Logger::%s', uc($type));
my $logger;
eval {
load $module_name;
$logger = $module_name->new(@args);
} or do { Carp::croak(sprintf("Failed to load adapter: %s, %s\n", $type, $@)) };
return $logger;
2019-02-21 22:30:49 +03:00
}
1;
2019-02-21 23:23:53 +03:00
__END__
=pod
=encoding UTF-8
=head1 NAME
Log::Mini - It's a very simple logger which can log your messages to a file or STDERR.
=head1 SYNOPSIS
use Log::Mini;
2019-04-12 01:01:37 +03:00
my $logger = Log::Mini->new(); #STDERR logger used by default. Error is default log level
2019-02-22 10:57:14 +03:00
$logger->error('Error message');
2019-02-21 23:23:53 +03:00
2019-04-12 01:01:37 +03:00
my $debug_logger = Log::Mini->new('stderr', level => 'debug'); #STDERR logger used by default
2019-02-22 10:57:14 +03:00
$debug_logger->error('Error message');
2019-02-21 23:23:53 +03:00
2019-04-12 01:01:37 +03:00
my $file_logger = Log::Mini->new(file => 'log_file.log');
2019-02-21 23:23:53 +03:00
$file_logger->info('message to log file');
#prevent buffered output. May slow down your application!
my $synced_file_logger = Log::Mini->get_logger(file => 'log_file.log', synced => 1);
2019-03-19 15:03:14 +03:00
#format message with sprintf
$logger->info('Message with %s %s', 'some', 'additional info');
#log method for better compatibility
$logger->log('info', 'information message');
2019-04-12 01:01:37 +03:00
#Null logger - drops all messages to /dev/null
my $logger = Log::Mini->new('null);
$logger->error('Error message'); #Message will be dropped
2019-02-21 23:23:53 +03:00
=head1 DESCRIPTION
Log::Mini is a very simple logger which can log your messages to a file or STDERR.
You can have a number of loggers for various log files.
=head1 LICENSE
Copyright (C) Denis Fedoseev
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 AUTHOR
Denis Fedoseev <denis.fedoseev@gmail.com>