Skip to content

Commit

Permalink
Merge pull request #5 from finwe/master
Browse files Browse the repository at this point in the history
Priority mapping of Fatal errors to PSR Log CRITICAL
  • Loading branch information
fprochazka committed Mar 22, 2015
2 parents 83918ab + 30bef30 commit 8625d3e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
36 changes: 31 additions & 5 deletions src/Kdyby/Monolog/Diagnostics/MonologAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@
class MonologAdapter extends Logger
{

/**
* @var array
*/
private $priorityMap = array(
self::DEBUG => Monolog\Logger::DEBUG,
self::INFO => Monolog\Logger::INFO,
self::WARNING => Monolog\Logger::WARNING,
self::ERROR => Monolog\Logger::ERROR,
self::EXCEPTION => Monolog\Logger::CRITICAL,
self::CRITICAL => Monolog\Logger::CRITICAL
);

/**
* @var Monolog\Logger
*/
Expand Down Expand Up @@ -90,22 +102,36 @@ public function log($message, $priority = self::INFO)
$normalised = implode($message);
}

$levels = $this->monolog->getLevels();
$level = isset($levels[$uPriority = strtoupper($priority)]) ? $levels[$uPriority] : Monolog\Logger::INFO;

switch ($priority) {
case 'access':
$this->monolog->addInfo($normalised, array('priority' => $priority) + $context);
break;

default:
$this->monolog->addRecord($level, $normalised, array('priority' => $priority) + $context);
$this->monolog->addRecord(
$this->getLevel($priority),
$normalised,
array('priority' => $priority) + $context
);
}

return isset($context['tracy']) ? $context['tracy'] : '';
}


/**
* @param string $priority
* @return int
*/
protected function getLevel($priority)
{
if (isset($this->priorityMap[$priority])) {
return $this->priorityMap[$priority];
}

$levels = $this->monolog->getLevels();
return isset($levels[$uPriority = strtoupper($priority)]) ? $levels[$uPriority] : Monolog\Logger::INFO;
}


/**
* @internal
Expand Down
2 changes: 1 addition & 1 deletion tests/KdybyTests/Monolog/MonologAdapter.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class MonologAdapterTest extends Tester\TestCase
list($record) = $this->testHandler->getRecords();
Assert::same('kdyby', $record['channel']);
Assert::same('test message', $record['message']);
Assert::same('INFO', $record['level_name']);
Assert::same('CRITICAL', $record['level_name']);
Assert::same('exception', $record['context']['priority']);
Assert::same('https://www.kdyby.org/', $record['context']['at']);
Assert::same('exception-2014-08-14-11-11-26-88167e58be9dc0dfd12a61b3d8d33838.html', $record['context']['tracy']);
Expand Down

0 comments on commit 8625d3e

Please sign in to comment.