-
Notifications
You must be signed in to change notification settings - Fork 1
Add a Multichannel DatabaseLogger #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feature/command-bus-system
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?php | ||
|
|
||
| namespace Morebec\YDB\Enum; | ||
|
|
||
| use Morebec\ValueObjects\BasicEnum; | ||
|
|
||
| /** | ||
| * LoggerChannel | ||
| */ | ||
| class LoggerChannel extends BasicEnum | ||
| { | ||
| const __DEFAULT = 'default'; | ||
| const COMMAND = 'command'; | ||
| const EVENT = 'event'; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,21 +6,28 @@ | |
| use Monolog\Logger; | ||
| use Morebec\YDB\DatabaseConfig; | ||
| use Morebec\YDB\Service\Database; | ||
| use Morebec\YDB\Enum\LoggerChannel; | ||
|
|
||
| /** | ||
| * DatabaseLogger | ||
| */ | ||
| class DatabaseLogger extends Logger | ||
| { | ||
|
|
||
| private $loggers = []; | ||
|
|
||
| /** | ||
| * Constructs an instance of the database logger | ||
| * @param string $databasePath path to the database | ||
| */ | ||
| public function __construct(string $databasePath) | ||
| { | ||
| parent::__construct('default'); | ||
| $logsDir = $databasePath . "/" . Database::LOGS_DIR_NAME; | ||
|
|
||
| $this->pushHandler(new RotatingFileHandler($logsDir . '/database.log', Logger::DEBUG)); | ||
| $channels = LoggerChannel::getValues(); | ||
| foreach($channels as $channel){ | ||
| $this->loggers[$channel] = new Logger($channel); | ||
| $logsDir = $databasePath . "/" . Database::LOGS_DIR_NAME; | ||
| $this->pushHandler(new RotatingFileHandler($logsDir . "/".$channel."log", Logger::DEBUG)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we are in PHP7 you can make use of the new feature which allows passing variable names inside double-quoted strings: "$logsDir/$channel.log"I know initially I had set the Logger Level to debug, but could you change it to INFO?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also since it no longer relies on the Monolog Logger class's parent constructor, it should maybe drop the inheritance of the Monolog logger and instead implement the standard PHP Psr\LoggerInterface. DatabaseLogger::logWithChannel(string LogLevel, string $message, array $context = [], LoggerChannel $channel = null) |
||
| } | ||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| use Symfony\Component\EventDispatcher\EventDispatcher; | ||
| use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
| use Symfony\Component\Filesystem\Filesystem; | ||
| use Morebec\YDB\Enum\LoggerChannel; | ||
|
|
||
| /** | ||
| * The engine class serves as a service container. | ||
|
|
@@ -100,14 +101,20 @@ public function dispatchEvent(string $name, DatabaseEvent $event): void | |
| * @param string $message message | ||
| * @param array $context optional context data | ||
| */ | ||
| public function log(string $level, string $message, array $context = []): void | ||
| public function log(string $level, string $message, array $context = [],string $channel = LoggerChannel::__DEFAULT): void | ||
| { | ||
| if (!$this->logger) { | ||
| return; | ||
| } | ||
|
|
||
| Assertion::keyExists($this->logger->loggers, $channel, "Unsupported channel '$channel'"); | ||
| $context['database_root'] = (string)$this->database->getPath(); | ||
| $this->logger->log($level, $message, $context); | ||
| $this->logger->loggers[LoggerChannel::__DEFAULT]->log($level, $message, $context); | ||
| if ($channel === LoggerChannel::__DEFAULT) { | ||
| return; | ||
| } | ||
| // Log in channel specific logger | ||
| $this->logger->loggers[$channel]->log($level, $message, $context); | ||
|
|
||
|
Comment on lines
+104
to
+117
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logging logic/implementation should really not be in the engine, it is not it's responsibility. It shouldn't know how to log, instead, it should only know that it is possible to log by calling the Logger. Also since LoggerChannel is now a type that handles validation internally, we could get rid of the Assertion call you made. if(!$channel) $channel = new LoggerChannel(LoggerChannel::__DEFAULT);(again this code should go in the logger, not the engine) |
||
| } | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good!
I have done some new devs on the branch to reintroduce queries, it would be great to be able to log to a query channel:
Good on the __DEFAULT keyword circumvention.