-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored logger, so that we expose our own adapter. (#325)
Refactored logger, so that we expose our own adapter, which covers functionality of previously used `AbstractOuzoLogger`. Added ability to enhance context parameters.
- Loading branch information
1 parent
ffe9daa
commit 6b16790
Showing
8 changed files
with
184 additions
and
121 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
/* | ||
* Copyright (c) Ouzo contributors, https://github.com/letsdrink/ouzo | ||
* This file is made available under the MIT License (view the LICENSE file for more information). | ||
*/ | ||
|
||
namespace Ouzo\Logger; | ||
|
||
interface ContextEnhancer | ||
{ | ||
public function enhanceContext(array $context): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
/* | ||
* Copyright (c) Ouzo contributors, https://github.com/letsdrink/ouzo | ||
* This file is made available under the MIT License (view the LICENSE file for more information). | ||
*/ | ||
|
||
namespace Ouzo\Logger; | ||
|
||
use Ouzo\Config; | ||
use Ouzo\Utilities\Arrays; | ||
use Ouzo\Utilities\Strings; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\LogLevel; | ||
|
||
class LoggerAdapter | ||
{ | ||
private MessageFormatter $messageFormatter; | ||
private ?ContextEnhancer $contextEnhancer; | ||
private array $minimalLevels; | ||
|
||
public function __construct( | ||
private string $name, | ||
string $configuration, | ||
private LoggerInterface $logger, | ||
) | ||
{ | ||
$loggerConfiguration = Config::getValue('logger', $configuration) ?: []; | ||
$messageFormatter = Arrays::getValue($loggerConfiguration, 'formatter', DefaultMessageFormatter::class); | ||
$contextEnhancer = Arrays::getValue($loggerConfiguration, 'context_enhancer'); | ||
|
||
$this->messageFormatter = new $messageFormatter(); | ||
$this->contextEnhancer = is_null($contextEnhancer) ? null : new $contextEnhancer(); | ||
$this->minimalLevels = Arrays::getValue($loggerConfiguration, 'minimal_levels', []); | ||
} | ||
|
||
public function emergency(string $message, array $context = []): void | ||
{ | ||
$this->log(LogLevel::EMERGENCY, $message, $context); | ||
} | ||
|
||
public function alert(string $message, array $context = []): void | ||
{ | ||
$this->log(LogLevel::ALERT, $message, $context); | ||
} | ||
|
||
public function critical(string $message, array $context = []): void | ||
{ | ||
$this->log(LogLevel::CRITICAL, $message, $context); | ||
} | ||
|
||
public function error(string $message, array $context = []): void | ||
{ | ||
$this->log(LogLevel::ERROR, $message, $context); | ||
} | ||
|
||
public function warning(string $message, array $context = []): void | ||
{ | ||
$this->log(LogLevel::WARNING, $message, $context); | ||
} | ||
|
||
public function notice(string $message, array $context = []): void | ||
{ | ||
$this->log(LogLevel::NOTICE, $message, $context); | ||
} | ||
|
||
public function info(string $message, array $context = []): void | ||
{ | ||
$this->log(LogLevel::INFO, $message, $context); | ||
} | ||
|
||
public function debug(string $message, array $context = []): void | ||
{ | ||
if ($this->isDebug()) { | ||
$this->log(LogLevel::DEBUG, $message, $context); | ||
} | ||
} | ||
|
||
public function log(string $level, $message, array $context = []): void | ||
{ | ||
$ouzoLevel = LogLevelTranslator::toSyslogLevel($level); | ||
$minimalLevel = $this->minimalLevels ? Arrays::getValue($this->minimalLevels, $this->name, LOG_DEBUG) : LOG_DEBUG; | ||
if ($ouzoLevel <= $minimalLevel) { | ||
$message = $this->messageFormatter->format($this->name, $level, $message); | ||
if (!is_null($this->contextEnhancer)) { | ||
$context = $this->contextEnhancer->enhanceContext($context); | ||
} | ||
if (!empty($context)) { | ||
$message = Strings::sprintAssoc($message, $context); | ||
} | ||
$this->logger->log($level, $message, $context); | ||
} | ||
} | ||
|
||
private function isDebug(): bool | ||
{ | ||
return Config::getValue('debug') === true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.