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

Allow to know if the logger has handlers which handle a level #53

Closed
egeloen opened this issue Jan 26, 2012 · 8 comments
Closed

Allow to know if the logger has handlers which handle a level #53

egeloen opened this issue Jan 26, 2012 · 8 comments

Comments

@egeloen
Copy link

egeloen commented Jan 26, 2012

Hi,

I'm currently integrating Monolog in a project which needs to know if the logger has handlers for a specific level. In this case, my app will create some specific objects in order to debug more precisely what's happened.

Currently, I must override the logger for being able to acces handlers.

If you're OK with this feature, I will create a PR.

@stof
Copy link
Contributor

stof commented Jan 26, 2012

there is an issue here: an handler decides to handle a specific message. All built-in handlers use only the level for this but you could imagine other behaviors for a custom handler: handling the message only for a specific channel, or only when some key exist in the context, or even randomly if you see a reason to do so.

@egeloen
Copy link
Author

egeloen commented Jan 26, 2012

Yes but my interest here is to know if there is at least one handler which is able to handle my message according to the level. I will give an example:

<?php

if ($logger->hasHandlers(Logger::DEBUG) {
    $logger->addDebug('my_message');
}

With that, before adding a debug message, I'm able to detect if there is indeed an handler which will be able to handle my message.

@stof
Copy link
Contributor

stof commented Jan 26, 2012

well, you can add the debug message. Monolog already checks internally if the message is handled by an handler (and supporting any check done in the handler) and returns early otherwise.

@egeloen
Copy link
Author

egeloen commented Jan 26, 2012

Sorry but I will try to explain me better.

What I want to do is to instanciate an object which will debug the execution time of some queries if there is an handler which is able to handle the level I use.

So, I would like my code looks like:

<?php

$debugStack = new DebugStack();

if ($logger->hasHandlers(Logger::INFO)) {
    $debugStack->start();
}

// Execute queries

if ($logger->hasHandlers(Logger::INFO)) {
    $debugStack->stop();
    $logger->addInfo($debugStack->toString());
}

I hope you understand my interest to detect if the logger can handle my message :)

@Seldaek
Copy link
Owner

Seldaek commented Jan 28, 2012

I guess a better implementation would be to use something similar to the existing isHandling:

    public function isHandling($level) 
    {
        $record = array(
            'message' => '',
            'context' => array(),
            'level' => $level,
            'level_name' => self::getLevelName($level),
            'channel' => $this->name,
            'datetime' => new \DateTime(),
            'extra' => array(),
        );
        foreach ($this->handlers as $key => $handler) {
            if ($handler->isHandling($record)) {
                return true;
            }
        }

        return false;
    }

It's not so efficient though to create a dummy log record and loop through handlers.

@egeloen
Copy link
Author

egeloen commented Jan 28, 2012

That's exactly what I want :)

Are you agree to add this feature to the core?

@Seldaek
Copy link
Owner

Seldaek commented Jan 28, 2012

Done

@egeloen
Copy link
Author

egeloen commented Jan 28, 2012

Thanks !

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

3 participants