Log-Dispatch-UnixSyslog

 view release on metacpan or  search on metacpan

lib/Log/Dispatch/UnixSyslog.pm  view on Meta::CPAN

#pod     )
#pod   );
#pod
#pod   while (@events) {
#pod     $log->warn($_);
#pod   }
#pod
#pod =head1 DESCRIPTION
#pod
#pod This provides a Log::Dispatch log output plugin that sends things to syslog.
#pod "But there's already Log::Dispatch:Syslog!" you cry.  Well, that uses
#pod Sys::Syslog, which is core, but it's overcomplicated and inefficient, too.
#pod This plugin uses Unix::Syslog, which does a lot less, and should be more
#pod efficient at doing it.
#pod
#pod =method new
#pod
#pod  my $output = Log::Dispatch::UnixSyslog->new(\%arg);
#pod
#pod This method constructs a new Log::Dispatch::UnixSyslog output object.  In
#pod addition to the standard parameters documented in L<Log::Dispatch::Output>,
#pod this takes the following arguments:
#pod
#pod   ident     - a string to prepend to all messages in the system log; required
#pod   facility  - which syslog facility to log to (as a string); required
#pod   logopt    - the numeric value of the openlog options parameter; (default: 0)
#pod
#pod =cut

my %IS_FACILITY = map {; $_ => 1 } qw(
  auth    authpriv    cron    daemon
  ftp     kern        lpr     mail
  news    security    syslog  user
  uucp
  local0  local1      local2  local3
  local4  local5      local6  local7
);

sub new {
  my ($class, %arg) = @_;

  Carp::croak('required parameter "ident" empty or undefined')
    unless length $arg{ident};

  Carp::croak('required parameter "facility" not defined')
    unless defined $arg{facility};

  Carp::croak('provided facility value is not a valid syslog facility')
    unless $IS_FACILITY{ $arg{facility} };

  my $const_name = "LOG_\U$arg{facility}";

  Carp::croak('provided facility value is valid but unknown?!')
    unless my $const = Unix::Syslog->can($const_name);

  my $self = {
    ident     => $arg{ident},
    facility  => scalar $const->(),
  };

  bless $self => $class;

  # this is our duty as a well-behaved Log::Dispatch plugin
  $self->_basic_init(%arg);

  # hand wringing: What if someone is re-openlog-ing after this?  Well, they
  # ought not to do that!  We could re-open every time, but let's just see how
  # this goes, for now. -- rjbs, 2020-08-11
  Unix::Syslog::openlog($self->{ident}, $arg{logopt} // 0, $self->{facility});

  return $self;
}

#pod =method log_message
#pod
#pod This is the method which performs the actual logging, as detailed by
#pod Log::Dispatch::Output.
#pod
#pod =cut

sub log_message {
  my ($self, %p) = @_;

  # In syslog, emergency is 0 and debug is 7.  In Log::Dispatch, it is the
  # reverse.  Bah. -- rjbs, 2020-08-11
  my $sys_level = 7 - $self->_level_as_number($p{level});
  my $priority  = $sys_level | $self->{facility};

  Unix::Syslog::syslog($priority, '%s', $p{message});

  return;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Log::Dispatch::UnixSyslog - log events to syslog with Unix::Syslog

=head1 VERSION

version 0.002

=head1 SYNOPSIS

  use Log::Dispatch;
  use Log::Dispatch::UnixSyslog;

  my $log = Log::Dispatch->new;

  $log->add(
    Log::Dispatch::UnixSyslog->new(
      ident => 'super-cool-daemon',
      min_level => 'debug',
      flush_if  => sub { (shift)->event_count >= 60 },

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.794 second using v1.00-cache-2.02-grep-82fe00e-cpan-9e6bc14194b )