Skip to content
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

Add a Multichannel DatabaseLogger #12

Open
wants to merge 4 commits into
base: feature/command-bus-system
Choose a base branch
from
Open

Add a Multichannel DatabaseLogger #12

wants to merge 4 commits into from

Conversation

andy-lsh
Copy link
Contributor

add command and event log

Copy link
Contributor

@jwillp jwillp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comments in the review.
If you want to run the tests to see if it works you can run the following command:

composer test

Currently, Travis indicates that this commit introduces errors could you check this as well?
(Every pull request/commits are tested by Travis to see if they are valid or not.)

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)

Comment on lines +104 to +117
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);

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)

/**
* 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.

@jwillp jwillp changed the title Feature/command bus system Add a Multichannel DatabaseLogger Nov 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants