From e122a4e37b3eef21fc6fc32ed7d31c56c38d673f Mon Sep 17 00:00:00 2001
From: Matthieu Napoli <matthieu@mnapoli.fr>
Date: Mon, 2 Feb 2015 11:45:32 +1300
Subject: [PATCH] Moved the monolog configuration and code into a Monolog
 plugin

---
 config/global.ini.php                         |  1 +
 config/global.php                             | 84 +---------------
 core/Updates/2.11.0-b5.php                    | 23 +++++
 core/Version.php                              |  2 +-
 misc/cron/archive.php                         |  2 +-
 .../Formatter/LineMessageFormatter.php        |  2 +-
 .../Monolog}/Handler/DatabaseHandler.php      |  2 +-
 .../Monolog}/Handler/FileHandler.php          |  2 +-
 .../Handler/WebNotificationHandler.php        |  2 +-
 plugins/Monolog/Monolog.php                   | 15 +++
 .../Monolog}/Processor/ClassNameProcessor.php | 18 +++-
 .../Processor/ExceptionToTextProcessor.php    |  2 +-
 .../Monolog}/Processor/RequestIdProcessor.php |  2 +-
 .../Monolog}/Processor/SprintfProcessor.php   |  2 +-
 .../Integration}/Fixture/LoggerWrapper.php    |  2 +-
 .../Monolog/Test/Integration}/LogTest.php     | 26 +++--
 .../Formatter/LineMessageFormatterTest.php    |  7 +-
 .../Processor/ClassNameProcessorTest.php      |  9 +-
 .../ExceptionToTextProcessorTest.php          |  9 +-
 .../Processor/RequestIdProcessorTest.php      |  8 +-
 .../Unit}/Processor/SprintfProcessorTest.php  |  7 +-
 plugins/Monolog/config/config.php             | 99 +++++++++++++++++++
 22 files changed, 192 insertions(+), 134 deletions(-)
 create mode 100644 core/Updates/2.11.0-b5.php
 rename {core/Log => plugins/Monolog}/Formatter/LineMessageFormatter.php (97%)
 rename {core/Log => plugins/Monolog}/Handler/DatabaseHandler.php (95%)
 rename {core/Log => plugins/Monolog}/Handler/FileHandler.php (94%)
 rename {core/Log => plugins/Monolog}/Handler/WebNotificationHandler.php (97%)
 create mode 100644 plugins/Monolog/Monolog.php
 rename {core/Log => plugins/Monolog}/Processor/ClassNameProcessor.php (70%)
 rename {core/Log => plugins/Monolog}/Processor/ExceptionToTextProcessor.php (97%)
 rename {core/Log => plugins/Monolog}/Processor/RequestIdProcessor.php (94%)
 rename {core/Log => plugins/Monolog}/Processor/SprintfProcessor.php (95%)
 rename {tests/PHPUnit/Integration/Log => plugins/Monolog/Test/Integration}/Fixture/LoggerWrapper.php (82%)
 rename {tests/PHPUnit/Integration/Log => plugins/Monolog/Test/Integration}/LogTest.php (90%)
 rename {tests/PHPUnit/Unit/Log => plugins/Monolog/Test/Unit}/Formatter/LineMessageFormatterTest.php (91%)
 rename {tests/PHPUnit/Unit/Log => plugins/Monolog/Test/Unit}/Processor/ClassNameProcessorTest.php (76%)
 rename {tests/PHPUnit/Unit/Log => plugins/Monolog/Test/Unit}/Processor/ExceptionToTextProcessorTest.php (88%)
 rename {tests/PHPUnit/Unit/Log => plugins/Monolog/Test/Unit}/Processor/RequestIdProcessorTest.php (85%)
 rename {tests/PHPUnit/Unit/Log => plugins/Monolog/Test/Unit}/Processor/SprintfProcessorTest.php (89%)
 create mode 100644 plugins/Monolog/config/config.php

diff --git a/config/global.ini.php b/config/global.ini.php
index 3a71c90683..5dba546c7b 100644
--- a/config/global.ini.php
+++ b/config/global.ini.php
@@ -732,6 +732,7 @@ Plugins[] = ExampleAPI
 Plugins[] = ExampleRssWidget
 Plugins[] = Provider
 Plugins[] = Feedback
+Plugins[] = Monolog
 
 Plugins[] = Login
 Plugins[] = UsersManager
diff --git a/config/global.php b/config/global.php
index af8272d3de..aff73fcdba 100644
--- a/config/global.php
+++ b/config/global.php
@@ -1,8 +1,6 @@
 <?php
 
 use Interop\Container\ContainerInterface;
