1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-10-22 00:56:08 +02:00

the appname can now be set from the handler's constructor

This commit is contained in:
odino
2013-08-26 13:20:13 +04:00
parent db3df1a1fa
commit 165778963e
2 changed files with 75 additions and 6 deletions

View File

@@ -21,11 +21,22 @@ use Monolog\Logger;
class NewRelicHandler extends AbstractProcessingHandler
{
/**
* {@inheritDoc}
* Name of the New Relic application that will receive logs from this handler.
*
* @var string
*/
public function __construct($level = Logger::ERROR, $bubble = true)
protected $appName;
/**
* {@inheritDoc}
*
* @param string $appName
*/
public function __construct($level = Logger::ERROR, $bubble = true, $appName = null)
{
parent::__construct($level, $bubble);
$this->appName = $appName;
}
/**
@@ -37,8 +48,8 @@ class NewRelicHandler extends AbstractProcessingHandler
throw new MissingExtensionException('The newrelic PHP extension is required to use the NewRelicHandler');
}
if (isset($record['context']['appname'])) {
newrelic_set_appname($record['context']['appname']);
if ($appName = $this->getAppName($record['context'])) {
$this->setNewRelicAppName($appName);
}
if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Exception) {
@@ -62,4 +73,30 @@ class NewRelicHandler extends AbstractProcessingHandler
{
return extension_loaded('newrelic');
}
/**
* Returns the appname where this log should be sent. Each log can override the default appname, set in this
* handler's constructor, by providing the appname in its context.
*
* @param array $context
* @return null|string
*/
protected function getAppName(array $context)
{
if (isset($context['appname'])) {
return $context['appname'];
}
return $this->appName;
}
/**
* Sets the NewRelic application that should receive this log.
*
* @param string $appName
*/
protected function setNewRelicAppName($appName)
{
newrelic_set_appname($appName);
}
}