Skip to content

Commit

Permalink
Moved the monolog configuration and code into a Monolog plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Feb 5, 2015
1 parent 851a567 commit c3267a0
Show file tree
Hide file tree
Showing 22 changed files with 192 additions and 134 deletions.
1 change: 1 addition & 0 deletions config/global.ini.php
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@
Plugins[] = ExampleRssWidget
Plugins[] = Provider
Plugins[] = Feedback
Plugins[] = Monolog

Plugins[] = Login
Plugins[] = UsersManager
Expand Down
84 changes: 1 addition & 83 deletions config/global.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php

use Interop\Container\ContainerInterface;
use Monolog\Logger;
use Piwik\Log;
use Piwik\Cache\Eager;
use Piwik\SettingsServer;

Expand Down Expand Up @@ -76,87 +74,7 @@
return $backend;
},

// 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')),
Expand Down
23 changes: 23 additions & 0 deletions core/Updates/2.11.0-b4.php
Original file line number Diff line number Diff line change
@@ -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_b4 extends Updates
{
static function update()
{
try {
Manager::getInstance()->activatePlugin('Monolog');
} catch (\Exception $e) {
}
}
}
2 changes: 1 addition & 1 deletion core/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class Version
* The current Piwik version.
* @var string
*/
const VERSION = '2.11.0-b3';
const VERSION = '2.11.0-b4';

public function isStableVersion($version)
{
Expand Down
2 changes: 1 addition & 1 deletion misc/cron/archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
/** @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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions plugins/Monolog/Monolog.php
Original file line number Diff line number Diff line change
@@ -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
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -52,7 +52,7 @@ private function getLoggingClassName()
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'];
}
}
Expand All @@ -63,9 +63,19 @@ private function getClassNameThatIsLogging($backtrace)
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,8 +15,8 @@
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
Expand All @@ -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()
Expand Down Expand Up @@ -82,7 +82,7 @@ public function testLoggingWorksWhenMessageIsString($backend)

Log::warning(self::TESTMESSAGE);

$this->checkBackend($backend, self::TESTMESSAGE, $formatMessage = true, $tag = __CLASS__);
$this->checkBackend($backend, self::TESTMESSAGE, $formatMessage = true, $tag = 'Monolog');
}

/**
Expand All @@ -94,7 +94,7 @@ public function testLoggingWorksWhenMessageIsSprintfString($backend)

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');
}

/**
Expand All @@ -107,7 +107,7 @@ public function testLoggingWorksWhenMessageIsError($backend)
$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');
}

/**
Expand All @@ -120,7 +120,7 @@ public function testLoggingWorksWhenMessageIsException($backend)
$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');
}

/**
Expand All @@ -132,9 +132,7 @@ public function testLoggingCorrectlyIdentifiesPlugin($backend)

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');
}

/**
Expand All @@ -159,9 +157,7 @@ public function testLogMessagesAreTrimmed($backend)

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');
}

/**
Expand All @@ -176,7 +172,7 @@ public function testNoInfiniteLoopWhenLoggingToDatabase()

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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit c3267a0

Please sign in to comment.