Bot-Cobalt

 view release on metacpan or  search on metacpan

lib/Bot/Cobalt/Logger/Output.pm  view on Meta::CPAN

package Bot::Cobalt::Logger::Output;
$Bot::Cobalt::Logger::Output::VERSION = '0.021003';
use Carp;
use strictures 2;

use Bot::Cobalt::Common qw/:types :string/;
use POSIX ();
use Try::Tiny;

use Moo;

has time_format => (
  is        => 'rw',
  isa       => Str,
  builder   => sub { "%Y-%m-%d %H:%M:%S" },  # strftime
);

has log_format => (
  is        => 'rw',
  isa       => Str,
  builder   => sub { "%level %time (%pkg%) %msg" }, # rplprintf
);


has _outputs => (
  is        => 'rwp',
  isa       => HashRef,
  default   => sub { +{} },
);


sub add {
  my ($self, @args) = @_;
  
  unless (@args && @args % 2 == 0) {
    confess "add() expects an even number of arguments, ",
         "mapping an Output class to a HASH of constructor arguments"
  }
  
  my $prefix = 'Bot::Cobalt::Logger::Output::' ;
  
  CONFIG: while (my ($alias, $opts) = splice @args, 0, 2) {
    confess "Can't add $alias, opts are not a HASH"
      unless ref $opts eq 'HASH';

    confess "Can't add $alias, no type specified"
      unless $opts->{type};

    my $target_pkg = $prefix . delete $opts->{type};

    { local $@;
      eval "require $target_pkg";
      
      if (my $err = $@) {
        carp "Could not add logger $alias: $err";
        next CONFIG
      }
    }

    my $new_obj = try {
      $target_pkg->new(%$opts)
    } catch {
      carp "Could not add logger $alias; new() died: $_";
      undef
    } or next CONFIG;

    $self->_outputs->{$alias} = $new_obj;
  }  ## CONFIG



( run in 1.971 second using v1.01-cache-2.11-cpan-0d23b851a93 )