Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Enum/LoggerChannel.php
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
Copy link
Contributor

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:

const QUERY = 'query'

Good on the __DEFAULT keyword circumvention.

{
const __DEFAULT = 'default';
const COMMAND = 'command';
const EVENT = 'event';
}
15 changes: 11 additions & 4 deletions src/Service/DatabaseLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Copy link
Contributor

Choose a reason for hiding this comment

The 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?
Thank you

Copy link
Contributor

Choose a reason for hiding this comment

The 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.
However, since the signature of the log method from that interface does not support the Channel Loggers, a new method should be added:

DatabaseLogger::logWithChannel(string LogLevel, string $message, array $context = [], LoggerChannel $channel = null)

}

}
}
13 changes: 10 additions & 3 deletions src/Service/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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.
The logger should contain the implementation details.
Therefore it would be better to implement the log method in the logToChannel method of the logger I discussed earlier.

Also since LoggerChannel is now a type that handles validation internally, we could get rid of the Assertion call you made.
By doing this log(string $level, string $message, array $context = [], ?LoggerChannel $channel = null)
Unfortunately PHP does not allow custom default argument values for cutom types.
To circumvent this, usually, the following strategy is used:
set a default argument value of null
and then check for null in the method

if(!$channel) $channel = new LoggerChannel(LoggerChannel::__DEFAULT);

(again this code should go in the logger, not the engine)

}

/**
Expand Down