From f368faff5beca465a4c95fbef24f5743c3afc9a4 Mon Sep 17 00:00:00 2001
From: Chris Hallberg <crhallberg@gmail.com>
Date: Fri, 2 Sep 2016 17:14:50 -0400
Subject: [PATCH] Verbosity trait for Loggers.

---
 module/VuFind/src/VuFind/Log/Writer/Db.php    | 28 ++---------------
 module/VuFind/src/VuFind/Log/Writer/Mail.php  | 30 +++----------------
 module/VuFind/src/VuFind/Log/Writer/Post.php  | 23 ++------------
 module/VuFind/src/VuFind/Log/Writer/Slack.php |  2 +-
 .../VuFind/src/VuFind/Log/Writer/Stream.php   | 28 ++---------------
 5 files changed, 14 insertions(+), 97 deletions(-)

diff --git a/module/VuFind/src/VuFind/Log/Writer/Db.php b/module/VuFind/src/VuFind/Log/Writer/Db.php
index d29815b6ee9..c467468935f 100644
--- a/module/VuFind/src/VuFind/Log/Writer/Db.php
+++ b/module/VuFind/src/VuFind/Log/Writer/Db.php
@@ -38,24 +38,7 @@ namespace VuFind\Log\Writer;
  */
 class Db extends \Zend\Log\Writer\Db
 {
-    /**
-     * Holds the verbosity level
-     *
-     * @var int
-     */
-    protected $verbosity = 1;
-
-    /**
-     * Set verbosity
-     *
-     * @param integer $verb verbosity setting
-     *
-     * @return void
-     */
-    public function setVerbosity($verb)
-    {
-        $this->verbosity = $verb;
-    }
+    use VerbosityTrait;
 
     /**
      * Write a message to the log.
@@ -67,12 +50,7 @@ class Db extends \Zend\Log\Writer\Db
      */
     protected function doWrite(array $event)
     {
-        // Apply verbosity filter:
-        if (is_array($event['message'])) {
-            $event['message'] = $event['message'][$this->verbosity];
-        }
-
-        // Call parent method:
-        return parent::doWrite($event);
+        // Apply verbosity, Call parent method:
+        return parent::doWrite($this->applyVerbosity($event));
     }
 }
diff --git a/module/VuFind/src/VuFind/Log/Writer/Mail.php b/module/VuFind/src/VuFind/Log/Writer/Mail.php
index 310bc4a29e9..853fbc03ae6 100644
--- a/module/VuFind/src/VuFind/Log/Writer/Mail.php
+++ b/module/VuFind/src/VuFind/Log/Writer/Mail.php
@@ -38,25 +38,8 @@ namespace VuFind\Log\Writer;
  */
 class Mail extends \Zend\Log\Writer\Mail
 {
-    /**
-     * Holds the verbosity level
-     *
-     * @var int
-     */
-    protected $verbosity = 1;
-
-    /**
-     * Set verbosity
-     *
-     * @param integer $verb verbosity setting
-     *
-     * @return void
-     */
-    public function setVerbosity($verb)
-    {
-        $this->verbosity = $verb;
-    }
-
+    use VerbosityTrait;
+    
     /**
      * Write a message to the log.
      *
@@ -67,12 +50,7 @@ class Mail extends \Zend\Log\Writer\Mail
      */
     protected function doWrite(array $event)
     {
-        // Apply verbosity filter:
-        if (is_array($event['message'])) {
-            $event['message'] = $event['message'][$this->verbosity];
-        }
-
-        // Call parent method:
-        return parent::doWrite($event);
+        // Apply verbosity, Call parent method:
+        return parent::doWrite($this->applyVerbosity($event));
     }
 }
diff --git a/module/VuFind/src/VuFind/Log/Writer/Post.php b/module/VuFind/src/VuFind/Log/Writer/Post.php
index 7e76435026d..21d38cc24eb 100644
--- a/module/VuFind/src/VuFind/Log/Writer/Post.php
+++ b/module/VuFind/src/VuFind/Log/Writer/Post.php
@@ -39,6 +39,8 @@ use Zend\Http\Client;
  */
 class Post extends \Zend\Log\Writer\AbstractWriter
 {
+    use VerbosityTrait;
+
     /**
      * Holds the verbosity level
      *
@@ -53,13 +55,6 @@ class Post extends \Zend\Log\Writer\AbstractWriter
      */
     protected $client = null;
 
-    /**
-     * Holds the verbosity level
-     *
-     * @var int
-     */
-    protected $verbosity = 1;
-
     /**
      * Content type
      *
@@ -79,18 +74,6 @@ class Post extends \Zend\Log\Writer\AbstractWriter
         $this->client = $client;
     }
 
-    /**
-     * Set verbosity
-     *
-     * @param integer $verb verbosity setting
-     *
-     * @return void
-     */
-    public function setVerbosity($verb)
-    {
-        $this->verbosity = $verb;
-    }
-
     /**
      * Set verbosity
      *
@@ -134,7 +117,7 @@ class Post extends \Zend\Log\Writer\AbstractWriter
         $this->client->setUri($this->url);
         $this->client->setMethod('POST');
         $this->client->setEncType($this->contentType);
-        $this->client->setRawBody($this->getBody($event));
+        $this->client->setRawBody($this->getBody($this->applyVerbosity($event)));
         // Send
         $response = $this->client->send();
     }
diff --git a/module/VuFind/src/VuFind/Log/Writer/Slack.php b/module/VuFind/src/VuFind/Log/Writer/Slack.php
index 26284522ac5..36f4f6b8444 100644
--- a/module/VuFind/src/VuFind/Log/Writer/Slack.php
+++ b/module/VuFind/src/VuFind/Log/Writer/Slack.php
@@ -101,7 +101,7 @@ class Slack extends Post
             'channel' => $this->channel,
             'username' => $this->username,
             'text' => $this->messageIcons[$event['priority']]
-                . $this->formatter->format($event) . PHP_EOL
+                . $this->formatter->format($this->applyVerbosity($event)) . PHP_EOL
         ];
         return json_encode($data);
     }
diff --git a/module/VuFind/src/VuFind/Log/Writer/Stream.php b/module/VuFind/src/VuFind/Log/Writer/Stream.php
index 0c89fb0bdf8..eca9096fb49 100644
--- a/module/VuFind/src/VuFind/Log/Writer/Stream.php
+++ b/module/VuFind/src/VuFind/Log/Writer/Stream.php
@@ -38,24 +38,7 @@ namespace VuFind\Log\Writer;
  */
 class Stream extends \Zend\Log\Writer\Stream
 {
-    /**
-     * Holds the verbosity level
-     *
-     * @var int
-     */
-    protected $verbosity = 1;
-
-    /**
-     * Set verbosity
-     *
-     * @param integer $verb verbosity setting
-     *
-     * @return void
-     */
-    public function setVerbosity($verb)
-    {
-        $this->verbosity = $verb;
-    }
+    use VerbosityTrait;
 
     /**
      * Write a message to the log.
@@ -67,12 +50,7 @@ class Stream extends \Zend\Log\Writer\Stream
      */
     protected function doWrite(array $event)
     {
-        // Apply verbosity filter:
-        if (is_array($event['message'])) {
-            $event['message'] = $event['message'][$this->verbosity];
-        }
-
-        // Call parent method:
-        return parent::doWrite($event);
+        // Apply verbosity, Call parent method:
+        return parent::doWrite($this->applyVerbosity($event));
     }
 }
-- 
GitLab