Log::Syslog::Fast::Simple - Wrapper around Log::Syslog::Fast that adds some


Log-Syslog-Fast documentation Contained in the Log-Syslog-Fast distribution.

Index


Code Index:

NAME

Top

Log::Syslog::Fast::Simple - Wrapper around Log::Syslog::Fast that adds some flexibility at the expense of additional runtime overhead.

SYNOPSIS

Top

  use Log::Syslog::Fast::Simple;

  # Simple usage:
  $logger = Log::Syslog::Fast::Simple->new;
  $logger->send("log message");

  # More customized usage:
  $logger = Log::Syslog::Fast::Simple->new(
      loghost  => 'myloghost',
      port     => 6666,
      facility => LOG_LOCAL2,
      severity => LOG_INFO,
      sender   => 'mymachine',
      name     => 'myapp',
  );
  $logger->send("log message", time, LOG_LOCAL3, LOG_DEBUG);

DESCRIPTION

Top

This module wraps Log::Syslog::Fast to provide a constructor with reasonable defaults and a send() method that optionally accepts override parameters for facility and severity.

METHODS

Top

Log::Syslog::Fast::Simple->new(%params);

Create a new Log::Syslog::Fast::Simple object with given parameters (may be a hash or hashref). Takes the following named parameters which have the same meaning as in Log::Syslog::Fast.

proto

Defaults to LOG_UDP

loghost

Defaults to 127.0.0.1

port

Defaults to 514

facility

Defaults to LOG_LOCAL0

severity

Defaults to LOG_INFO

sender

Defaults to Sys::Hostname::hostname

name

Defaults to a cleaned $0

$logger->send($logmsg, [$time], [$severity], [$facility])

Send a syslog message through the configured logger. If $time is not provided, the current time is used. If $severity or $facility are not provided, the default provided at construction time is used.

EXPORT

Top

Same as Log::Syslog::Fast.

SEE ALSO

Top

Log::Syslog::Fast

AUTHOR

Top

Adam Thomason, <athomason@sixapart.com>

COPYRIGHT AND LICENSE

Top


Log-Syslog-Fast documentation Contained in the Log-Syslog-Fast distribution.

package Log::Syslog::Fast::Simple;

use strict;
use warnings;

use Log::Syslog::Fast ':all';
use Sys::Hostname;

require Exporter;

our @ISA = qw(Exporter);

our %EXPORT_TAGS = %Log::Syslog::Fast::EXPORT_TAGS;
our @EXPORT_OK   = @Log::Syslog::Fast::EXPORT_OK;
our @EXPORT      = qw();

use constant _LOGGERS   => 0;
use constant _ARGS      => 1;

use constant _PROTO     => 0;
use constant _HOSTNAME  => 1;
use constant _PORT      => 2;
use constant _FACILITY  => 3;
use constant _SEVERITY  => 4;
use constant _SENDER    => 5;
use constant _NAME      => 6;

sub new {
    my $what = shift;
    my $class = ref $what || $what;

    my $default_name = $0;
    $default_name =~ s,.*/,,;
    $default_name =~ s/[^\w.-_]//g;

    my $args = (@_ == 1 && ref $_[0] eq 'HASH') ? $_[0] : {@_};

    $args->{proto}      ||= LOG_UDP;
    $args->{hostname}   ||= '127.0.0.1';
    $args->{port}       ||= 514;
    $args->{facility}   ||= LOG_LOCAL0;
    $args->{severity}   ||= LOG_INFO;
    $args->{sender}     ||= Sys::Hostname::hostname;
    $args->{name}       ||= $default_name;

    return bless [
        [],    # loggers
        [@{ $args }{qw/
            proto hostname port facility severity sender name
        /}],
    ], $class;
}

sub send {
    my $severity = $_[3] || $_[0][_ARGS][_SEVERITY];
    my $facility = $_[4] || $_[0][_ARGS][_FACILITY];

    my $logger = $_[0][_LOGGERS][$facility][$severity];
    if (!$logger) {
        my @args = @{ $_[0][_ARGS] };
        $args[_FACILITY] = $facility;
        $args[_SEVERITY] = $severity;

        $logger = $_[0][_LOGGERS][$facility][$severity] = Log::Syslog::Fast->new(@args);
    }

    return $logger->send($_[1], $_[2] || time);
}

1;
__END__