diff options
author | Alexander Sulfrian <alex@spline.inf.fu-berlin.de> | 2016-05-06 22:02:28 +0200 |
---|---|---|
committer | wartung <wartung@vm-mail.spline.inf.fu-berlin.de> | 2016-05-06 22:02:28 +0200 |
commit | 9fe5682b3f7a7890ac07340864a5e27d73a8a839 (patch) | |
tree | 5b2f73cd55ff980d31b38363d51b7222206798be | |
parent | b02f9d4c30b96b011a5e72730219982cd7c6d6da (diff) | |
download | dmarc-9fe5682b3f7a7890ac07340864a5e27d73a8a839.tar.gz dmarc-9fe5682b3f7a7890ac07340864a5e27d73a8a839.tar.bz2 dmarc-9fe5682b3f7a7890ac07340864a5e27d73a8a839.zip |
Spline::Log: Logging to syslog by default
Logging to stderr is still available.
-rw-r--r-- | Spline/Log.pm | 51 |
1 files changed, 45 insertions, 6 deletions
diff --git a/Spline/Log.pm b/Spline/Log.pm index f56d5f0..32080dc 100644 --- a/Spline/Log.pm +++ b/Spline/Log.pm @@ -9,6 +9,7 @@ Spline::Log - Utilities for logging use Spline::Log qw(...); + set_stderr($bool); set_verbose($bool); set_log_context($context); debug($message); @@ -24,9 +25,12 @@ use strict; use warnings; use feature 'say'; +use Sys::Syslog; + use base 'Exporter'; our @EXPORT = qw(); our @EXPORT_OK = qw( + set_stderr set_verbose set_log_context debug @@ -35,6 +39,28 @@ our @EXPORT_OK = qw( my $context = undef; my $verbose = 0; +my $syslog = 0; + +=head2 set_stderr + + set_stderr($bool); + +=cut + +sub set_stderr($) { + my $value = shift; + + if ($value) { + if (defined $syslog && $syslog) { + closelog(); + } + + $syslog = undef; + } + else { + $syslog = 0 unless defined $syslog; + } +} =head2 set_verbose @@ -88,7 +114,7 @@ sub debug($) { my $msg = shift; if ($verbose) { - _log($msg); + _log('DEBUG', $msg); } } @@ -103,7 +129,7 @@ prepended with the logging context (if defined). sub info($) { my $msg = shift; - _log($msg); + _log('INFO', $msg); } =head2 _get_context @@ -123,18 +149,31 @@ sub _get_context() { =head2 _log - _log($msg); + _log($level, $msg); Log the supplied message. The message is prepended with the logging context (if defined). So the log format is something like this: CONTEXT: MESSAGE +The supplied level is only used for syslog. + =cut -sub _log($) { - my $msg = shift; - say _get_context() . $msg; +sub _log($$) { + my ($level, $msg) = @_; + + my $output = _get_context() . $msg; + if (defined $syslog) { + unless ($syslog) { + openlog('dmarc_milter', 'ndelay,pid', 'mail'); + } + + syslog($level, $output); + } + else { + say STDERR $output; + } } =head1 AUTHOR |