-use Monolog\Logger;
-use Piwik\Log;
 use Piwik\Cache\Eager;
 use Piwik\SettingsServer;
 
@@ -52,87 +50,7 @@ return array(
         return 'eagercache-' . str_replace(array('.', '-'), '', \Piwik\Version::VERSION) . '-';
     },
 
-    // Log
-    'Psr\Log\LoggerInterface' => DI\object('Monolog\Logger')
-        ->constructor('piwik', DI\link('log.handlers'), DI\link('log.processors')),
-    'log.handlers' => DI\factory(function (ContainerInterface $c) {
-        if ($c->has('ini.log.log_writers')) {
-            $writerNames = $c->get('ini.log.log_writers');
-        } else {
-            return array();
-        }
-        $classes = array(
-            'file'     => 'Piwik\Log\Handler\FileHandler',
-            'screen'   => 'Piwik\Log\Handler\WebNotificationHandler',
-            'database' => 'Piwik\Log\Handler\DatabaseHandler',
-        );
-        $writerNames = array_map('trim', $writerNames);
-        $writers = array();
-        foreach ($writerNames as $writerName) {
-            if (isset($classes[$writerName])) {
-                $writers[$writerName] = $c->get($classes[$writerName]);
-            }
-        }
-        return array_values($writers);
-    }),
-    'log.processors' => array(
-        DI\link('Piwik\Log\Processor\ClassNameProcessor'),
-        DI\link('Piwik\Log\Processor\RequestIdProcessor'),
-        DI\link('Piwik\Log\Processor\ExceptionToTextProcessor'),
-        DI\link('Piwik\Log\Processor\SprintfProcessor'),
-        DI\link('Monolog\Processor\PsrLogMessageProcessor'),
-    ),
-    'Piwik\Log\Handler\FileHandler' => DI\object()
-        ->constructor(DI\link('log.file.filename'), DI\link('log.level'))
-        ->method('setFormatter', DI\link('Piwik\Log\Formatter\LineMessageFormatter')),
-    'Piwik\Log\Handler\DatabaseHandler' => DI\object()
-        ->constructor(DI\link('log.level'))
-        ->method('setFormatter', DI\link('Piwik\Log\Formatter\LineMessageFormatter')),
-    'Piwik\Log\Handler\WebNotificationHandler' => DI\object()
-        ->constructor(DI\link('log.level'))
-        ->method('setFormatter', DI\link('Piwik\Log\Formatter\LineMessageFormatter')),
-    'log.level' => DI\factory(function (ContainerInterface $c) {
-        if ($c->has('ini.log.log_level')) {
-            $level = strtoupper($c->get('ini.log.log_level'));
-            if (!empty($level) && defined('Piwik\Log::'.strtoupper($level))) {
-                return Log::getMonologLevel(constant('Piwik\Log::'.strtoupper($level)));
-            }
-        }
-        return Logger::WARNING;
-    }),
-    'log.file.filename' => DI\factory(function (ContainerInterface $c) {
-        $logPath = $c->get('ini.log.logger_file_path');
-
-        // Absolute path
-        if (strpos($logPath, '/') === 0) {
-            return $logPath;
-        }
-
-        // Remove 'tmp/' at the beginning
-        if (strpos($logPath, 'tmp/') === 0) {
-            $logPath = substr($logPath, strlen('tmp'));
-        }
-
-        if (empty($logPath)) {
-            // Default log file
-            $logPath = '/logs/piwik.log';
-        }
-
-        $logPath = $c->get('path.tmp') . $logPath;
-        if (is_dir($logPath)) {
-            $logPath .= '/piwik.log';
-        }
-
-        return $logPath;
-    }),
-    'Piwik\Log\Formatter\LineMessageFormatter' => DI\object()
-        ->constructor(DI\link('log.format')),
-    'log.format' => DI\factory(function (ContainerInterface $c) {
-        if ($c->has('ini.log.string_message_format')) {
-            return $c->get('ini.log.string_message_format');
-        }
-        return '%level% %tag%[%datetime%] %message%';
-    }),
+    'Psr\Log\LoggerInterface' => DI\object('Psr\Log\NullLogger'),
 
     'Piwik\Translation\Loader\LoaderInterface' => DI\object('Piwik\Translation\Loader\LoaderCache')
         ->constructor(DI\link('Piwik\Translation\Loader\JsonFileLoader')),
