]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 1442288 - Bugzilla::Logging should log when a program is being run interactively
authorDylan William Hardison <dylan@hardison.net>
Fri, 2 Mar 2018 02:17:23 +0000 (21:17 -0500)
committerGitHub <noreply@github.com>
Fri, 2 Mar 2018 02:17:23 +0000 (21:17 -0500)
Bugzilla/Logging.pm
conf/log4perl-json.conf
conf/log4perl-syslog.conf
conf/log4perl-test.conf

index c10f4c125e3a1a2bd23d538e4a9a08567456be8f..1d42f1ffdc7baf1700a48a510c59c85ee29a5421 100644 (file)
@@ -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;
index 41562034e1e21f312ec7e322e07f652bd82bec2f..c5d7e2c1fd9d58e223aa678f39d6780705a6fec9 100644 (file)
@@ -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
index 32f3d4c11a4a1f4f22b7cc95c260d193ea0782ec..c21c240d89d3571e93670294140f3f64d02c41ce 100644 (file)
@@ -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
index 7f2309c80c14bc6e80816a7aab16c761b0cc9d6a..eda81d31b9eabe7fb1b988666538b69fbd158c2b 100644 (file)
@@ -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