From b5ece2542a5416c97f3b9e060b877404349fafe6 Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Thu, 1 Mar 2018 21:17:23 -0500 Subject: Bug 1442288 - Bugzilla::Logging should log when a program is being run interactively --- Bugzilla/Logging.pm | 93 ++++++++++++++++++++++++++++++++++++++++++++++- conf/log4perl-json.conf | 22 +++++++---- conf/log4perl-syslog.conf | 21 +++++++---- conf/log4perl-test.conf | 20 ++++++---- 4 files changed, 131 insertions(+), 25 deletions(-) diff --git a/Bugzilla/Logging.pm b/Bugzilla/Logging.pm index c10f4c125..1d42f1ffd 100644 --- a/Bugzilla/Logging.pm +++ b/Bugzilla/Logging.pm @@ -14,12 +14,101 @@ use Log::Log4perl; use Log::Log4perl::MDC; use File::Spec::Functions qw(rel2abs); use Bugzilla::Constants qw(bz_locations); +use English qw(-no_match_vars $PROGRAM_NAME); BEGIN { - my $file = $ENV{LOG4PERL_CONFIG_FILE} // "log4perl-syslog.conf"; + my $file = $ENV{LOG4PERL_CONFIG_FILE} // 'log4perl-syslog.conf'; Log::Log4perl::Logger::create_custom_level('NOTICE', 'WARN', 5, 2); Log::Log4perl->init(rel2abs($file, bz_locations->{confdir})); - Log::Log4perl->get_logger(__PACKAGE__)->debug("logging enabled in $0"); + Log::Log4perl->get_logger(__PACKAGE__)->trace("logging enabled in $PROGRAM_NAME"); +} + +# this is copied from Log::Log4perl's :easy handling, +# except we also export NOTICE. +sub import { + my $caller_pkg = caller; + + return 1 if $Log::Log4perl::IMPORT_CALLED{$caller_pkg}++; + + # Define default logger object in caller's package + my $logger = Log::Log4perl->get_logger("$caller_pkg"); + + # Define DEBUG, INFO, etc. routines in caller's package + for (qw(TRACE DEBUG INFO NOTICE WARN ERROR FATAL ALWAYS)) { + my $level = $_; + $level = 'OFF' if $level eq 'ALWAYS'; + my $lclevel = lc $_; + Log::Log4perl::easy_closure_create( + $caller_pkg, + $_, + sub { + Log::Log4perl::Logger::init_warn() + unless $Log::Log4perl::Logger::INITIALIZED or $Log::Log4perl::Logger::NON_INIT_WARNED; + $logger->{$level}->( $logger, @_, $level ); + }, + $logger + ); + } + + # Define LOGCROAK, LOGCLUCK, etc. routines in caller's package + for (qw(LOGCROAK LOGCLUCK LOGCARP LOGCONFESS)) { + my $method = 'Log::Log4perl::Logger::' . lc $_; + + Log::Log4perl::easy_closure_create( + $caller_pkg, + $_, + sub { + unshift @_, $logger; + goto &$method; + }, + $logger + ); + } + + # Define LOGDIE, LOGWARN + Log::Log4perl::easy_closure_create( + $caller_pkg, + 'LOGDIE', + sub { + Log::Log4perl::Logger::init_warn() + unless $Log::Log4perl::Logger::INITIALIZED or $Log::Log4perl::Logger::NON_INIT_WARNED; + $logger->{FATAL}->( $logger, @_, 'FATAL' ); + $Log::Log4perl::LOGDIE_MESSAGE_ON_STDERR + ? CORE::die( Log::Log4perl::Logger::callerline( join '', @_ ) ) + : exit $Log::Log4perl::LOGEXIT_CODE; + }, + $logger + ); + + Log::Log4perl::easy_closure_create( + $caller_pkg, + 'LOGEXIT', + sub { + Log::Log4perl::Logger::init_warn() + unless $Log::Log4perl::Logger::INITIALIZED or $Log::Log4perl::Logger::NON_INIT_WARNED; + $logger->{FATAL}->( $logger, @_, 'FATAL' ); + exit $Log::Log4perl::LOGEXIT_CODE; + }, + $logger + ); + + Log::Log4perl::easy_closure_create( + $caller_pkg, + 'LOGWARN', + sub { + Log::Log4perl::Logger::init_warn() + unless $Log::Log4perl::Logger::INITIALIZED or $Log::Log4perl::Logger::NON_INIT_WARNED; + $logger->{WARN}->( $logger, @_, 'WARN' ); + CORE::warn( Log::Log4perl::Logger::callerline( join '', @_ ) ) + if $Log::Log4perl::LOGDIE_MESSAGE_ON_STDERR; + }, + $logger + ); +} + +sub is_interactive { + state $is_tty = -t STDOUT || -t STDIN; + return $is_tty || $ENV{"Bugzilla.pm"} && Bugzilla->usage_mode == Bugzilla::Constants::USAGE_MODE_CMDLINE; } 1; diff --git a/conf/log4perl-json.conf b/conf/log4perl-json.conf index 41562034e..c5d7e2c1f 100644 --- a/conf/log4perl-json.conf +++ b/conf/log4perl-json.conf @@ -1,16 +1,22 @@ -log4perl.rootLogger = INFO, Socket -log4perl.appender.Socket = Log::Log4perl::Appender::Socket -log4perl.appender.Socket.PeerAddr=127.0.0.1 -log4perl.appender.Socket.PeerPort=5880 -log4perl.appender.Socket.defer_connection=1 +log4perl.rootLogger = INFO, Cereal, Screen +log4perl.appender.Cereal = Log::Log4perl::Appender::Socket +log4perl.appender.Cereal.PeerAddr=127.0.0.1 +log4perl.appender.Cereal.PeerPort=5880 +log4perl.appender.Cereal.defer_connection=1 # This class is currently bundled with bugzilla -log4perl.appender.Socket.layout = Log::Log4perl::Layout::Mozilla +log4perl.appender.Cereal.layout = Log::Log4perl::Layout::Mozilla # lines longer than this will not be logged in detail. # instead a scary message with a much higher severity will be logged. -log4perl.appender.Socket.layout.max_json_length = 16384 +log4perl.appender.Cereal.layout.max_json_length = 16384 # The default is Bugzilla. This is the "Logger" field # in https://wiki.mozilla.org/Firefox/Services/Logging#MozLog_JSON_schema #and it might be useful to pass in different values for different jobs. -log4perl.appender.Socket.layout.name = Bugzilla +log4perl.appender.Cereal.layout.name = Bugzilla + +log4perl.filter.IS_INTERACTIVE = sub { Bugzilla::Logging::is_interactive() } +log4perl.appender.Screen = Log::Log4perl::Appender::Screen +log4perl.appender.Screen.Filter = IS_INTERACTIVE +log4perl.appender.Screen.layout = Log::Log4perl::Layout::PatternLayout +log4perl.appender.Screen.layout.ConversionPattern = %-5.5p [%d] [%c] %m{chomp} at %F line %L (%M)%n diff --git a/conf/log4perl-syslog.conf b/conf/log4perl-syslog.conf index 32f3d4c11..c21c240d8 100644 --- a/conf/log4perl-syslog.conf +++ b/conf/log4perl-syslog.conf @@ -1,9 +1,14 @@ -log4perl.rootLogger = INFO, syslog -log4perl.appender.syslog = Log::Dispatch::Syslog -log4perl.appender.syslog.min_level = notice -log4perl.appender.syslog.ident = apache -log4perl.appender.syslog.facility = local4 -log4perl.appender.syslog.logopt = cons,pid -log4perl.appender.syslog.layout = Log::Log4perl::Layout::PatternLayout -log4perl.appender.syslog.layout.ConversionPattern = [%c] %m{chomp}%n +log4perl.rootLogger = INFO, Syslog, Screen +log4perl.appender.Syslog = Log::Dispatch::Syslog +log4perl.appender.Syslog.min_level = notice +log4perl.appender.Syslog.ident = apache +log4perl.appender.Syslog.facility = local4 +log4perl.appender.Syslog.logopt = cons,pid +log4perl.appender.Syslog.layout = Log::Log4perl::Layout::PatternLayout +log4perl.appender.Syslog.layout.ConversionPattern = [%c] %m{chomp}%n +log4perl.filter.IS_INTERACTIVE = sub { Bugzilla::Logging::is_interactive() } +log4perl.appender.Screen = Log::Log4perl::Appender::Screen +log4perl.appender.Screen.Filter = IS_INTERACTIVE +log4perl.appender.Screen.layout = Log::Log4perl::Layout::PatternLayout +log4perl.appender.Screen.layout.ConversionPattern = %-5.5p [%d] [%c] %m{chomp} at %F line %L (%M)%n diff --git a/conf/log4perl-test.conf b/conf/log4perl-test.conf index 7f2309c80..eda81d31b 100644 --- a/conf/log4perl-test.conf +++ b/conf/log4perl-test.conf @@ -1,7 +1,13 @@ -log4perl.rootLogger = DEBUG, DebugSocket -log4perl.appender.DebugSocket = Log::Log4perl::Appender::Socket -log4perl.appender.DebugSocket.PeerAddr=127.0.0.1 -log4perl.appender.DebugSocket.PeerPort=5880 -log4perl.appender.DebugSocket.defer_connection=1 -log4perl.appender.DebugSocket.layout = Log::Log4perl::Layout::PatternLayout -log4perl.appender.DebugSocket.layout.ConversionPattern = [%d] [%c] <%p> %m{chomp} at %F line %L (%M)%n +log4perl.rootLogger = DEBUG, Cereal, Screen +log4perl.appender.Cereal = Log::Log4perl::Appender::Socket +log4perl.appender.Cereal.PeerAddr=127.0.0.1 +log4perl.appender.Cereal.PeerPort=5880 +log4perl.appender.Cereal.defer_connection=1 +log4perl.appender.Cereal.layout = Log::Log4perl::Layout::PatternLayout +log4perl.appender.Cereal.layout.ConversionPattern = %-5.5p [%d] [%c] %m{chomp} at %F line %L (%M)%n + +log4perl.filter.IS_INTERACTIVE = sub { Bugzilla::Logging::is_interactive() } +log4perl.appender.Screen = Log::Log4perl::Appender::Screen +log4perl.appender.Screen.Filter = IS_INTERACTIVE +log4perl.appender.Screen.layout = Log::Log4perl::Layout::PatternLayout +log4perl.appender.Screen.layout.ConversionPattern = %-5.5p [%d] [%c] %m{chomp} at %F line %L (%M)%n \ No newline at end of file -- cgit v1.2.3-24-g4f1b