summaryrefslogtreecommitdiffabout
path: root/lib/Mojo/Log/Syslog.pm
blob: 621301fb331e85a57c13174195b8f537f3ba0af2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package Mojo::Log::Syslog;
use strict;
use warnings;
use Mojo::Base 'Mojo::Log';
use File::Basename 'basename';
use Sys::Syslog qw(:standard :macros);
use Carp;

our $VERSION = '1.0';

sub new {
    my $class = shift;
    local %_ = @_;
    my $ident = delete $_{ident} || basename($0);
    my $facility = delete $_{facility} || LOG_USER;
    my $level = delete $_{level} || 'debug';
    my $logopt = delete $_{logopt} || 'ndelay,pid';
    if (ref($logopt) eq 'ARRAY') {
	$logopt = join(',', @$logopt);
    }
    croak "unrecognized arguments" if keys(%_);
    openlog($ident, $logopt, $facility);
    return $class->SUPER::new(level => $level);
}

sub debug { shift->_syslog(debug => LOG_DEBUG => @_) }
sub warn  { shift->_syslog(warn  => LOG_WARN  => @_) }
sub info  { shift->_syslog(info  => LOG_INFO  => @_) }
sub error { shift->_syslog(error => LOG_ERROR => @_) }
sub fatal { shift->_syslog(fatal => LOG_CRIT  => @_) }

sub _syslog {
    my ($self, $level, $prio, @args) = @_;
    $self->{_priority} = $prio;
    $self->${\ "SUPER::$level"}(@args);
}

sub append {
    my ($self, $msg) = @_;
    syslog($self->{_priority}, "%s", $msg);
}

has format => sub { shift->short ? \&_fmt_short : \&_fmt_long };

sub _fmt_short {
    my (undef, undef, @lines) = @_;
    return join("\n", @lines);
}

sub _fmt_long {
    my (undef, $level, @lines) = @_;
    return "[$level]: ".join("\n", @lines);
}

1;
__END__

=head1 NAME

Mojo::Log::Syslog - syslog for Mojo projects

=head1 SYNOPSIS

    use Mojo::Log::Syslog;

    $logger = new Mojo::Log::Syslog(facility => 'user',
                                    ident => 'myapp',
                                    level => 'warn');

    app->log($logger);

=head1 DESCRIPTION

Syslog-based logger for Mojo applications.

=head1 CONSTRUCTOR

The B<Mojo::Log::Syslog> constructor takes the following keyword arguments,
all of which are optional:

=over 4

=item B<facility =E<gt>> I<FACILITY>

Sets the syslog facility to use. Valid facility names are: C<auth>,
C<authpriv>, C<cron>, C<daemon>, C<ftp>, C<kern>, C<local0> through
C<local7>, C<lpr>, C<mail>, C<news>, C<user>, and C<uucp>. See also
B<Sys::Syslog>(3), section B<Facilities>.

The default is C<user>.

=item B<ident =E<gt>> I<STRING>

Syslog message identifier. Defaults to the base name from B<$0>.

=item B<logopt =E<gt>> I<OPTLIST>

Defines the list of options for B<openlog>. I<OPTLIST> is either a string
with comma-separated option names or a list reference containing option
names. The following two options are equivalent:

    logopt => "ndelay,pid,nowait"

    logopt => [qw(ndeay pid nowait)]

See B<Sys::Syslog>(3) for a list of available option names.
    
Defaults to C<ndelay,pid>.

=item B<level E<gt>> I<NAME>

Sets minimum logging level. See B<Mojo::Log>, for a list of levels.    
    
=back

=head1 EXAMPLE

=head2 Using with Mojolicious::Lite

    use Mojolicious::Lite;
    use Mojo::Log::Syslog;

    my $logger = new Mojo::Log::Syslog(facility => 'local0',
                                       level => warn);
    app->log($logger);

=head2 Using with Mojolicious

    package MyApp;
    use Mojo::Base 'Mojolicious';

    sub startup {
        my $self = shift;
 
        my $logger = new Mojo::Log::Syslog(facility => 'local0',
                                           level => warn);
        $self->app->log($logger);
    }

=head1 SEE ALSO

B<Mojo::Log>(3),     
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>,

=cut
    

Return to:

Send suggestions and report system problems to the System administrator.