Browse Source

Add possibility to pass handlers/processors to Logger::__construct, fixes #157

Jordi Boggiano 13 years ago
parent
commit
9f515d47ce
2 changed files with 35 additions and 4 deletions
  1. 15 4
      src/Monolog/Logger.php
  2. 20 0
      tests/Monolog/LoggerTest.php

+ 15 - 4
src/Monolog/Logger.php

@@ -99,16 +99,27 @@ class Logger implements LoggerInterface
      *
      * @var array of Monolog\Handler\HandlerInterface
      */
-    protected $handlers = array();
+    protected $handlers;
 
-    protected $processors = array();
+    /**
+     * Processors that will process all log records
+     *
+     * To process records of a single handler instead, add the processor on that specific handler
+     *
+     * @var array of callables
+     */
+    protected $processors;
 
     /**
-     * @param string $name The logging channel
+     * @param string $name       The logging channel
+     * @param array  $handlers   Optional stack of handlers, the first one in the array is called first, etc.
+     * @param array  $processors Optional array of processors
      */
-    public function __construct($name)
+    public function __construct($name, array $handlers = array(), array $processors = array())
     {
         $this->name = $name;
+        $this->handlers = $handlers;
+        $this->processors = $processors;
     }
 
     /**

+ 20 - 0
tests/Monolog/LoggerTest.php

@@ -85,6 +85,26 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
         $this->assertFalse($logger->addWarning('test'));
     }
 
+    public function testHandlersInCtor()
+    {
+        $handler1 = new TestHandler;
+        $handler2 = new TestHandler;
+        $logger = new Logger(__METHOD__, array($handler1, $handler2));
+
+        $this->assertEquals($handler1, $logger->popHandler());
+        $this->assertEquals($handler2, $logger->popHandler());
+    }
+
+    public function testProcessorsInCtor()
+    {
+        $processor1 = new WebProcessor;
+        $processor2 = new WebProcessor;
+        $logger = new Logger(__METHOD__, array(), array($processor1, $processor2));
+
+        $this->assertEquals($processor1, $logger->popProcessor());
+        $this->assertEquals($processor2, $logger->popProcessor());
+    }
+
     /**
      * @covers Monolog\Logger::pushHandler
      * @covers Monolog\Logger::popHandler