basic SimpleLogger code
Arthur Axel 'fREW' Schmidt [Sat, 20 Feb 2010 10:12:25 +0000 (04:12 -0600)]
lib/Log/Contextual/SimpleLogger.pm [new file with mode: 0644]
t/simplelogger.t [new file with mode: 0644]

diff --git a/lib/Log/Contextual/SimpleLogger.pm b/lib/Log/Contextual/SimpleLogger.pm
new file mode 100644 (file)
index 0000000..55d11f1
--- /dev/null
@@ -0,0 +1,43 @@
+package Log::Contextual::SimpleLogger;
+
+use strict;
+use warnings;
+
+{
+  for my $name (qw[ trace debug info warn error fatal ]) {
+
+    no strict 'refs';
+
+    *{$name} = sub {
+      my $self = shift;
+
+      $self->_log( $name, @_ )
+        if ($self->{$name});
+    };
+
+    *{"is_$name"} = sub {
+      my $self = shift;
+      return $self->{$name};
+    };
+  }
+}
+
+sub new {
+  my ($class, $args) = @_;
+  my $self = bless {}, $class;
+
+  $self->{$_} = 1 for @{$args->{levels}};
+  $self->{coderef} = $args->{coderef} || sub { print STDERR @_};
+  return $self;
+}
+
+sub _log {
+  my $self    = shift;
+  my $level   = shift;
+  my $message = join( "\n", @_ );
+  $message .= "\n" unless $message =~ /\n$/;
+  $self->{coderef}->(sprintf( "[%s] %s", $level, $message ));
+}
+
+1;
+
diff --git a/t/simplelogger.t b/t/simplelogger.t
new file mode 100644 (file)
index 0000000..772f03b
--- /dev/null
@@ -0,0 +1,13 @@
+use strict;
+use warnings;
+
+use Log::Contextual::SimpleLogger;
+use Log::Contextual qw{:log} => -logger =>
+   Log::Contextual::SimpleLogger->new({levels => [qw{debug}]});
+use Test::More qw(no_plan);
+my $l = Log::Contextual::SimpleLogger->new({levels => [qw{debug}]});
+
+ok($l->is_debug, 'is_debug is true on SimpleLogger');
+
+log_debug { 'set_logger' };
+log_trace { die 'this should live' };