| 1 | ########################################################################### | ||
| 2 | # | ||
| 3 | # Syslog.pm | ||
| 4 | # | ||
| 5 | # Copyright (C) 1999 Raphael Manfredi. | ||
| 6 | # Copyright (C) 2002-2017 Mark Rogaski, mrogaski@cpan.org; | ||
| 7 | # all rights reserved. | ||
| 8 | # | ||
| 9 | # See the README file included with the | ||
| 10 | # distribution for license information. | ||
| 11 | # | ||
| 12 | ########################################################################## | ||
| 13 | |||
| 14 | use strict; | ||
| 15 | |||
| 16 | ######################################################################## | ||
| 17 | package Log::Agent::Driver::Syslog; | ||
| 18 | |||
| 19 | require Log::Agent::Driver; | ||
| 20 | use vars qw(@ISA); | ||
| 21 | @ISA = qw(Log::Agent::Driver); | ||
| 22 | |||
| 23 | require Log::Agent::Channel::Syslog; | ||
| 24 | |||
| 25 | # | ||
| 26 | # ->make -- defined | ||
| 27 | # | ||
| 28 | # Creation routine. | ||
| 29 | # | ||
| 30 | # All switches are passed to Log::Agent::Channel::Syslog. | ||
| 31 | # | ||
| 32 | # prefix the application name | ||
| 33 | # facility the syslog facility name to use ("auth", "daemon", etc...) | ||
| 34 | # showpid whether to show pid | ||
| 35 | # socktype socket type ('unix' or 'inet') | ||
| 36 | # logopt list of openlog() options: 'ndelay', 'cons' or 'nowait' | ||
| 37 | # | ||
| 38 | sub make { | ||
| 39 | my $self = bless {}, shift; | ||
| 40 | my (%args) = @_; | ||
| 41 | my $prefix; | ||
| 42 | |||
| 43 | my %set = ( | ||
| 44 | -prefix => \$prefix, # Handled by parent via _init | ||
| 45 | ); | ||
| 46 | |||
| 47 | while (my ($arg, $val) = each %args) { | ||
| 48 | my $vset = $set{lc($arg)}; | ||
| 49 | next unless ref $vset; | ||
| 50 | $$vset = $val; | ||
| 51 | } | ||
| 52 | |||
| 53 | $self->{channel} = Log::Agent::Channel::Syslog->make(@_); | ||
| 54 | $self->_init($prefix, 0); # 0 is the skip Carp penalty | ||
| 55 | return $self; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub channel { $_[0]->{channel} } | ||
| 59 | |||
| 60 | # | ||
| 61 | # ->prefix_msg -- defined | ||
| 62 | # | ||
| 63 | # NOP -- syslog will handle this | ||
| 64 | # | ||
| 65 | sub prefix_msg { | ||
| 66 | my $self = shift; | ||
| 67 | return $_[0]; | ||
| 68 | } | ||
| 69 | |||
| 70 | # | ||
| 71 | # ->channel_eq -- defined | ||
| 72 | # | ||
| 73 | # Always true. | ||
| 74 | # | ||
| 75 | sub channel_eq { | ||
| 76 | return 1; | ||
| 77 | } | ||
| 78 | |||
| 79 | my %syslog_pri = ( | ||
| 80 | 'em' => 'emerg', | ||
| 81 | 'al' => 'alert', | ||
| 82 | 'cr' => 'crit', | ||
| 83 | 'er' => 'err', | ||
| 84 | 'wa' => 'warning', | ||
| 85 | 'no' => 'notice', | ||
| 86 | 'in' => 'info', | ||
| 87 | 'de' => 'debug' | ||
| 88 | ); | ||
| 89 | |||
| 90 | # | ||
| 91 | # ->map_pri -- redefined | ||
| 92 | # | ||
| 93 | # Levels ignored, only priorities matter. | ||
| 94 | # | ||
| 95 | sub map_pri { | ||
| 96 | my $self = shift; | ||
| 97 | my ($priority, $level) = @_; | ||
| 98 | return $syslog_pri{lc(substr($priority, 0, 2))} || 'debug'; | ||
| 99 | } | ||
| 100 | |||
| 101 | # | ||
| 102 | # ->write -- defined | ||
| 103 | # | ||
| 104 | # $channel is ignored | ||
| 105 | # | ||
| 106 | sub write { | ||
| 107 | my $self = shift; | ||
| 108 | my ($channel, $priority, $logstring) = @_; | ||
| 109 | $self->channel->write($priority, $logstring); | ||
| 110 | } | ||
| 111 | |||
| 112 | 1; # for require | ||
| 113 | __END__ |