简体   繁体   中英

PHP Monolog as Logger Only Prints Info Level

I have an class that I autoload through composer to all my other classes:

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

class LogStreamer
{

    protected static $instance;

    public static function getLogger()
    {
        if(! self::$instance){
            self::configureInstance();
        }

        return self::$instance;
    }

    private static function configureInstance() {
        $logger = new Logger('Q');
        $logger->pushHandler(new StreamHandler('/var/log/php/php.log'), Logger::INFO);
        self::$instance = $logger;
    }

    public static function info($message, array $context = []){
        self::getLogger()->addInfo($message, $context);
    }
    public static function debug($message, array $context = []){
        self::getLogger()->addDebug($message, $context);
    }

When I use it in other classes it looks like this:

public function getOptions() {
    LogStreamer::debug("Debug happened in log streamer");
    LogStreamer::info("Info happened in log streamer");

And it does print stuff out:

 [2018-07-02 09:56:34] Q.DEBUG: Debug happened in log streamer [] [] [2018-07-02 09:56:34] Q.INFO: Info happened in log streamer [] [] 

But of course I only want info log level in this case. It however, prints both. How can I fix this?

The log level is a parameter of the StreamHandler constructor, not the function pushHandler() .

from:

$logger->pushHandler(new StreamHandler('/var/log/php/php.log'), Logger::INFO);

to:

$logger->pushHandler(new StreamHandler('/var/log/php/php.log', Logger::INFO));

shoud do what you want

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM