… | |
… | |
32 | |
32 | |
33 | # send all critical and higher priority messages to syslog, |
33 | # send all critical and higher priority messages to syslog, |
34 | # regardless of (most) other settings |
34 | # regardless of (most) other settings |
35 | $AnyEvent::Log::COLLECT->attach (new AnyEvent::Log::Ctx |
35 | $AnyEvent::Log::COLLECT->attach (new AnyEvent::Log::Ctx |
36 | level => "critical", |
36 | level => "critical", |
37 | log_to_syslog => 0, |
37 | log_to_syslog => "user", |
38 | ); |
38 | ); |
39 | |
39 | |
40 | =head1 DESCRIPTION |
40 | =head1 DESCRIPTION |
41 | |
41 | |
42 | This module implements a relatively simple "logging framework". It doesn't |
42 | This module implements a relatively simple "logging framework". It doesn't |
… | |
… | |
864 | |
864 | |
865 | Needless(?) to say, if you do not want to be bitten by some evil person |
865 | Needless(?) to say, if you do not want to be bitten by some evil person |
866 | calling C<chdir>, the path should be absolute. Doesn't help with |
866 | calling C<chdir>, the path should be absolute. Doesn't help with |
867 | C<chroot>, but hey... |
867 | C<chroot>, but hey... |
868 | |
868 | |
869 | =item $ctx->log_to_syslog ([$log_flags]) |
869 | =item $ctx->log_to_syslog ([$facility]) |
870 | |
870 | |
871 | Logs all messages via L<Sys::Syslog>, mapping C<trace> to C<debug> and all |
871 | Logs all messages via L<Sys::Syslog>, mapping C<trace> to C<debug> and |
872 | the others in the obvious way. If specified, then the C<$log_flags> are |
872 | all the others in the obvious way. If specified, then the C<$facility> is |
873 | simply or'ed onto the priority argument and can contain any C<LOG_xxx> |
873 | used as the facility (C<user>, C<auth>, C<local0> and so on). The default |
874 | flags valid for Sys::Syslog::syslog, except for the priority levels. |
874 | facility is C<user>. |
875 | |
875 | |
876 | Note that this function also sets a C<fmt_cb> - the logging part requires |
876 | Note that this function also sets a C<fmt_cb> - the logging part requires |
877 | an array reference with [$level, $str] as input. |
877 | an array reference with [$level, $str] as input. |
878 | |
878 | |
879 | =cut |
879 | =cut |
… | |
… | |
922 | 0 |
922 | 0 |
923 | }); |
923 | }); |
924 | } |
924 | } |
925 | |
925 | |
926 | sub log_to_syslog { |
926 | sub log_to_syslog { |
927 | my ($ctx, $flags) = @_; |
927 | my ($ctx, $facility) = @_; |
928 | |
928 | |
929 | require Sys::Syslog; |
929 | require Sys::Syslog; |
930 | |
930 | |
931 | $ctx->fmt_cb (sub { |
931 | $ctx->fmt_cb (sub { |
932 | my $str = $_[3]; |
932 | my $str = $_[3]; |
933 | $str =~ s/\n(?=.)/\n+ /g; |
933 | $str =~ s/\n(?=.)/\n+ /g; |
934 | |
934 | |
935 | [$_[2], "($_[1][0]) $str"] |
935 | [$_[2], "($_[1][0]) $str"] |
936 | }); |
936 | }); |
937 | |
937 | |
|
|
938 | $facility ||= "user"; |
|
|
939 | |
938 | $ctx->log_cb (sub { |
940 | $ctx->log_cb (sub { |
939 | my $lvl = $_[0][0] < 9 ? $_[0][0] : 8; |
941 | my $lvl = $_[0][0] < 9 ? $_[0][0] : 8; |
940 | |
942 | |
941 | Sys::Syslog::syslog ($flags | ($lvl - 1), $_) |
943 | Sys::Syslog::syslog ("$facility|" . ($lvl - 1), $_) |
942 | for split /\n/, $_[0][1]; |
944 | for split /\n/, $_[0][1]; |
943 | |
945 | |
944 | 0 |
946 | 0 |
945 | }); |
947 | }); |
946 | } |
948 | } |
… | |
… | |
1163 | while (/\G((?:[^,:[:space:]]+|::|\\.)+)/gc) { |
1165 | while (/\G((?:[^,:[:space:]]+|::|\\.)+)/gc) { |
1164 | for ("$1") { |
1166 | for ("$1") { |
1165 | if ($_ eq "stderr" ) { $ctx->log_to_warn; |
1167 | if ($_ eq "stderr" ) { $ctx->log_to_warn; |
1166 | } elsif (/^file=(.+)/ ) { $ctx->log_to_file ("$1"); |
1168 | } elsif (/^file=(.+)/ ) { $ctx->log_to_file ("$1"); |
1167 | } elsif (/^path=(.+)/ ) { $ctx->log_to_path ("$1"); |
1169 | } elsif (/^path=(.+)/ ) { $ctx->log_to_path ("$1"); |
1168 | } elsif (/syslog(?:=(.*))?/ ) { require Sys::Syslog; $ctx->log_to_syslog (eval "package Sys::Syslog; $1"); |
1170 | } elsif (/syslog(?:=(.*))?/ ) { require Sys::Syslog; $ctx->log_to_syslog ($1); |
1169 | } elsif ($_ eq "nolog" ) { $ctx->log_cb (undef); |
1171 | } elsif ($_ eq "nolog" ) { $ctx->log_cb (undef); |
1170 | } elsif (/^\+(.+)$/ ) { $ctx->attach ($pkg->("$1")); |
1172 | } elsif (/^\+(.+)$/ ) { $ctx->attach ($pkg->("$1")); |
1171 | } elsif ($_ eq "+" ) { $ctx->slaves; |
1173 | } elsif ($_ eq "+" ) { $ctx->slaves; |
1172 | } elsif ($_ eq "off" or $_ eq "0") { $ctx->level (0); |
1174 | } elsif ($_ eq "off" or $_ eq "0") { $ctx->level (0); |
1173 | } elsif ($_ eq "all" ) { $ctx->level ("all"); |
1175 | } elsif ($_ eq "all" ) { $ctx->level ("all"); |