Adamw has submitted this change and it was merged.

Change subject: Allow the Tagged Logger to Alter Context
......................................................................


Allow the Tagged Logger to Alter Context

Because it was silly to have to call the Logger instance when
you had a perfectly good tagged logger around.

A review comment suggested that I not access the LoggingContext
object as a static; so that change was made as well.

Change-Id: I6de8f8362e552a4d1750a2b25c08549848f8a979
---
M Core/Logging/Logger.php
M Core/Logging/TaggedLogger.php
M Maintenance/MaintenanceBase.php
3 files changed, 70 insertions(+), 8 deletions(-)

Approvals:
  Adamw: Verified; Looks good to me, approved



diff --git a/Core/Logging/Logger.php b/Core/Logging/Logger.php
index 1e27744..155860e 100644
--- a/Core/Logging/Logger.php
+++ b/Core/Logging/Logger.php
@@ -6,7 +6,7 @@
 class Logger {
 
        /** @var LogContextHandler */
-       public static $context = null;
+       protected static $context = null;
 
        /** @var int The log level must be greater than this to be processed. */
        protected static $threshold = LOG_DEBUG;
@@ -66,6 +66,20 @@
 
        /* --- CONTEXT HELPER METHODS --- */
        /**
+        * Obtain the logging context. Only one context will be present in an
+        * instantiation of Logger (which implies only one per process.)
+        *
+        * @throws SmashPigException if logger has not been initialized
+        * @return LogContextHandler
+        */
+       public static function getContext() {
+               if ( Logger::$context === null ) {
+                       throw new SmashPigException( "No context available. 
Logger not initialized?" );
+               }
+               return Logger::$context;
+       }
+
+       /**
         * Enters a new context with the current context as its parent.
         * Shadows @ref LogContextHandler->enterContext()
         *
diff --git a/Core/Logging/TaggedLogger.php b/Core/Logging/TaggedLogger.php
index 7d2293a..0b3c6f8 100644
--- a/Core/Logging/TaggedLogger.php
+++ b/Core/Logging/TaggedLogger.php
@@ -14,10 +14,58 @@
 
        protected $tag = null;
 
+       /** @var LogContextHandler */
+       protected $context = null;
+
        public function __construct( $tag = null ) {
                $this->tag = $tag;
+               $this->context = Logger::getContext();
        }
 
+       /* === CONTEXT HELPER METHODS === */
+       /**
+        * Enters a new context with the current context as its parent.
+        * Shadows @ref LogContextHandler->enterContext()
+        *
+        * @param string $name Child context name
+        */
+       public static function enterContext( $name ) {
+               Logger::enterContext( $name );
+       }
+
+       /**
+        * Renames the current logging context. Effects the log prefix used for 
all
+        * events under this context. May have adverse effects on logstreams 
that log
+        * in real time (IE: Syslog) because they will have logged items under 
the old
+        * context name.
+        *
+        * Shadows @ref LogContextHandler->renameContext()
+        *
+        * @param string   $newName     New name for the current context
+        * @param bool     $addLogEntry If false will not create a log line 
stating the name change
+        *
+        * @return string The old name of this context
+        */
+       public static function renameContext( $newName, $addLogEntry = true ) {
+               return Logger::renameContext( $newName, $addLogEntry );
+       }
+
+       /**
+        * Leaves the current context for the parent context. You may not leave 
the root
+        * context.
+        *
+        * Side effects include removing all stored log lines for this context. 
Before this
+        * happens all LogStreams have the opportunity to do last chance 
processing.
+        *
+        * Shadows @ref LogContextHandler->leaveContext()
+        *
+        * @return string|bool The current context name, or false if this is 
the root context
+        */
+       public static function leaveContext() {
+               return Logger::leaveContext();
+       }
+
+       /* === EVENT HANDLING === */
        /**
         * Log an immediate/critical failure. Will be immediately forwarded to 
the designated
         * error contact. Use this for things like database failures, top of 
PHP error stack
@@ -28,7 +76,7 @@
         * @param null|\Exception  $ex          Exception object relevant to 
the event, if any
         */
        public function alert( $msg, $data = null, $ex = null ) {
-               Logger::$context->addEventToContext( new LogEvent( LOG_ALERT, 
$msg, $this->tag, $data, $ex ) );
+               $this->context->addEventToContext( new LogEvent( LOG_ALERT, 
$msg, $this->tag, $data, $ex ) );
        }
 
        /**
@@ -41,7 +89,7 @@
         * @param null|\Exception  $ex          Exception object relevant to 
the event, if any
         */
        public function error( $msg, $data = null, $ex = null ) {
-               Logger::$context->addEventToContext( new LogEvent( LOG_ERR, 
$msg, $this->tag, $data, $ex ) );
+               $this->context->addEventToContext( new LogEvent( LOG_ERR, $msg, 
$this->tag, $data, $ex ) );
        }
 
        /**
@@ -53,7 +101,7 @@
         * @param null|\Exception  $ex          Exception object relevant to 
the event, if any
         */
        public function warning( $msg, $data = null, $ex = null ) {
-               Logger::$context->addEventToContext( new LogEvent( LOG_WARNING, 
$msg, $this->tag, $data, $ex ) );
+               $this->context->addEventToContext( new LogEvent( LOG_WARNING, 
$msg, $this->tag, $data, $ex ) );
        }
 
        /**
@@ -65,7 +113,7 @@
         * @param null|\Exception  $ex          Exception object relevant to 
the event, if any
         */
        public function notice( $msg, $data = null, $ex = null ) {
-               Logger::$context->addEventToContext( new LogEvent( LOG_NOTICE, 
$msg, $this->tag, $data, $ex ) );
+               $this->context->addEventToContext( new LogEvent( LOG_NOTICE, 
$msg, $this->tag, $data, $ex ) );
        }
 
        /**
@@ -77,7 +125,7 @@
         * @param null|\Exception  $ex          Exception object relevant to 
the event, if any
         */
        public function info( $msg, $data = null, $ex = null ) {
-               Logger::$context->addEventToContext( new LogEvent( LOG_INFO, 
$msg, $this->tag, $data, $ex ) );
+               $this->context->addEventToContext( new LogEvent( LOG_INFO, 
$msg, $this->tag, $data, $ex ) );
        }
 
        /**
@@ -89,6 +137,6 @@
         * @param null|\Exception  $ex          Exception object relevant to 
the event, if any
         */
        public function debug( $msg, $data = null, $ex = null ) {
-               Logger::$context->addEventToContext( new LogEvent( LOG_DEBUG, 
$msg, $this->tag, $data, $ex ) );
+               $this->context->addEventToContext( new LogEvent( LOG_DEBUG, 
$msg, $this->tag, $data, $ex ) );
        }
 }
\ No newline at end of file
diff --git a/Maintenance/MaintenanceBase.php b/Maintenance/MaintenanceBase.php
index bd3a528..486dc83 100644
--- a/Maintenance/MaintenanceBase.php
+++ b/Maintenance/MaintenanceBase.php
@@ -137,7 +137,7 @@
                        $config->val( 'logging/log-level' ),
                        $config
                );
-               Logger::$context->addLogStream( new ConsoleLogStream() );
+               Logger::getContext()->addLogStream( new ConsoleLogStream() );
 
                Logger::enterContext( Context::get()->getContextId() );
 

-- 
To view, visit https://gerrit.wikimedia.org/r/90284
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I6de8f8362e552a4d1750a2b25c08549848f8a979
Gerrit-PatchSet: 2
Gerrit-Project: wikimedia/fundraising/SmashPig
Gerrit-Branch: master
Gerrit-Owner: Mwalker <[email protected]>
Gerrit-Reviewer: Adamw <[email protected]>
Gerrit-Reviewer: Katie Horn <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to