diff --git a/core/Updates/2.11.0-b5.php b/core/Updates/2.11.0-b5.php
new file mode 100644
index 0000000000..1a75fd9f88
--- /dev/null
+++ b/core/Updates/2.11.0-b5.php
@@ -0,0 +1,23 @@
+<?php
+/**
+ * Piwik - free/libre analytics platform
+ *
+ * @link http://piwik.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
+ */
+
+namespace Piwik\Updates;
+
+use Piwik\Plugin\Manager;
+use Piwik\Updates;
+
+class Updates_2_11_0_b5 extends Updates
+{
+    static function update()
+    {
+        try {
+            Manager::getInstance()->activatePlugin('Monolog');
+        } catch (\Exception $e) {
+        }
+    }
+}
diff --git a/core/Version.php b/core/Version.php
index f67f64b677..644f25ec21 100644
--- a/core/Version.php
+++ b/core/Version.php
@@ -20,7 +20,7 @@ final class Version
      * The current Piwik version.
      * @var string
      */
-    const VERSION = '2.11.0-b4';
+    const VERSION = '2.11.0-b5';
 
     public function isStableVersion($version)
     {
diff --git a/misc/cron/archive.php b/misc/cron/archive.php
index be81b9a6a7..eecd78946b 100644
--- a/misc/cron/archive.php
+++ b/misc/cron/archive.php
@@ -73,7 +73,7 @@ if (isset($_SERVER['argv']) && Piwik\Console::isSupported()) {
         /** @var \Monolog\Logger $logger */
         $logger = StaticContainer::get('Psr\Log\LoggerInterface');
         $handler = new StreamHandler('php://output', Logger::INFO);
-        $handler->setFormatter(StaticContainer::get('Piwik\Log\Formatter\LineMessageFormatter'));
+        $handler->setFormatter(StaticContainer::get('Piwik\Plugins\Monolog\Formatter\LineMessageFormatter'));
         $logger->pushHandler($handler);
     }
 
diff --git a/core/Log/Formatter/LineMessageFormatter.php b/plugins/Monolog/Formatter/LineMessageFormatter.php
similarity index 97%
rename from core/Log/Formatter/LineMessageFormatter.php
rename to plugins/Monolog/Formatter/LineMessageFormatter.php
index 7c38fc32df..6b81f2daec 100644
--- a/core/Log/Formatter/LineMessageFormatter.php
+++ b/plugins/Monolog/Formatter/LineMessageFormatter.php
@@ -6,7 +6,7 @@
  * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  */
 
-namespace Piwik\Log\Formatter;
+namespace Piwik\Plugins\Monolog\Formatter;
 
 use Monolog\Formatter\FormatterInterface;
 
diff --git a/core/Log/Handler/DatabaseHandler.php b/plugins/Monolog/Handler/DatabaseHandler.php
similarity index 95%
rename from core/Log/Handler/DatabaseHandler.php
rename to plugins/Monolog/Handler/DatabaseHandler.php
index 1cdcd0f8b9..99ccd68670 100644
--- a/core/Log/Handler/DatabaseHandler.php
+++ b/plugins/Monolog/Handler/DatabaseHandler.php
@@ -6,7 +6,7 @@
  * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  */
 
-namespace Piwik\Log\Handler;
+namespace Piwik\Plugins\Monolog\Handler;
 
 use Monolog\Handler\AbstractProcessingHandler;
 use Piwik\Common;
diff --git a/core/Log/Handler/FileHandler.php b/plugins/Monolog/Handler/FileHandler.php
similarity index 94%
rename from core/Log/Handler/FileHandler.php
rename to plugins/Monolog/Handler/FileHandler.php
index 57f31610c1..abfd632365 100644
--- a/core/Log/Handler/FileHandler.php
+++ b/plugins/Monolog/Handler/FileHandler.php
@@ -6,7 +6,7 @@
  * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  */
 
-namespace Piwik\Log\Handler;
+namespace Piwik\Plugins\Monolog\Handler;
 
 use Monolog\Handler\StreamHandler;
 use Piwik\Filechecks;
diff --git a/core/Log/Handler/WebNotificationHandler.php b/plugins/Monolog/Handler/WebNotificationHandler.php
similarity index 97%
rename from core/Log/Handler/WebNotificationHandler.php
rename to plugins/Monolog/Handler/WebNotificationHandler.php
index 37004d6521..edd2fc76c2 100644
--- a/core/Log/Handler/WebNotificationHandler.php
+++ b/plugins/Monolog/Handler/WebNotificationHandler.php
@@ -6,7 +6,7 @@
  * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  */
 
-namespace Piwik\Log\Handler;
+namespace Piwik\Plugins\Monolog\Handler;
 
 use Monolog\Handler\AbstractProcessingHandler;
 use Monolog\Logger;
diff --git a/plugins/Monolog/Monolog.php b/plugins/Monolog/Monolog.php
new file mode 100644
index 0000000000..79f42cdd41
--- /dev/null
+++ b/plugins/Monolog/Monolog.php
@@ -0,0 +1,15 @@
+<?php
+/**
+ * Piwik - free/libre analytics platform
+ *
+ * @link http://piwik.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
+ */
+
+namespace Piwik\Plugins\Monolog;
+
+use Piwik\Plugin;
+
+class Monolog extends Plugin
+{
+}
diff --git a/core/Log/Processor/ClassNameProcessor.php b/plugins/Monolog/Processor/ClassNameProcessor.php
similarity index 70%
rename from core/Log/Processor/ClassNameProcessor.php
rename to plugins/Monolog/Processor/ClassNameProcessor.php
index a5b211d7d3..3365f207db 100644
--- a/core/Log/Processor/ClassNameProcessor.php
+++ b/plugins/Monolog/Processor/ClassNameProcessor.php
@@ -6,7 +6,7 @@
  * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  */
 
-namespace Piwik\Log\Processor;
+namespace Piwik\Plugins\Monolog\Processor;
 
 use Piwik\Plugin;
 
@@ -52,7 +52,7 @@ class ClassNameProcessor
     private function getClassNameThatIsLogging($backtrace)
     {
         foreach ($backtrace as $line) {
-            if (isset($line['class']) && !in_array($line['class'], $this->skippedClasses)) {
+            if (isset($line['class'])) {
                 return $line['class'];
             }
         }
@@ -63,9 +63,19 @@ class ClassNameProcessor
     private function getBacktrace()
     {
         if (version_compare(phpversion(), '5.3.6', '>=')) {
-            return debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT);
+            $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT);
+        } else {
+            $backtrace = debug_backtrace();
         }
 
-        return debug_backtrace();
+        $skippedClasses = $this->skippedClasses;
+        $backtrace = array_filter($backtrace, function ($item) use ($skippedClasses) {
+            if (isset($item['class'])) {
+                return !in_array($item['class'], $skippedClasses);
+            }
+            return true;
+        });
+
+        return $backtrace;
     }
 }
