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

Remove logs #21

Open
joesantos418 opened this issue Nov 18, 2020 · 2 comments
Open

Remove logs #21

joesantos418 opened this issue Nov 18, 2020 · 2 comments
Milestone

Comments

@joesantos418
Copy link
Contributor

Logging errors and other things should be a responsibility for the user. I move we remove logs and use exceptions where it makes sense.

@xico42
Copy link
Contributor

xico42 commented Dec 7, 2020

For this, I believe that the best would be to add some sort of event propagation and let the user add "subscribers" to handle those events.

Then, we could create a default event subscriber that implements the same logging logic of today in order to allow for a smoother transition and keeping the current behaviour as it is.

I would suggest for us to implement something similar to what Ganesha does.

As an example, today we log any consumer errors (code extracted from our Consumer class):

        if (!in_array($message->err, self::IGNORABLE_CONSUME_ERRORS)) {
            $this->logger->error($message, null, 'CONSUMER');
            throw new KafkaConsumerException($message->errstr(), $message->err);
        }

We could internally replace this with the following:

        if (!in_array($message->err, self::IGNORABLE_CONSUME_ERRORS)) {
            $this->eventDispatcher->dispatch(new FailedToConsumeMessage($message));
            throw new KafkaConsumerException($message->errstr(), $message->err);
        }

This dispatcher component could be something like the following:

<?php

declare(strict_types=1);

namespace Kafka\Consumer;

class EventDispatcher
{
    private $subscribers;

    public function __construct(array $subscribers)
    {
        $this->subscribers = $subscribers;
    }

    public function dispatch($event): void
    {
        // Get all subscribers for the given event
        $subscribers = $this->subscribers[get_class($event)] ?? [];
        
        // Execute every subscriber
        foreach ($subscribers as $subscriber) {
            $subscriber($event);
        }
    }
}

Then, the user could provide any callable as a subscriber to handle that type of event:

$subscriber = function (FailedToConsumeMessage $event) {
    $logger->error($message, null, 'CONSUMER');
};

xico42 referenced this issue in xico42/php-kafka-consumer Dec 9, 2020
Implement the concept of events and create a event subscriber to
dispatch events.

Extract logging logic from the consumer into an event subscriber in
order to decouple logging logic from the consumer class.

Fixes #21
@joesantos418 joesantos418 added this to the Version 3.0.0 milestone Jan 29, 2021
@joesantos418
Copy link
Contributor Author

The job of communicating errors is well performed by the throwing of exceptions. Every user has different log requirements and catering to all of them is beyond the scope of this project. The need for this log is up for discussion, but as of right now, the proposal is to remove them.

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

No branches or pull requests

2 participants