From 9fe5682b3f7a7890ac07340864a5e27d73a8a839 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Fri, 6 May 2016 22:02:28 +0200 Subject: Spline::Log: Logging to syslog by default Logging to stderr is still available. --- Spline/Log.pm | 51 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file 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 -- cgit v1.2.3-1-g7c22