diff --git a/core/Log/Processor/ExceptionToTextProcessor.php b/plugins/Monolog/Processor/ExceptionToTextProcessor.php
similarity index 97%
rename from core/Log/Processor/ExceptionToTextProcessor.php
rename to plugins/Monolog/Processor/ExceptionToTextProcessor.php
index c3d51ae9e5..daaaee479e 100644
--- a/core/Log/Processor/ExceptionToTextProcessor.php
+++ b/plugins/Monolog/Processor/ExceptionToTextProcessor.php
@@ -6,7 +6,7 @@
  * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  */
 
-namespace Piwik\Log\Processor;
+namespace Piwik\Plugins\Monolog\Processor;
 
 use Piwik\ErrorHandler;
 use Piwik\Log;
diff --git a/core/Log/Processor/RequestIdProcessor.php b/plugins/Monolog/Processor/RequestIdProcessor.php
similarity index 94%
rename from core/Log/Processor/RequestIdProcessor.php
rename to plugins/Monolog/Processor/RequestIdProcessor.php
index 46ae17372e..f36568193a 100644
--- a/core/Log/Processor/RequestIdProcessor.php
+++ b/plugins/Monolog/Processor/RequestIdProcessor.php
@@ -6,7 +6,7 @@
  * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  */
 
-namespace Piwik\Log\Processor;
+namespace Piwik\Plugins\Monolog\Processor;
 
 use Piwik\Common;
 
diff --git a/core/Log/Processor/SprintfProcessor.php b/plugins/Monolog/Processor/SprintfProcessor.php
similarity index 95%
rename from core/Log/Processor/SprintfProcessor.php
rename to plugins/Monolog/Processor/SprintfProcessor.php
index 9032f5ba7a..dafa46e5ba 100644
--- a/core/Log/Processor/SprintfProcessor.php
+++ b/plugins/Monolog/Processor/SprintfProcessor.php
@@ -6,7 +6,7 @@
  * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  */
 
