GithubHelp home page GithubHelp logo

Comments (8)

stof avatar stof commented on May 17, 2024

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.

from monolog.

egeloen avatar egeloen commented on May 17, 2024

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.

from monolog.

stof avatar stof commented on May 17, 2024

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.

from monolog.

egeloen avatar egeloen commented on May 17, 2024

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 :)

from monolog.

Seldaek avatar Seldaek commented on May 17, 2024

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.

from monolog.

egeloen avatar egeloen commented on May 17, 2024

That's exactly what I want :)

Are you agree to add this feature to the core?

from monolog.

Seldaek avatar Seldaek commented on May 17, 2024

Done

from monolog.

egeloen avatar egeloen commented on May 17, 2024

Thanks !

from monolog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.