From a9b57679b3713ab00dac5539757d9bba5ac41e54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=CC=81s=CC=8C=20Tatarko?= Date: Wed, 13 Nov 2013 21:13:08 +0100 Subject: [PATCH 01/18] Ability to access logger instances in the global scope via static calls --- src/Monolog/Logger.php | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/Monolog/Logger.php b/src/Monolog/Logger.php index 087dee2f0..60f2ed101 100644 --- a/src/Monolog/Logger.php +++ b/src/Monolog/Logger.php @@ -107,6 +107,12 @@ class Logger implements LoggerInterface */ protected static $timezone; + /** + * List of all active Logger instances + * @var array of Monolog\Logger + */ + protected static $loggerInstances = array(); + protected $name; /** @@ -125,6 +131,31 @@ class Logger implements LoggerInterface */ protected $processors; + /** + * Registers new Logger instance + * @param string $name The logging channel name + * @param Monolog\Logger $instance Instance to register + */ + public static function registerLogger($name, Logger $instance) + { + self::$loggerInstances[$name] = $instance; + } + + /** + * Gets instance of requested logging channel + * @param string $name The logging channel name + * @return Monolog\Logger + * @throws InvalidArgumentException If logging channel has not been created + */ + public static function __callStatic($name) + { + if(!isset(self::$loggerInstances[$name])) { + throw new \InvalidArgumentException('The requested logging channel has not been created yet'); + } + + return self::$loggerInstances[$name]; + } + /** * @param string $name The logging channel * @param array $handlers Optional stack of handlers, the first one in the array is called first, etc. @@ -135,6 +166,7 @@ public function __construct($name, array $handlers = array(), array $processors $this->name = $name; $this->handlers = $handlers; $this->processors = $processors; + self::registerLogger($this->name, $this); } /** From c9f8c405ab1261b574719ff7fa598671335ea793 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=CC=81s=CC=8C=20Tatarko?= Date: Wed, 13 Nov 2013 21:33:35 +0100 Subject: [PATCH 02/18] unregisterLogger() method added, phpdoc typo --- src/Monolog/Logger.php | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/Monolog/Logger.php b/src/Monolog/Logger.php index 60f2ed101..e0b1650cb 100644 --- a/src/Monolog/Logger.php +++ b/src/Monolog/Logger.php @@ -132,15 +132,27 @@ class Logger implements LoggerInterface protected $processors; /** - * Registers new Logger instance - * @param string $name The logging channel name - * @param Monolog\Logger $instance Instance to register - */ + * Registers new Logger instance + * @param string $name The logging channel name + * @param Monolog\Logger $instance Instance to register + */ public static function registerLogger($name, Logger $instance) { self::$loggerInstances[$name] = $instance; } + /** + * Unregisters Logger instance + * @param string $name The logging channel name + */ + public static function unregisterLogger($name) + { + if(isset(self::$loggerInstances[$name])) { + self::$loggerInstances[$name] = null; + unset(self::$loggerInstances[$name]); + } + } + /** * Gets instance of requested logging channel * @param string $name The logging channel name From e63dff26eae6af9093f03d9b19d727eed7587101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=CC=81s=CC=8C=20Tatarko?= Date: Wed, 13 Nov 2013 21:37:04 +0100 Subject: [PATCH 03/18] Filled second argument for __callStatic() --- src/Monolog/Logger.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Monolog/Logger.php b/src/Monolog/Logger.php index e0b1650cb..d3af1024e 100644 --- a/src/Monolog/Logger.php +++ b/src/Monolog/Logger.php @@ -159,7 +159,7 @@ public static function unregisterLogger($name) * @return Monolog\Logger * @throws InvalidArgumentException If logging channel has not been created */ - public static function __callStatic($name) + public static function __callStatic($name, $arguments) { if(!isset(self::$loggerInstances[$name])) { throw new \InvalidArgumentException('The requested logging channel has not been created yet'); From 6779cf1e04a675412418f82df6d561a4179b86a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=CC=81s=CC=8C=20Tatarko?= Date: Thu, 14 Nov 2013 14:44:19 +0100 Subject: [PATCH 04/18] Moved all the logic to the separated class - Monolog\Registry --- src/Monolog/Logger.php | 52 +++-------------- src/Monolog/Registry.php | 121 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+), 44 deletions(-) create mode 100644 src/Monolog/Registry.php diff --git a/src/Monolog/Logger.php b/src/Monolog/Logger.php index d3af1024e..bc1728932 100644 --- a/src/Monolog/Logger.php +++ b/src/Monolog/Logger.php @@ -107,12 +107,6 @@ class Logger implements LoggerInterface */ protected static $timezone; - /** - * List of all active Logger instances - * @var array of Monolog\Logger - */ - protected static $loggerInstances = array(); - protected $name; /** @@ -131,43 +125,6 @@ class Logger implements LoggerInterface */ protected $processors; - /** - * Registers new Logger instance - * @param string $name The logging channel name - * @param Monolog\Logger $instance Instance to register - */ - public static function registerLogger($name, Logger $instance) - { - self::$loggerInstances[$name] = $instance; - } - - /** - * Unregisters Logger instance - * @param string $name The logging channel name - */ - public static function unregisterLogger($name) - { - if(isset(self::$loggerInstances[$name])) { - self::$loggerInstances[$name] = null; - unset(self::$loggerInstances[$name]); - } - } - - /** - * Gets instance of requested logging channel - * @param string $name The logging channel name - * @return Monolog\Logger - * @throws InvalidArgumentException If logging channel has not been created - */ - public static function __callStatic($name, $arguments) - { - if(!isset(self::$loggerInstances[$name])) { - throw new \InvalidArgumentException('The requested logging channel has not been created yet'); - } - - return self::$loggerInstances[$name]; - } - /** * @param string $name The logging channel * @param array $handlers Optional stack of handlers, the first one in the array is called first, etc. @@ -178,7 +135,6 @@ public function __construct($name, array $handlers = array(), array $processors $this->name = $name; $this->handlers = $handlers; $this->processors = $processors; - self::registerLogger($this->name, $this); } /** @@ -620,4 +576,12 @@ public function emergency($message, array $context = array()) { return $this->addRecord(static::EMERGENCY, $message, $context); } + + /** + * Adds instance to the Registry + */ + public function addToRegistry() + { + Registry::addLogger($this); + } } diff --git a/src/Monolog/Registry.php b/src/Monolog/Registry.php new file mode 100644 index 000000000..d20a1914d --- /dev/null +++ b/src/Monolog/Registry.php @@ -0,0 +1,121 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog; + +use InvalidArgumentException; + +/** + * Monolog log registry + * + * Allows to get `Logger` instances in the global scope + * via static method calls on this class. + * + * + * $application = new Monolog\Logger('application'); + * $api = new Monolog\Logger('api'); + * + * $application->addToRegistry(); + * Monolog\Registry::addLogger($api); + * + * function testLogger() + * { + * Monolog\Registry::api()->addError('Sent to $api Logger instance'); + * Monolog\Registry::application()->addError('Sent to $application Logger instance'); + * } + * + * + * @author Tomas Tatarko + */ +class Registry +{ + /** + * List of all loggers in the registry (ba named indexes) + * @var array of Monolog\Logger + */ + protected static $loggers = array(); + + /** + * Adds new logging channel to the registry + * @param Monolog\Logger $logger Instance of the logging channel + * @param string $name Name of the logging channel ($logger->getName() by default) + * @param boolean $overwrite Overwrite instance in the registry if the given name already exists? + * @throws InvalidArgumentException If $overwrite set to false and named Logger instance already exists + */ + public static function addLogger(Logger $logger, $name = null, $overwrite = false) + { + $name = $name ? : $logger->getName(); + + if (isset(self::$loggers[$name]) && !$overwrite) { + throw new InvalidArgumentException('Logger with the given name already exists'); + } + + self::$loggers[$name] = $logger; + } + + /** + * Removes instance from registry by the given name + * @param string $name Named index to remove + */ + public static function removeLoggerByName($name) + { + self::$loggers[$name] = null; + unset(self::$loggers[$name]); + } + + /** + * Removes instance from registry by the given instance + * @param Monolog\Logger $instance Instance thats pointer should be removed from the registry + */ + public static function removeLoggerByInstance(Logger $instance) + { + foreach (self::$loggers as $key => $logger) { + if ($logger === $instance) { + self::removeLoggerByName($key); + } + } + } + + /** + * Clears the registry + */ + public static function clear() + { + self::$loggers = array(); + } + + /** + * Gets Logger instance from the registry + * @param string $name Name of the requested Logger instance + * @return Monolog\Logger Requested instance of Logger + * @throws InvalidArgumentException If named Logger instance is not in the registry + */ + public static function getInstance($name) + { + if (!isset(self::$loggers[$name])) { + throw new InvalidArgumentException(sprintf('Requested "%s" logger instance is not in the registry', $name)); + } + + return self::$loggers[$name]; + } + + /** + * Gets Logger instance from the registry via static method call + * @param string $name Name of the requested Logger instance + * @param array $arguments Arguments passed to static method call + * @return Monolog\Logger Requested instance of Logger + * @throws InvalidArgumentException If named Logger instance is not in the registry + */ + public static function __callStatic($name, $arguments) + { + return self::getInstance($name); + } +} \ No newline at end of file From d67c34d4564918c08520e16808c72f1705e1addd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=CC=81s=CC=8C=20Tatarko?= Date: Thu, 14 Nov 2013 15:52:22 +0100 Subject: [PATCH 05/18] phpdoc typo --- src/Monolog/Registry.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Monolog/Registry.php b/src/Monolog/Registry.php index d20a1914d..245fe4e23 100644 --- a/src/Monolog/Registry.php +++ b/src/Monolog/Registry.php @@ -39,16 +39,16 @@ class Registry { /** * List of all loggers in the registry (ba named indexes) - * @var array of Monolog\Logger + * @var array of Logger */ protected static $loggers = array(); /** * Adds new logging channel to the registry - * @param Monolog\Logger $logger Instance of the logging channel + * @param Logger $logger Instance of the logging channel * @param string $name Name of the logging channel ($logger->getName() by default) * @param boolean $overwrite Overwrite instance in the registry if the given name already exists? - * @throws InvalidArgumentException If $overwrite set to false and named Logger instance already exists + * @throws \InvalidArgumentException If $overwrite set to false and named Logger instance already exists */ public static function addLogger(Logger $logger, $name = null, $overwrite = false) { @@ -73,7 +73,7 @@ public static function removeLoggerByName($name) /** * Removes instance from registry by the given instance - * @param Monolog\Logger $instance Instance thats pointer should be removed from the registry + * @param Logger $instance Instance thats pointer should be removed from the registry */ public static function removeLoggerByInstance(Logger $instance) { @@ -95,8 +95,8 @@ public static function clear() /** * Gets Logger instance from the registry * @param string $name Name of the requested Logger instance - * @return Monolog\Logger Requested instance of Logger - * @throws InvalidArgumentException If named Logger instance is not in the registry + * @return Logger Requested instance of Logger + * @throws \InvalidArgumentException If named Logger instance is not in the registry */ public static function getInstance($name) { @@ -111,8 +111,8 @@ public static function getInstance($name) * Gets Logger instance from the registry via static method call * @param string $name Name of the requested Logger instance * @param array $arguments Arguments passed to static method call - * @return Monolog\Logger Requested instance of Logger - * @throws InvalidArgumentException If named Logger instance is not in the registry + * @return Logger Requested instance of Logger + * @throws \InvalidArgumentException If named Logger instance is not in the registry */ public static function __callStatic($name, $arguments) { From 1827de4da3d52452ae323d86170258d73a3cdbc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=CC=81s=CC=8C=20Tatarko?= Date: Thu, 14 Nov 2013 15:53:26 +0100 Subject: [PATCH 06/18] removed unnecessary presetting index to null --- src/Monolog/Registry.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Monolog/Registry.php b/src/Monolog/Registry.php index 245fe4e23..ba04d038f 100644 --- a/src/Monolog/Registry.php +++ b/src/Monolog/Registry.php @@ -67,7 +67,6 @@ public static function addLogger(Logger $logger, $name = null, $overwrite = fals */ public static function removeLoggerByName($name) { - self::$loggers[$name] = null; unset(self::$loggers[$name]); } From 9c579677004b325055161c886e7797aec0cd1e85 Mon Sep 17 00:00:00 2001 From: otternq Date: Sat, 16 Nov 2013 17:40:16 -0800 Subject: [PATCH 07/18] adding a processor for basic git information Adds the current Git Branch, and Git Commit key to the extra field --- src/Monolog/Processor/GitProcessor.php | 59 ++++++++++++++++++++ tests/Monolog/Processor/GitProcessorTest.php | 31 ++++++++++ 2 files changed, 90 insertions(+) create mode 100644 src/Monolog/Processor/GitProcessor.php create mode 100644 tests/Monolog/Processor/GitProcessorTest.php diff --git a/src/Monolog/Processor/GitProcessor.php b/src/Monolog/Processor/GitProcessor.php new file mode 100644 index 000000000..220a56691 --- /dev/null +++ b/src/Monolog/Processor/GitProcessor.php @@ -0,0 +1,59 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog\Processor; + +/** + * Injects Git branch and Git commit SHA in all records + * + * @author Nick Otter + */ +class GitProcessor +{ + /** + * @param array $record + * @return array + */ + public function __invoke(array $record) + { + + $branch = self::getBranch(); + $commit = self::getCommit(); + + $record['extra'] = array_merge( + $record['extra'], + array( + 'git' => array( + 'branch' => $branch, + 'commit' => $commit + ), + ) + ); + + return $record; + } + + static protected function getBranch() { + $branches = explode("\n", `git branch`); + + foreach ($branches as $branch) { + if ($branch[0] == "*") { + return substr($branch, 2); + } + } + return $branches; + } + + static protected function getCommit() { + $s = `git rev-parse HEAD`; + return $s; + } +} diff --git a/tests/Monolog/Processor/GitProcessorTest.php b/tests/Monolog/Processor/GitProcessorTest.php new file mode 100644 index 000000000..5f7a3e245 --- /dev/null +++ b/tests/Monolog/Processor/GitProcessorTest.php @@ -0,0 +1,31 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog\Processor; + +use Monolog\TestCase; + +class GitProcessorTest extends TestCase +{ + /** + * @covers Monolog\Processor\GitProcessor::__invoke + */ + public function testProcessor() + { + $processor = new GitProcessor(); + $record = $processor($this->getRecord()); + + $this->assertArrayHasKey('git', $record['extra']); + + $this->assertTrue(!is_array($record['extra']['git']['branch'])); + + } +} From e1feb74001ccb49f585529719d9f77101f7ea21d Mon Sep 17 00:00:00 2001 From: otternq Date: Sat, 16 Nov 2013 17:52:07 -0800 Subject: [PATCH 08/18] removing whitespace from getCommit() value --- src/Monolog/Processor/GitProcessor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Monolog/Processor/GitProcessor.php b/src/Monolog/Processor/GitProcessor.php index 220a56691..f2eafd1c7 100644 --- a/src/Monolog/Processor/GitProcessor.php +++ b/src/Monolog/Processor/GitProcessor.php @@ -54,6 +54,6 @@ static protected function getBranch() { static protected function getCommit() { $s = `git rev-parse HEAD`; - return $s; + return trim($s); } } From 1c3b7a6cdd3cffec935767f47e410c4c0c472e91 Mon Sep 17 00:00:00 2001 From: otternq Date: Wed, 20 Nov 2013 00:41:49 -0800 Subject: [PATCH 09/18] updated data assignment based on @Seldaek suggestion --- src/Monolog/Processor/GitProcessor.php | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/Monolog/Processor/GitProcessor.php b/src/Monolog/Processor/GitProcessor.php index f2eafd1c7..0bb45fca2 100644 --- a/src/Monolog/Processor/GitProcessor.php +++ b/src/Monolog/Processor/GitProcessor.php @@ -28,15 +28,8 @@ public function __invoke(array $record) $branch = self::getBranch(); $commit = self::getCommit(); - $record['extra'] = array_merge( - $record['extra'], - array( - 'git' => array( - 'branch' => $branch, - 'commit' => $commit - ), - ) - ); + $record['extra']['git']['branch'] = $branch; + $record['extra']['git']['commit'] = $commit; return $record; } From aff8d92e294d638e8022e3f8c1a27111c6b2482a Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 20 Nov 2013 13:28:13 +0100 Subject: [PATCH 10/18] Add caching, group exec calls in one, refs #274 --- src/Monolog/Processor/GitProcessor.php | 44 +++++++++++++------- tests/Monolog/Processor/GitProcessorTest.php | 2 - 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/Monolog/Processor/GitProcessor.php b/src/Monolog/Processor/GitProcessor.php index 0bb45fca2..96c2abfef 100644 --- a/src/Monolog/Processor/GitProcessor.php +++ b/src/Monolog/Processor/GitProcessor.php @@ -11,42 +11,54 @@ namespace Monolog\Processor; +use Monolog\Logger; + /** * Injects Git branch and Git commit SHA in all records * * @author Nick Otter + * @author Jordi Boggiano */ class GitProcessor { + private $level; + private static $cache; + + public function __construct($level = Logger::DEBUG) + { + $this->level = $level; + } + /** * @param array $record * @return array */ public function __invoke(array $record) { + // return if the level is not high enough + if ($record['level'] < $this->level) { + return $record; + } - $branch = self::getBranch(); - $commit = self::getCommit(); - - $record['extra']['git']['branch'] = $branch; - $record['extra']['git']['commit'] = $commit; + $record['extra']['git'] = self::getGitInfo(); return $record; } - static protected function getBranch() { - $branches = explode("\n", `git branch`); + private static function getGitInfo() + { + if (self::$cache) { + return self::$cache; + } - foreach ($branches as $branch) { - if ($branch[0] == "*") { - return substr($branch, 2); - } + $branches = `git branch -v --no-abbrev`; + if (preg_match('{^\* (.+?)\s+([a-f0-9]{40})(?:\s|$)}m', $branches, $matches)) { + return self::$cache = array( + 'branch' => $matches[1], + 'commit' => $matches[2], + ); } - return $branches; - } - static protected function getCommit() { - $s = `git rev-parse HEAD`; - return trim($s); + return self::$cache = array(); } } diff --git a/tests/Monolog/Processor/GitProcessorTest.php b/tests/Monolog/Processor/GitProcessorTest.php index 5f7a3e245..5adb505dc 100644 --- a/tests/Monolog/Processor/GitProcessorTest.php +++ b/tests/Monolog/Processor/GitProcessorTest.php @@ -24,8 +24,6 @@ public function testProcessor() $record = $processor($this->getRecord()); $this->assertArrayHasKey('git', $record['extra']); - $this->assertTrue(!is_array($record['extra']['git']['branch'])); - } } From 89ba162dca13c6981346eb1985049cbf4d7c439b Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 20 Nov 2013 13:30:00 +0100 Subject: [PATCH 11/18] Update readme, fix cs --- README.mdown | 1 + src/Monolog/Formatter/ElasticaFormatter.php | 4 +++- src/Monolog/Formatter/ScalarFormatter.php | 2 +- src/Monolog/Handler/DynamoDbHandler.php | 8 ++++---- src/Monolog/Handler/ElasticSearchHandler.php | 10 +++++----- src/Monolog/Handler/StreamHandler.php | 3 ++- 6 files changed, 16 insertions(+), 12 deletions(-) diff --git a/README.mdown b/README.mdown index 64b4cfe36..93994d974 100644 --- a/README.mdown +++ b/README.mdown @@ -196,6 +196,7 @@ Processors - _MemoryPeakUsageProcessor_: Adds the peak memory usage to a log record. - _ProcessIdProcessor_: Adds the process id to a log record. - _UidProcessor_: Adds a unique identifier to a log record. +- _GitProcessor_: Adds the current git branch and commit to a log record. Utilities --------- diff --git a/src/Monolog/Formatter/ElasticaFormatter.php b/src/Monolog/Formatter/ElasticaFormatter.php index f460abe42..b0b0cf066 100644 --- a/src/Monolog/Formatter/ElasticaFormatter.php +++ b/src/Monolog/Formatter/ElasticaFormatter.php @@ -47,6 +47,7 @@ public function __construct($index, $type) public function format(array $record) { $record = parent::format($record); + return $this->getDocument($record); } @@ -71,7 +72,7 @@ public function getType() /** * Convert a log message into an Elastica Document * - * @param array $record Log message + * @param array $record Log message * @return Document */ protected function getDocument($record) @@ -80,6 +81,7 @@ protected function getDocument($record) $document->setData($record); $document->setType($this->type); $document->setIndex($this->index); + return $document; } } diff --git a/src/Monolog/Formatter/ScalarFormatter.php b/src/Monolog/Formatter/ScalarFormatter.php index 600750303..9e4201ab3 100644 --- a/src/Monolog/Formatter/ScalarFormatter.php +++ b/src/Monolog/Formatter/ScalarFormatter.php @@ -34,7 +34,7 @@ public function format(array $record) } /** - * @param mixed $value + * @param mixed $value * @return mixed */ protected function normalizeValue($value) diff --git a/src/Monolog/Handler/DynamoDbHandler.php b/src/Monolog/Handler/DynamoDbHandler.php index 9a7aa7505..ad6622e1e 100644 --- a/src/Monolog/Handler/DynamoDbHandler.php +++ b/src/Monolog/Handler/DynamoDbHandler.php @@ -39,9 +39,9 @@ class DynamoDbHandler extends AbstractProcessingHandler /** * @param DynamoDbClient $client - * @param string $table - * @param integer $level - * @param boolean $bubble + * @param string $table + * @param integer $level + * @param boolean $bubble */ public function __construct(DynamoDbClient $client, $table, $level = Logger::DEBUG, $bubble = true) { @@ -70,7 +70,7 @@ protected function write(array $record) } /** - * @param array $record + * @param array $record * @return array */ protected function filterEmptyFields(array $record) diff --git a/src/Monolog/Handler/ElasticSearchHandler.php b/src/Monolog/Handler/ElasticSearchHandler.php index 1508e77de..96e5d57f2 100644 --- a/src/Monolog/Handler/ElasticSearchHandler.php +++ b/src/Monolog/Handler/ElasticSearchHandler.php @@ -46,10 +46,10 @@ class ElasticSearchHandler extends AbstractProcessingHandler protected $options = array(); /** - * @param Client $client Elastica Client object - * @param array $options Handler configuration - * @param integer $level The minimum logging level at which this handler will be triggered - * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not + * @param Client $client Elastica Client object + * @param array $options Handler configuration + * @param integer $level The minimum logging level at which this handler will be triggered + * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not */ public function __construct(Client $client, array $options = array(), $level = Logger::DEBUG, $bubble = true) { @@ -112,7 +112,7 @@ public function handleBatch(array $records) /** * Use Elasticsearch bulk API to send list of documents - * @param array $documents + * @param array $documents * @throws \RuntimeException */ protected function bulkSend(array $documents) diff --git a/src/Monolog/Handler/StreamHandler.php b/src/Monolog/Handler/StreamHandler.php index 521b5751f..281752949 100644 --- a/src/Monolog/Handler/StreamHandler.php +++ b/src/Monolog/Handler/StreamHandler.php @@ -73,7 +73,8 @@ protected function write(array $record) fwrite($this->stream, (string) $record['formatted']); } - private function customErrorHandler($code, $msg) { + private function customErrorHandler($code, $msg) + { $this->errorMessage = preg_replace('{^fopen\(.*?\): }', '', $msg); } } From 11cb918bbc471da036c6148b995d42f79806428d Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 20 Nov 2013 13:44:25 +0100 Subject: [PATCH 12/18] Tweak html output a bit for readability, update readme, force unicode, refs #275 --- README.mdown | 1 + ...mlEmailFormatter.php => HtmlFormatter.php} | 29 ++++++++++--------- 2 files changed, 17 insertions(+), 13 deletions(-) rename src/Monolog/Formatter/{HtmlEmailFormatter.php => HtmlFormatter.php} (77%) diff --git a/README.mdown b/README.mdown index 93994d974..168b7ecf8 100644 --- a/README.mdown +++ b/README.mdown @@ -178,6 +178,7 @@ Formatters ---------- - _LineFormatter_: Formats a log record into a one-line string. +- _HtmlFormatter_: Used to format log records into a human readable html table, mainly suitable for emails. - _NormalizerFormatter_: Normalizes objects/resources down to strings so a record can easily be serialized/encoded. - _ScalarFormatter_: Used to format log records into an associative array of scalar values. - _JsonFormatter_: Encodes a log record into json. diff --git a/src/Monolog/Formatter/HtmlEmailFormatter.php b/src/Monolog/Formatter/HtmlFormatter.php similarity index 77% rename from src/Monolog/Formatter/HtmlEmailFormatter.php rename to src/Monolog/Formatter/HtmlFormatter.php index 056bf13b9..559420fd5 100644 --- a/src/Monolog/Formatter/HtmlEmailFormatter.php +++ b/src/Monolog/Formatter/HtmlFormatter.php @@ -11,13 +11,13 @@ namespace Monolog\Formatter; /** - * Formats incoming records into a HTML table + * Formats incoming records into an HTML table * * This is especially useful for html email logging * * @author Tiago Brito */ -class HtmlEmailFormatter extends NormalizerFormatter +class HtmlFormatter extends NormalizerFormatter { /** * Translates Monolog log levels to html color priorities. @@ -50,8 +50,8 @@ public function __construct($dateFormat = null) */ private function addRow($th, $td = ' ') { - $th = htmlspecialchars($th); - $td = '
'.htmlspecialchars($td).'
'; + $th = htmlspecialchars($th, ENT_NOQUOTES, 'UTF-8'); + $td = '
'.htmlspecialchars($td, ENT_NOQUOTES, 'UTF-8').'
'; return "\n$th:\n".$td."\n"; } @@ -65,8 +65,8 @@ private function addRow($th, $td = ' ') */ private function addTitle($title, $level) { - $title = htmlspecialchars($title); - + $title = htmlspecialchars($title, ENT_NOQUOTES, 'UTF-8'); + return '

'.$title.'

'; } /** @@ -77,15 +77,18 @@ private function addTitle($title, $level) */ public function format(array $record) { - $output = $this->addTitle($this->convertToString($record['level_name']), $record['level']); + $output = $this->addTitle($record['level_name'], $record['level']); $output .= ''; - $output .= $this->addRow('Message', $this->convertToString($record['message'])); - $output .= $this->addRow('Generated at', $this->convertToString($record['datetime'])); - $output .= $this->addRow('Level', $this->convertToString($record['level'])); - $output .= $this->addRow('Channel', $this->convertToString($record['channel'])); - $output .= $this->addRow('Context', $this->convertToString($record['context'])); - $output .= $this->addRow('Extra', $this->convertToString($record['extra'])); + $output .= $this->addRow('Message', (string) $record['message']); + $output .= $this->addRow('Time', $record['datetime']->format('Y-m-d\TH:i:s.uO')); + $output .= $this->addRow('Channel', $record['channel']); + if ($record['context']) { + $output .= $this->addRow('Context', $this->convertToString($record['context'])); + } + if ($record['extra']) { + $output .= $this->addRow('Extra', $this->convertToString($record['extra'])); + } return $output.'
'; } From ed8c6716603f783428d0a97e27a56d216fd808eb Mon Sep 17 00:00:00 2001 From: alsar Date: Sat, 23 Nov 2013 21:11:53 +0100 Subject: [PATCH 13/18] added missing use statement --- src/Monolog/Formatter/HtmlFormatter.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Monolog/Formatter/HtmlFormatter.php b/src/Monolog/Formatter/HtmlFormatter.php index 559420fd5..5fa21e18d 100644 --- a/src/Monolog/Formatter/HtmlFormatter.php +++ b/src/Monolog/Formatter/HtmlFormatter.php @@ -10,6 +10,8 @@ namespace Monolog\Formatter; +use Monolog\Logger; + /** * Formats incoming records into an HTML table * From 0a5db1985cddffd869e8c4322baab06485929f35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=CC=81s=CC=8C=20Tatarko?= Date: Sun, 24 Nov 2013 11:51:46 +0100 Subject: [PATCH 14/18] Removed addToRegistry() method --- src/Monolog/Logger.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/Monolog/Logger.php b/src/Monolog/Logger.php index bc1728932..087dee2f0 100644 --- a/src/Monolog/Logger.php +++ b/src/Monolog/Logger.php @@ -576,12 +576,4 @@ public function emergency($message, array $context = array()) { return $this->addRecord(static::EMERGENCY, $message, $context); } - - /** - * Adds instance to the Registry - */ - public function addToRegistry() - { - Registry::addLogger($this); - } } From 0c3cf7842db4f83694dada813d5cc79127324781 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=CC=81s=CC=8C=20Tatarko?= Date: Sun, 24 Nov 2013 14:18:03 +0100 Subject: [PATCH 15/18] Fixed outdated phpdoc --- src/Monolog/Registry.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Monolog/Registry.php b/src/Monolog/Registry.php index ba04d038f..327435f78 100644 --- a/src/Monolog/Registry.php +++ b/src/Monolog/Registry.php @@ -23,7 +23,7 @@ * $application = new Monolog\Logger('application'); * $api = new Monolog\Logger('api'); * - * $application->addToRegistry(); + * Monolog\Registry::addLogger($application); * Monolog\Registry::addLogger($api); * * function testLogger() From 193e627a0b510f314e73d7988cef648b36b541b1 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 25 Nov 2013 14:22:15 +0100 Subject: [PATCH 16/18] Merge removeLogger* methods in a simpler one, refs #273 --- src/Monolog/Registry.php | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/Monolog/Registry.php b/src/Monolog/Registry.php index 327435f78..095223fbe 100644 --- a/src/Monolog/Registry.php +++ b/src/Monolog/Registry.php @@ -39,12 +39,14 @@ class Registry { /** * List of all loggers in the registry (ba named indexes) + * * @var array of Logger */ - protected static $loggers = array(); + private static $loggers = array(); /** * Adds new logging channel to the registry + * * @param Logger $logger Instance of the logging channel * @param string $name Name of the logging channel ($logger->getName() by default) * @param boolean $overwrite Overwrite instance in the registry if the given name already exists? @@ -52,7 +54,7 @@ class Registry */ public static function addLogger(Logger $logger, $name = null, $overwrite = false) { - $name = $name ? : $logger->getName(); + $name = $name ?: $logger->getName(); if (isset(self::$loggers[$name]) && !$overwrite) { throw new InvalidArgumentException('Logger with the given name already exists'); @@ -62,24 +64,18 @@ public static function addLogger(Logger $logger, $name = null, $overwrite = fals } /** - * Removes instance from registry by the given name - * @param string $name Named index to remove + * Removes instance from registry by name or instance + * + * @param string|Logger $logger Name or logger instance */ - public static function removeLoggerByName($name) + public static function removeLogger($logger) { - unset(self::$loggers[$name]); - } - - /** - * Removes instance from registry by the given instance - * @param Logger $instance Instance thats pointer should be removed from the registry - */ - public static function removeLoggerByInstance(Logger $instance) - { - foreach (self::$loggers as $key => $logger) { - if ($logger === $instance) { - self::removeLoggerByName($key); + if ($logger instanceof Logger) { + if (false !== ($idx = array_search($logger, self::$loggers, true))) { + unset(self::$loggers[$idx]); } + } else { + unset(self::$loggers[$logger]); } } @@ -93,6 +89,7 @@ public static function clear() /** * Gets Logger instance from the registry + * * @param string $name Name of the requested Logger instance * @return Logger Requested instance of Logger * @throws \InvalidArgumentException If named Logger instance is not in the registry @@ -108,6 +105,7 @@ public static function getInstance($name) /** * Gets Logger instance from the registry via static method call + * * @param string $name Name of the requested Logger instance * @param array $arguments Arguments passed to static method call * @return Logger Requested instance of Logger @@ -117,4 +115,4 @@ public static function __callStatic($name, $arguments) { return self::getInstance($name); } -} \ No newline at end of file +} From eaa3c05509d1b9d07ea1548d4f439581a4bb407e Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 25 Nov 2013 14:25:05 +0100 Subject: [PATCH 17/18] Fix CS --- src/Monolog/Formatter/HtmlFormatter.php | 8 ++++---- src/Monolog/Registry.php | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Monolog/Formatter/HtmlFormatter.php b/src/Monolog/Formatter/HtmlFormatter.php index 5fa21e18d..d853547ba 100644 --- a/src/Monolog/Formatter/HtmlFormatter.php +++ b/src/Monolog/Formatter/HtmlFormatter.php @@ -46,8 +46,8 @@ public function __construct($dateFormat = null) /** * Creates an HTML table row * - * @param $th string Row header content - * @param string $td Row standard cell content + * @param string $th Row header content + * @param string $td Row standard cell content * @return string */ private function addRow($th, $td = ' ') @@ -61,8 +61,8 @@ private function addRow($th, $td = ' ') /** * Create a HTML h1 tag * - * @param $title string Text to be in the h1 - * @param $level integer Error level + * @param string $title Text to be in the h1 + * @param integer $level Error level * @return string */ private function addTitle($title, $level) diff --git a/src/Monolog/Registry.php b/src/Monolog/Registry.php index 095223fbe..396043b7a 100644 --- a/src/Monolog/Registry.php +++ b/src/Monolog/Registry.php @@ -47,9 +47,9 @@ class Registry /** * Adds new logging channel to the registry * - * @param Logger $logger Instance of the logging channel - * @param string $name Name of the logging channel ($logger->getName() by default) - * @param boolean $overwrite Overwrite instance in the registry if the given name already exists? + * @param Logger $logger Instance of the logging channel + * @param string $name Name of the logging channel ($logger->getName() by default) + * @param boolean $overwrite Overwrite instance in the registry if the given name already exists? * @throws \InvalidArgumentException If $overwrite set to false and named Logger instance already exists */ public static function addLogger(Logger $logger, $name = null, $overwrite = false) @@ -90,8 +90,8 @@ public static function clear() /** * Gets Logger instance from the registry * - * @param string $name Name of the requested Logger instance - * @return Logger Requested instance of Logger + * @param string $name Name of the requested Logger instance + * @return Logger Requested instance of Logger * @throws \InvalidArgumentException If named Logger instance is not in the registry */ public static function getInstance($name) @@ -106,9 +106,9 @@ public static function getInstance($name) /** * Gets Logger instance from the registry via static method call * - * @param string $name Name of the requested Logger instance - * @param array $arguments Arguments passed to static method call - * @return Logger Requested instance of Logger + * @param string $name Name of the requested Logger instance + * @param array $arguments Arguments passed to static method call + * @return Logger Requested instance of Logger * @throws \InvalidArgumentException If named Logger instance is not in the registry */ public static function __callStatic($name, $arguments) From 6f5b3d2a9c63a857d40d200f1bb9f62991c07bf8 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 25 Nov 2013 14:58:14 +0100 Subject: [PATCH 18/18] Fix docblock --- src/Monolog/Registry.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Monolog/Registry.php b/src/Monolog/Registry.php index 396043b7a..03cd3eaaa 100644 --- a/src/Monolog/Registry.php +++ b/src/Monolog/Registry.php @@ -40,7 +40,7 @@ class Registry /** * List of all loggers in the registry (ba named indexes) * - * @var array of Logger + * @var Logger[] */ private static $loggers = array();