1package Log::Syslog::Fast::Simple;
2
3use strict;
4use warnings;
5
6use Log::Syslog::Fast ':all';
7use Sys::Hostname;
8
9require Exporter;
10our @ISA = qw(Exporter);
11our @EXPORT      = qw();
12our %EXPORT_TAGS = %Log::Syslog::Fast::Constants::EXPORT_TAGS;
13our @EXPORT_OK   = @Log::Syslog::Fast::Constants::EXPORT_OK;
14
15use constant _LOGGERS   => 0;
16use constant _ARGS      => 1;
17
18use constant _PROTO     => 0;
19use constant _HOSTNAME  => 1;
20use constant _PORT      => 2;
21use constant _FACILITY  => 3;
22use constant _SEVERITY  => 4;
23use constant _SENDER    => 5;
24use constant _NAME      => 6;
25use constant _FORMAT    => 7;
26
27sub new {
28    my $what = shift;
29    my $class = ref $what || $what;
30
31    my $default_name = $0;
32    $default_name =~ s,.*/,,;
33    $default_name =~ s/[^\w.-_]//g;
34
35    my $args = (@_ == 1 && ref $_[0] eq 'HASH') ? $_[0] : {@_};
36
37    $args->{proto}      ||= LOG_UDP;
38    $args->{hostname}   ||= '127.0.0.1';
39    $args->{port}       ||= 514;
40    $args->{facility}   ||= LOG_LOCAL0;
41    $args->{severity}   ||= LOG_INFO;
42    $args->{sender}     ||= Sys::Hostname::hostname;
43    $args->{name}       ||= $default_name;
44    $args->{format}     ||= LOG_RFC3164;
45
46    return bless [
47        [],    # loggers
48        [@{ $args }{qw/
49            proto hostname port facility severity sender name format
50        /}],
51    ], $class;
52}
53
54sub send {
55    my $severity = $_[3] || $_[0][_ARGS][_SEVERITY];
56    my $facility = $_[4] || $_[0][_ARGS][_FACILITY];
57
58    my $logger = $_[0][_LOGGERS][$facility][$severity];
59    if (!$logger) {
60        my @args = @{ $_[0][_ARGS] };
61        $args[_FACILITY] = $facility;
62        $args[_SEVERITY] = $severity;
63
64        my $format = pop(@args);
65        $logger = $_[0][_LOGGERS][$facility][$severity] = Log::Syslog::Fast->new(@args);
66        $logger->set_format($format);
67    }
68
69    return $logger->send($_[1], $_[2] || time);
70}
71
721;
73__END__
74
75=head1 NAME
76
77Log::Syslog::Fast::Simple - Wrapper around Log::Syslog::Fast that adds some
78flexibility at the expense of additional runtime overhead.
79
80=head1 SYNOPSIS
81
82  use Log::Syslog::Fast::Simple;
83
84  # Simple usage:
85  $logger = Log::Syslog::Fast::Simple->new;
86  $logger->send("log message");
87
88  # More customized usage:
89  $logger = Log::Syslog::Fast::Simple->new(
90      loghost  => 'myloghost',
91      port     => 6666,
92      facility => LOG_LOCAL2,
93      severity => LOG_INFO,
94      sender   => 'mymachine',
95      name     => 'myapp',
96  );
97  $logger->send("log message", time, LOG_LOCAL3, LOG_DEBUG);
98
99=head1 DESCRIPTION
100
101This module wraps L<Log::Syslog::Fast> to provide a constructor with reasonable
102defaults and a send() method that optionally accepts override parameters for
103facility and severity.
104
105=head1 METHODS
106
107=over 4
108
109=item Log::Syslog::Fast::Simple-E<gt>new(%params);
110
111Create a new Log::Syslog::Fast::Simple object with given parameters (may be a
112hash or hashref). Takes the following named parameters which have the same
113meaning as in Log::Syslog::Fast.
114
115=over 4
116
117=item proto
118
119Defaults to LOG_UDP
120
121=item loghost
122
123Defaults to 127.0.0.1
124
125=item port
126
127Defaults to 514
128
129=item facility
130
131Defaults to LOG_LOCAL0
132
133=item severity
134
135Defaults to LOG_INFO
136
137=item sender
138
139Defaults to Sys::Hostname::hostname
140
141=item name
142
143Defaults to a cleaned $0
144
145=back
146
147=item $logger-E<gt>send($logmsg, [$time], [$severity], [$facility])
148
149Send a syslog message through the configured logger. If $time is not provided,
150the current time is used. If $severity or $facility are not provided, the
151default provided at construction time is used.
152
153=back
154
155=head1 EXPORT
156
157Same as Log::Syslog::Fast.
158
159=head1 SEE ALSO
160
161L<Log::Syslog::Fast>
162
163=head1 AUTHOR
164
165Adam Thomason, E<lt>athomason@sixapart.comE<gt>
166
167=head1 COPYRIGHT AND LICENSE
168
169Copyright (C) 2009-2011 by Say Media, Inc.
170
171This library is free software; you can redistribute it and/or modify
172it under the same terms as Perl itself, either Perl version 5.8.5 or,
173at your option, any later version of Perl 5 you may have available.
174
175=cut
176