Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:mallgroup/monolog
Browse files Browse the repository at this point in the history
  • Loading branch information
Radovan Kepák committed May 27, 2021
2 parents 2737f69 + 62619d5 commit 4568598
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 20 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
],
"require": {
"php": ">=7.4 <8.1",
"nette/di": "^3.0.5",
"nette/di": "^3.0.7",
"nette/utils": "^3.0",
"monolog/monolog": "^2.2",
"nette/schema": "^1.2"
Expand Down
30 changes: 21 additions & 9 deletions src/DI/MonologExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ public function getConfigSchema(): Schema
'tracyHook' => Expect::bool(true),
'tracyBaseUrl' => Expect::string(),
'usePriorityProcessor' => Expect::bool(true),
'registerFallback' => Expect::bool(false),
'fallback' => Expect::structure([
'register' => Expect::bool(false),
'defaultFormat' => Expect::string('[%datetime%] %message% %context% %extra%'),
'priorityFormat' => Expect::string('[%datetime%] %level_name%: %message% %context% %extra%'),
]),
'registerFallback' => Expect::bool(false)->deprecated(),
'accessPriority' => Expect::string(ILogger::INFO),
]);
}
Expand All @@ -86,6 +91,11 @@ public function loadConfiguration(): void
# Load other stuff
$this->loadHandlers();
$this->loadProcessors();

# registerFallback transformation
if ($this->config->registerFallback) {
$this->config->fallback->register = true;
}
}

protected function loadHandlers(): void
Expand Down Expand Up @@ -132,31 +142,31 @@ protected function getDefinition($definition, string $name)
{
$builder = $this->getContainerBuilder();

// String definition
# String definition
if (is_string($definition)) {

// @alias
# @alias
if (Strings::startsWith($definition, '@')) {
$defName = substr($definition, 1);

return $builder->hasDefinition($defName) ? $builder->getDefinition($defName) : $definition;
}

// Inline definition
# Inline definition
return $builder
->addDefinition($name, (new ServiceDefinition)->setType($definition))
->setAutowired(false);
}

// Add service and set autowired
# Add service and set autowired
if (is_array($definition)) {
$definition['autowired'] ??= false;
}
$this->compiler->loadDefinitionsFromConfig([
$name => $definition,
]);

return $name;
return '@' . $name;
}

public function beforeCompile(): void
Expand All @@ -168,20 +178,22 @@ public function beforeCompile(): void
$logger->setArgument('handlers', $this->getDefinitionsByPriority($this->handlers));
$logger->setArgument('processors', $this->getDefinitionsByPriority($this->processors));

// Register fallback, if no handlers is set or registerFallback is true
if (!$this->handlers || $this->config->registerFallback) {
# Register fallback, if no handlers is set or fallback->register is true
if (!$this->handlers || $this->config->fallback->register) {
$builder
->addDefinition($this->prefix('fallback'))
->setFactory(FallbackNetteHandler::class, [
'appName' => $this->config->name,
'logDir' => $this->config->logDir,
'defaultFormat' => $this->config->fallback->defaultFormat,
'priorityFormat' => $this->config->fallback->priorityFormat,
])
->setAutowired(false);

$logger->addSetup('pushHandler', ['@' . $this->prefix('fallback')]);
}

// Decorator for LoggerAwareInterface
# Decorator for LoggerAwareInterface
foreach ($builder->findByType(LoggerAwareInterface::class) as $service) {
/** @var ServiceDefinition $service */
$service->addSetup('setLogger', ['@' . $this->prefix('logger')]);
Expand Down
16 changes: 10 additions & 6 deletions src/Handler/FallbackNetteHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,25 @@
class FallbackNetteHandler extends \Monolog\Handler\ErrorLogHandler
{

use \Nette\SmartObject;

private string $appName;
private string $logDir;
private LineFormatter $defaultFormatter;
private LineFormatter $priorityFormatter;

public function __construct(string $appName, string $logDir, bool $expandNewlines = FALSE, int $level = MonologLogger::DEBUG)
{
public function __construct(
string $appName,
string $logDir,
string $defaultFormat,
string $priorityFormat,
bool $expandNewlines = FALSE,
int $level = MonologLogger::DEBUG
) {
parent::__construct(self::SAPI, $level, TRUE, $expandNewlines);
$this->appName = $appName;
$this->logDir = $logDir;

$this->defaultFormatter = new LineFormatter('[%datetime%] %message% %context% %extra%');
$this->priorityFormatter = new LineFormatter('[%datetime%] %level_name%: %message% %context% %extra%');
$this->defaultFormatter = new LineFormatter($defaultFormat);
$this->priorityFormatter = new LineFormatter($priorityFormat);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/Monolog/ExtensionTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Tests\Monolog;

use Mallgroup\Monolog\CustomChannel;
use Mallgroup\Monolog\DI\MonologExtension;
use Mallgroup\Monolog\Handler\FallbackNetteHandler;
use Mallgroup\Monolog\Logger as MonologLogger;
use Mallgroup\Monolog\Processor\PriorityProcessor;
use Mallgroup\Monolog\Processor\TracyExceptionProcessor;
Expand Down Expand Up @@ -247,6 +248,15 @@ class ExtensionTest extends \Tester\TestCase
Assert::type(GitProcessor::class, array_shift($processors));
}

public function testDeprecated(): void {
Assert::error(function(){
$dic = $this->createContainer('deprecated');
$logger = $dic->getByType(MonologLogger::class);
$handlers = $logger->getHandlers();

Assert::type(FallbackNetteHandler::class, array_shift($handlers));
}, E_USER_DEPRECATED);
}
}

(new ExtensionTest())->run();
7 changes: 6 additions & 1 deletion tests/Monolog/FallbackNetteHandlerTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ class FallbackNetteHandlerTest extends \Tester\TestCase
unlink($logFile);
}

$this->handler = new FallbackNetteHandler('mall', $this->logDir);
$this->handler = new FallbackNetteHandler(
'mall',
$this->logDir,
'[%datetime%] %message% %context% %extra%',
'[%datetime%] %level_name%: %message% %context% %extra%'
);

$this->now = new DateTime();
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Monolog/config/default.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
monolog:
registerFallback: true
fallback:
register: true
logDir: %tempDir%/log
3 changes: 3 additions & 0 deletions tests/Monolog/config/deprecated.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
monolog:
registerFallback: true
logDir: %tempDir%/log
8 changes: 6 additions & 2 deletions tests/Monolog/config/handlers.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
services:
browserConsoleHandler: Monolog\Handler\BrowserConsoleHandler

monolog:
logDir: %tempDir%/log
handlers:
- Monolog\Handler\BrowserConsoleHandler
- Monolog\Handler\ChromePHPHandler
- @browserConsoleHandler
-
factory: Monolog\Handler\ChromePHPHandler
- Monolog\Handler\NewRelicHandler

0 comments on commit 4568598

Please sign in to comment.