Skip to content

Commit

Permalink
Merge pull request #2 from fidelize/SQDT-774
Browse files Browse the repository at this point in the history
Sqdt 774
  • Loading branch information
renatofl authored Jan 6, 2023
2 parents 2ebd36a + 2420a1f commit b602ad9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 183 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/
vendor/
bin/
composer.lock
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
],
"require": {
"php": ">=5.4",
"monolog/monolog": "*"
"monolog/monolog": "^2.0",
"yiisoft/yii": "^1"
},
"require-dev": {
"phpunit/phpunit": "*"
Expand Down
179 changes: 0 additions & 179 deletions src/MonologFileLogRoute.php

This file was deleted.

54 changes: 51 additions & 3 deletions src/MonologLogRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,72 @@

namespace YiiMonolog;

use Monolog\Formatter\FormatterInterface;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Log\InvalidArgumentException;
use Psr\Log\LoggerInterface;
use Monolog\Registry;
use Psr\Log\LogLevel;

class MonologLogRoute extends \CLogRoute
{
/** @var string */
public $loggerName = 'main';
const FILE_PERMISSION = 0666;

/** @var LoggerInterface */
protected $logger;

/** @var string */
public $name = 'application';

/** @var FormatterInterface */
public $formatter;

/** @var string */
public $stream;

/** @var array */
public $processors = [];

/**
* @inheritdoc
*/
public function init()
{
$this->logger = Registry::getInstance($this->loggerName);
$this->logger = $this->getMonolog();
}

/**
* @return LoggerInterface
*/
protected function getMonolog()
{
$key = md5(serialize([
$this->name,
$this->formatter,
$this->stream,
$this->processors
]));

if (Registry::hasLogger($key)) {
return Registry::getInstance($key);
}

$logger = new Logger($this->name);

// Create a handler
$handler = new StreamHandler($this->stream, LogLevel::DEBUG, true, self::FILE_PERMISSION);
$handler->setFormatter($this->formatter);

$logger->pushHandler($handler);

foreach ($this->processors as $processor) {
$logger->pushProcessor(new $processor());
}

Registry::addLogger($logger, $key);

return $logger;
}

/**
Expand Down

0 comments on commit b602ad9

Please sign in to comment.