-namespace Piwik\Log\Processor;
+namespace Piwik\Plugins\Monolog\Processor;
 
 /**
  * Processes a log message using `sprintf()`.
diff --git a/tests/PHPUnit/Integration/Log/Fixture/LoggerWrapper.php b/plugins/Monolog/Test/Integration/Fixture/LoggerWrapper.php
similarity index 82%
rename from tests/PHPUnit/Integration/Log/Fixture/LoggerWrapper.php
rename to plugins/Monolog/Test/Integration/Fixture/LoggerWrapper.php
index 07b8a04d20..0d5ea11264 100644
--- a/tests/PHPUnit/Integration/Log/Fixture/LoggerWrapper.php
+++ b/plugins/Monolog/Test/Integration/Fixture/LoggerWrapper.php
@@ -6,7 +6,7 @@
  * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  */
 
-namespace Piwik\Tests\Integration\Log\Fixture;
+namespace Piwik\Plugins\Monolog\Test\Integration\Fixture;
 
 use Piwik\Log;
 
diff --git a/tests/PHPUnit/Integration/Log/LogTest.php b/plugins/Monolog/Test/Integration/LogTest.php
similarity index 90%
rename from tests/PHPUnit/Integration/Log/LogTest.php
rename to plugins/Monolog/Test/Integration/LogTest.php
index 0b6516185e..c21a2f82f5 100644
--- a/tests/PHPUnit/Integration/Log/LogTest.php
+++ b/plugins/Monolog/Test/Integration/LogTest.php
@@ -6,7 +6,7 @@
  * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  */
 
-namespace Piwik\Tests\Integration\Log;
+namespace Piwik\Plugins\Monolog\Test\Integration;
 
 use Exception;
 use Piwik\Common;
@@ -15,8 +15,8 @@ use Piwik\Container\ContainerFactory;
 use Piwik\Container\StaticContainer;
 use Piwik\Db;
 use Piwik\Log;
+use Piwik\Plugins\Monolog\Test\Integration\Fixture\LoggerWrapper;
 use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
-use Piwik\Tests\Integration\Log\Fixture\LoggerWrapper;
 
 /**
  * @group Core
@@ -28,10 +28,10 @@ class LogTest extends IntegrationTestCase
     const STRING_MESSAGE_FORMAT = '[%tag%] %message%';
     const STRING_MESSAGE_FORMAT_SPRINTF = "[%s] %s";
 
-    public static $expectedExceptionOutput = '[Piwik\Tests\Integration\Log\LogTest] LogTest.php(120): dummy error message
+    public static $expectedExceptionOutput = '[Monolog] LogTest.php(120): dummy error message
   dummy backtrace';
 
-    public static $expectedErrorOutput = '[Piwik\Tests\Integration\Log\LogTest] dummyerrorfile.php(145): Unknown error (102) - dummy error string
+    public static $expectedErrorOutput = '[Monolog] dummyerrorfile.php(145): Unknown error (102) - dummy error string
   dummy backtrace';
 
     public function setUp()
@@ -82,7 +82,7 @@ class LogTest extends IntegrationTestCase
 
         Log::warning(self::TESTMESSAGE);
 
-        $this->checkBackend($backend, self::TESTMESSAGE, $formatMessage = true, $tag = __CLASS__);
+        $this->checkBackend($backend, self::TESTMESSAGE, $formatMessage = true, $tag = 'Monolog');
     }
 
     /**
@@ -94,7 +94,7 @@ class LogTest extends IntegrationTestCase
 
         Log::warning(self::TESTMESSAGE, " subst ");
 
-        $this->checkBackend($backend, sprintf(self::TESTMESSAGE, " subst "), $formatMessage = true, $tag = __CLASS__);
+        $this->checkBackend($backend, sprintf(self::TESTMESSAGE, " subst "), $formatMessage = true, $tag = 'Monolog');
     }
 
     /**
@@ -107,7 +107,7 @@ class LogTest extends IntegrationTestCase
         $error = new \ErrorException("dummy error string", 0, 102, "dummyerrorfile.php", 145);
         Log::error($error);
 
-        $this->checkBackend($backend, self::$expectedErrorOutput, $formatMessage = false, $tag = __CLASS__);
+        $this->checkBackend($backend, self::$expectedErrorOutput, $formatMessage = false, $tag = 'Monolog');
     }
 
     /**
@@ -120,7 +120,7 @@ class LogTest extends IntegrationTestCase
         $exception = new Exception("dummy error message");
         Log::error($exception);
 
-        $this->checkBackend($backend, self::$expectedExceptionOutput, $formatMessage = false, $tag = __CLASS__);
+        $this->checkBackend($backend, self::$expectedExceptionOutput, $formatMessage = false, $tag = 'Monolog');
     }
 
     /**
@@ -132,9 +132,7 @@ class LogTest extends IntegrationTestCase
 
         LoggerWrapper::doLog(self::TESTMESSAGE);
 
-        $tag = 'Piwik\Tests\Integration\Log\Fixture\LoggerWrapper';
-
-        $this->checkBackend($backend, self::TESTMESSAGE, $formatMessage = true, $tag);
+        $this->checkBackend($backend, self::TESTMESSAGE, $formatMessage = true, 'Monolog');
     }
 
     /**
@@ -159,9 +157,7 @@ class LogTest extends IntegrationTestCase
 
         LoggerWrapper::doLog(" \n   ".self::TESTMESSAGE."\n\n\n   \n");
 
-        $tag = 'Piwik\Tests\Integration\Log\Fixture\LoggerWrapper';
-
-        $this->checkBackend($backend, self::TESTMESSAGE, $formatMessage = true, $tag);
+        $this->checkBackend($backend, self::TESTMESSAGE, $formatMessage = true, 'Monolog');
     }
 
     /**
@@ -176,7 +172,7 @@ class LogTest extends IntegrationTestCase
 
         Log::info(self::TESTMESSAGE);
 
-        $this->checkBackend('database', self::TESTMESSAGE, $formatMessage = true, $tag = __CLASS__);
+        $this->checkBackend('database', self::TESTMESSAGE, $formatMessage = true, $tag = 'Monolog');
     }
 
     private function checkBackend($backend, $expectedMessage, $formatMessage = false, $tag = false)
diff --git a/tests/PHPUnit/Unit/Log/Formatter/LineMessageFormatterTest.php b/plugins/Monolog/Test/Unit/Formatter/LineMessageFormatterTest.php
similarity index 91%
rename from tests/PHPUnit/Unit/Log/Formatter/LineMessageFormatterTest.php
rename to plugins/Monolog/Test/Unit/Formatter/LineMessageFormatterTest.php
index 9b9aac78b1..2b53633e6d 100644
--- a/tests/PHPUnit/Unit/Log/Formatter/LineMessageFormatterTest.php
+++ b/plugins/Monolog/Test/Unit/Formatter/LineMessageFormatterTest.php
@@ -6,15 +6,14 @@
  * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  */
 
