1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-04 20:27:31 +02:00

Add ->withName to clone a handler and get a new name, fixes #730

This commit is contained in:
Jordi Boggiano
2016-02-13 16:46:17 +00:00
parent baca9641ba
commit cb2778ae77
3 changed files with 29 additions and 0 deletions

View File

@@ -189,6 +189,9 @@ $logger->pushHandler($firephp);
$securityLogger = new Logger('security'); $securityLogger = new Logger('security');
$securityLogger->pushHandler($stream); $securityLogger->pushHandler($stream);
$securityLogger->pushHandler($firephp); $securityLogger->pushHandler($firephp);
// Or clone the first one to only change the channel
$securityLogger = $logger->withName('security');
``` ```
## Customizing the log format ## Customizing the log format

View File

@@ -153,6 +153,19 @@ class Logger implements LoggerInterface
return $this->name; return $this->name;
} }
/**
* Return a new cloned instance with the name changed
*
* @return static
*/
public function withName($name)
{
$new = clone $this;
$new->name = $name;
return $new;
}
/** /**
* Pushes a handler on to the stack. * Pushes a handler on to the stack.
* *

View File

@@ -33,6 +33,19 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('ERROR', Logger::getLevelName(Logger::ERROR)); $this->assertEquals('ERROR', Logger::getLevelName(Logger::ERROR));
} }
/**
* @covers Monolog\Logger::withName
*/
public function testWithName()
{
$first = new Logger('first', array($handler = new TestHandler()));
$second = $first->withName('second');
$this->assertSame('first', $first->getName());
$this->assertSame('second', $second->getName());
$this->assertSame($handler, $second->popHandler());
}
/** /**
* @covers Monolog\Logger::toMonologLevel * @covers Monolog\Logger::toMonologLevel
*/ */