| 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 | require Log::Agent::Channel; | ||
| 16 | |||
| 17 | ######################################################################## | ||
| 18 | package Log::Agent::Channel::Syslog; | ||
| 19 | |||
| 20 | use vars qw(@ISA); | ||
| 21 | @ISA = qw(Log::Agent::Channel); | ||
| 22 | |||
| 23 | use Sys::Syslog qw(:DEFAULT setlogsock); | ||
| 24 | |||
| 25 | # | ||
| 26 | # ->make -- defined | ||
| 27 | # | ||
| 28 | # Creation routine. | ||
| 29 | # | ||
| 30 | # Attributes (and switches that set them): | ||
| 31 | # | ||
| 32 | # prefix the logging prefix to use (application name, usally) | ||
| 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 | |||
| 42 | my %set = ( | ||
| 43 | -prefix => \$self->{'prefix'}, | ||
| 44 | -facility => \$self->{'facility'}, | ||
| 45 | -showpid => \$self->{'showpid'}, | ||
| 46 | -socktype => \$self->{'socktype'}, | ||
| 47 | -logopt => \$self->{'logopt'}, | ||
| 48 | ); | ||
| 49 | |||
| 50 | while (my ($arg, $val) = each %args) { | ||
| 51 | my $vset = $set{lc($arg)}; | ||
| 52 | unless (ref $vset) { | ||
| 53 | require Carp; | ||
| 54 | Carp::croak("Unknown switch $arg"); | ||
| 55 | } | ||
| 56 | $$vset = $val; | ||
| 57 | } | ||
| 58 | |||
| 59 | $self->{'logopt'} =~ s/\bpid\b//g; # Must use showpid => 1 | ||
| 60 | $self->{'logopt'} .= ' pid' if $self->showpid; | ||
| 61 | |||
| 62 | return $self; | ||
| 63 | } | ||
| 64 | |||
| 65 | # | ||
| 66 | # Attribute access | ||
| 67 | # | ||
| 68 | |||
| 69 | sub prefix { $_[0]->{'prefix'} } | ||
| 70 | sub facility { $_[0]->{'facility'} || 'user' } | ||
| 71 | sub showpid { $_[0]->{'showpid'} } | ||
| 72 | sub socktype { $_[0]->{'socktype'} } | ||
| 73 | sub logopt { $_[0]->{'logopt'} } | ||
| 74 | sub connected { $_[0]->{'connected'} } | ||
| 75 | |||
| 76 | # | ||
| 77 | # ->connect | ||
| 78 | # | ||
| 79 | # Connect to syslogd. | ||
| 80 | # | ||
| 81 | sub connect { | ||
| 82 | my $self = shift; | ||
| 83 | setlogsock $self->socktype if $self->socktype; | ||
| 84 | openlog $self->prefix, $self->logopt, $self->facility; | ||
| 85 | $self->{'connected'}++; | ||
| 86 | } | ||
| 87 | |||
| 88 | # | ||
| 89 | # ->close -- defined | ||
| 90 | # | ||
| 91 | # Disconnect from syslogd. | ||
| 92 | # | ||
| 93 | sub disconnect { | ||
| 94 | my $self = shift; | ||
| 95 | return unless $self->connected; | ||
| 96 | closelog; | ||
| 97 | $self->{'connected'} = 0; | ||
| 98 | } | ||
| 99 | |||
| 100 | # | ||
| 101 | # ->write -- defined | ||
| 102 | # | ||
| 103 | sub write { | ||
| 104 | my $self = shift; | ||
| 105 | my ($priority, $logstring) = @_; | ||
| 106 | $self->connect unless $self->connected; | ||
| 107 | syslog $priority, "%s", $logstring; | ||
| 108 | } | ||
| 109 | |||
| 110 | 1; # for require | ||
| 111 | __END__ |