-namespace Piwik\Tests\Unit\Log\Formatter;
+namespace Piwik\Plugins\Monolog\Test\Unit\Formatter;
 
 use DateTime;
-use Piwik\Log\Formatter\LineMessageFormatter;
+use Piwik\Plugins\Monolog\Formatter\LineMessageFormatter;
 
 /**
- * @group Core
  * @group Log
- * @covers \Piwik\Log\Formatter\LineMessageFormatter
+ * @covers \Piwik\Plugins\Monolog\Formatter\LineMessageFormatter
  */
 class LineMessageFormatterTest extends \PHPUnit_Framework_TestCase
 {
diff --git a/tests/PHPUnit/Unit/Log/Processor/ClassNameProcessorTest.php b/plugins/Monolog/Test/Unit/Processor/ClassNameProcessorTest.php
similarity index 76%
rename from tests/PHPUnit/Unit/Log/Processor/ClassNameProcessorTest.php
rename to plugins/Monolog/Test/Unit/Processor/ClassNameProcessorTest.php
index 2e002196e2..b1b307a4d7 100644
--- a/tests/PHPUnit/Unit/Log/Processor/ClassNameProcessorTest.php
+++ b/plugins/Monolog/Test/Unit/Processor/ClassNameProcessorTest.php
@@ -6,14 +6,13 @@
  * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  */
 
-namespace Piwik\Tests\Unit\Log\Processor;
+namespace Piwik\Plugins\Monolog\Test\Unit\Processor;
 
-use Piwik\Log\Processor\ClassNameProcessor;
+use Piwik\Plugins\Monolog\Processor\ClassNameProcessor;
 
 /**
- * @group Core
  * @group Log
- * @covers \Piwik\Log\Processor\ClassNameProcessor
+ * @covers \Piwik\Plugins\Monolog\Processor\ClassNameProcessor
  */
 class ClassNameProcessorTest extends \PHPUnit_Framework_TestCase
 {
@@ -33,7 +32,7 @@ class ClassNameProcessorTest extends \PHPUnit_Framework_TestCase
         $expected = array(
             'extra' => array(
                 'foo' => 'bar',
-                'class' => __CLASS__,
+                'class' => 'Monolog',
             ),
         );
 
diff --git a/tests/PHPUnit/Unit/Log/Processor/ExceptionToTextProcessorTest.php b/plugins/Monolog/Test/Unit/Processor/ExceptionToTextProcessorTest.php
similarity index 88%
rename from tests/PHPUnit/Unit/Log/Processor/ExceptionToTextProcessorTest.php
rename to plugins/Monolog/Test/Unit/Processor/ExceptionToTextProcessorTest.php
index 54cff2ae37..5620325ef5 100644
--- a/tests/PHPUnit/Unit/Log/Processor/ExceptionToTextProcessorTest.php
+++ b/plugins/Monolog/Test/Unit/Processor/ExceptionToTextProcessorTest.php
@@ -6,15 +6,14 @@
  * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  */
 
-namespace Piwik\Tests\Unit\Log\Processor;
+namespace Piwik\Plugins\Monolog\Test\Unit\Processor;
 
 use Piwik\Log;
-use Piwik\Log\Processor\ExceptionToTextProcessor;
+use Piwik\Plugins\Monolog\Processor\ExceptionToTextProcessor;
 
 /**
- * @group Core
  * @group Log
- * @covers \Piwik\Log\Processor\ExceptionToTextProcessor
+ * @covers \Piwik\Plugins\Monolog\Processor\ExceptionToTextProcessor
  */
 class ExceptionToTextProcessorTest extends \PHPUnit_Framework_TestCase
 {
@@ -48,7 +47,7 @@ class ExceptionToTextProcessorTest extends \PHPUnit_Framework_TestCase
         $result = $processor($record);
 
         $expected = array(
-            'message' => __FILE__ . "(41): Hello world\n[stack trace]",
+            'message' => __FILE__ . "(40): Hello world\n[stack trace]",
             'context' => array(
                 'exception' => $exception,
             ),
diff --git a/tests/PHPUnit/Unit/Log/Processor/RequestIdProcessorTest.php b/plugins/Monolog/Test/Unit/Processor/RequestIdProcessorTest.php
similarity index 85%
rename from tests/PHPUnit/Unit/Log/Processor/RequestIdProcessorTest.php
rename to plugins/Monolog/Test/Unit/Processor/RequestIdProcessorTest.php
index b9c809b6e6..3fc3c6a2f9 100644
--- a/tests/PHPUnit/Unit/Log/Processor/RequestIdProcessorTest.php
+++ b/plugins/Monolog/Test/Unit/Processor/RequestIdProcessorTest.php
@@ -6,15 +6,15 @@
  * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  */
 
-namespace Piwik\Tests\Unit\Log\Processor;
+namespace Piwik\Plugins\Monolog\Test\Unit\Processor;
 
+use PHPUnit_Framework_TestCase;
 use Piwik\Common;
-use Piwik\Log\Processor\RequestIdProcessor;
+use Piwik\Plugins\Monolog\Processor\RequestIdProcessor;
 
 /**
- * @group Core
  * @group Log
- * @covers \Piwik\Log\Processor\RequestIdProcessor
+ * @covers \Piwik\Plugins\Monolog\Processor\RequestIdProcessor
  */
 class RequestIdProcessorTest extends \PHPUnit_Framework_TestCase
 {
diff --git a/tests/PHPUnit/Unit/Log/Processor/SprintfProcessorTest.php b/plugins/Monolog/Test/Unit/Processor/SprintfProcessorTest.php
similarity index 89%
rename from tests/PHPUnit/Unit/Log/Processor/SprintfProcessorTest.php
rename to plugins/Monolog/Test/Unit/Processor/SprintfProcessorTest.php
index 7966b165de..27ac6cbd02 100644
--- a/tests/PHPUnit/Unit/Log/Processor/SprintfProcessorTest.php
+++ b/plugins/Monolog/Test/Unit/Processor/SprintfProcessorTest.php
@@ -6,14 +6,13 @@
  * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  */
 
-namespace Piwik\Tests\Unit\Log\Processor;
+namespace Piwik\Plugins\Monolog\Test\Unit\Processor;
 
-use Piwik\Log\Processor\SprintfProcessor;
+use Piwik\Plugins\Monolog\Processor\SprintfProcessor;
 
 /**
- * @group Core
  * @group Log
- * @covers \Piwik\Log\Processor\SprintfProcessor
+ * @covers \Piwik\Plugins\Monolog\Processor\SprintfProcessor
  */
 class SprintfProcessorTest extends \PHPUnit_Framework_TestCase
 {
diff --git a/plugins/Monolog/config/config.php b/plugins/Monolog/config/config.php
new file mode 100644
index 0000000000..02aff13313
--- /dev/null
+++ b/plugins/Monolog/config/config.php
@@ -0,0 +1,99 @@
+<?php
+
+use Interop\Container\ContainerInterface;
+use Monolog\Logger;
+use Piwik\Log;
+
+return array(
+
+    'Psr\Log\LoggerInterface' => DI\object('Monolog\Logger')
+        ->constructor('piwik', DI\link('log.handlers'), DI\link('log.processors')),
+
+    'log.handlers' => DI\factory(function (ContainerInterface $c) {
+        if ($c->has('ini.log.log_writers')) {
+            $writerNames = $c->get('ini.log.log_writers');
+        } else {
+            return array();
+        }
+        $classes = array(
+            'file'     => 'Piwik\Plugins\Monolog\Handler\FileHandler',
+            'screen'   => 'Piwik\Plugins\Monolog\Handler\WebNotificationHandler',
+            'database' => 'Piwik\Plugins\Monolog\Handler\DatabaseHandler',
+        );
+        $writerNames = array_map('trim', $writerNames);
+        $writers = array();
+        foreach ($writerNames as $writerName) {
+            if (isset($classes[$writerName])) {
+                $writers[$writerName] = $c->get($classes[$writerName]);
+            }
+        }
+        return array_values($writers);
+    }),
+
+    'log.processors' => array(
+        DI\link('Piwik\Plugins\Monolog\Processor\ClassNameProcessor'),
+        DI\link('Piwik\Plugins\Monolog\Processor\RequestIdProcessor'),
+        DI\link('Piwik\Plugins\Monolog\Processor\ExceptionToTextProcessor'),
+        DI\link('Piwik\Plugins\Monolog\Processor\SprintfProcessor'),
+        DI\link('Monolog\Processor\PsrLogMessageProcessor'),
+    ),
+
+    'Piwik\Plugins\Monolog\Handler\FileHandler' => DI\object()
+        ->constructor(DI\link('log.file.filename'), DI\link('log.level'))
+        ->method('setFormatter', DI\link('Piwik\Plugins\Monolog\Formatter\LineMessageFormatter')),
+
+    'Piwik\Plugins\Monolog\Handler\DatabaseHandler' => DI\object()
+        ->constructor(DI\link('log.level'))
+        ->method('setFormatter', DI\link('Piwik\Plugins\Monolog\Formatter\LineMessageFormatter')),
+
+    'Piwik\Plugins\Monolog\Handler\WebNotificationHandler' => DI\object()
+        ->constructor(DI\link('log.level'))
+        ->method('setFormatter', DI\link('Piwik\Plugins\Monolog\Formatter\LineMessageFormatter')),
+
+    'log.level' => DI\factory(function (ContainerInterface $c) {
+        if ($c->has('ini.log.log_level')) {
+            $level = strtoupper($c->get('ini.log.log_level'));
+            if (!empty($level) && defined('Piwik\Log::'.strtoupper($level))) {
+                return Log::getMonologLevel(constant('Piwik\Log::'.strtoupper($level)));
+            }
+        }
+        return Logger::WARNING;
+    }),
+
+    'log.file.filename' => DI\factory(function (ContainerInterface $c) {
+        $logPath = $c->get('ini.log.logger_file_path');
+
+        // Absolute path
+        if (strpos($logPath, '/') === 0) {
+            return $logPath;
+        }
+
+        // Remove 'tmp/' at the beginning
+        if (strpos($logPath, 'tmp/') === 0) {
+            $logPath = substr($logPath, strlen('tmp'));
+        }
+
+        if (empty($logPath)) {
+            // Default log file
+            $logPath = '/logs/piwik.log';
+        }
+
+        $logPath = $c->get('path.tmp') . $logPath;
+        if (is_dir($logPath)) {
+            $logPath .= '/piwik.log';
+        }
+
+        return $logPath;
+    }),
+
+    'Piwik\Plugins\Monolog\Formatter\LineMessageFormatter' => DI\object()
+        ->constructor(DI\link('log.format')),
+
+    'log.format' => DI\factory(function (ContainerInterface $c) {
+        if ($c->has('ini.log.string_message_format')) {
+            return $c->get('ini.log.string_message_format');
+        }
+        return '%level% %tag%[%datetime%] %message%';
+    }),
+
+);
-- 
GitLab