首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多次调用的Monolog消息

多次调用的Monolog消息
EN

Stack Overflow用户
提问于 2022-07-30 04:20:54
回答 1查看 40关注 0票数 0

我有一个用于monolog的包装类,如下所示

log.php

代码语言:javascript
复制
<?php

namespace Plugin\Core;

use Monolog\Handler\StreamHandler;
use Monolog\Level;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;

class Log
{

    private $logger;

    private $path;

    public function __construct($name = 'wpmatrimony', $path = NULL)
    {
   
        if(empty($path)){
            
            $path = WPMATRIMONY_PLUGIN_DIR.'./logs/'.$name.'.log';

        }

        $this->path = $path;

        $this->logger = new Logger($name);

        $this->logger->setTimezone(new \DateTimeZone('ASIA/KOLKATA'));

    }

    private function handler($level){

        // https://stackoverflow.com/questions/13968967/how-not-to-show-last-bracket-in-a-monolog-log-line

        $handler = new StreamHandler($this->path, $level);

        $dateFormat = "d-m-Y H:i:s";

        $handler->setFormatter(new LineFormatter(NULL, $dateFormat, false, true));

        return $handler;

    }

    public function info($log){

        $this->logger->pushHandler($this->handler(Level::Info));

        $this->logger->info($log);

    }

    public function debug($log){

        $this->logger->pushHandler($this->handler(Level::Debug));

        $this->logger->debug($log);

    }

    public function error($log){

        $this->logger->pushHandler($this->handler(Level::Error));

        $this->logger->error($log);

    }

    public function warning($log){

        $this->logger->pushHandler($this->handler(Level::Warning));

        $this->logger->warning($log);

    }

    public function notice($log){

        $this->logger->pushHandler($this->handler(Level::Notice));

        $this->logger->notice($log);

    }


    public function critical($log){

        $this->logger->pushHandler($this->handler(Level::Critical));

        $this->logger->critical($log);

    }

    public function alert($log){

        $this->logger->pushHandler($this->handler(Level::Alert));

        $this->logger->alert($log);

    }

    public function emergency($log){

        $this->logger->pushHandler($this->handler(Level::Emergency));

        $this->logger->emergency($log);

    }

}

这很好用。但是我得到了每一行的日志增量,在下面的例子中

代码语言:javascript
复制
$log = new Log('test-monolog');
$log->debug('debug');
$log->info('info');
$log->warning('warning');

实际输出

代码语言:javascript
复制
[30-07-2022 09:41:50] test-monolog.DEBUG: debug  
[30-07-2022 09:41:50] test-monolog.INFO: info  
[30-07-2022 09:41:50] test-monolog.INFO: info  
[30-07-2022 09:41:50] test-monolog.WARNING: warning  
[30-07-2022 09:41:50] test-monolog.WARNING: warning  
[30-07-2022 09:41:50] test-monolog.WARNING: warning 

预期输出

代码语言:javascript
复制
[30-07-2022 09:47:22] test-monolog.DEBUG: debug  
[30-07-2022 09:47:22] test-monolog.INFO: info  
[30-07-2022 09:47:22] test-monolog.WARNING: warning

如果我的代码如下所示,我将得到预期的输出

代码语言:javascript
复制
 $log = new Log('test-monolog');
 $log->debug('debug');
 $log = new Log('test-monolog');
 $log->info('info');
 $log = new Log('test-monolog');
 $log->warning('warning');

如何避免多次调用Log类?

EN

回答 1

Stack Overflow用户

发布于 2022-07-31 02:39:52

看起来,monolog没有清除之前的实例化,而是在冒泡。所以下面的代码起作用了。

代码语言:javascript
复制
<?php

namespace Plugin\Core;

use Monolog\Handler\StreamHandler;
use Monolog\Level;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;

class Log
{

   
    private $path;

    private $name;

    public function __construct($name = 'wpmatrimony', $path = NULL)
    {
   
        if(empty($path)){
            
            $path = WPMATRIMONY_PLUGIN_DIR.'./logs/'.$name.'.log';

        }

        $this->path = $path;

        $this->name = $name;

        

    }

    public  function logger($level){

        $logger = new Logger($this->name);

        $logger->setTimezone(new \DateTimeZone('ASIA/KOLKATA'));

        $handler = new StreamHandler($this->path, $level);

        $dateFormat = "d-m-Y H:i:s";

        $handler->setFormatter(new LineFormatter(NULL, $dateFormat, false, true));

        $logger->pushHandler($handler);

        return $logger;

    }

    public function info($log){

        $logger = $this->logger(Level::Info);

        $logger->info($log);

        return;

    }

    public function debug($log){

        $logger = $this->logger(Level::Debug);

        $logger->debug($log);

        return;

    }

    public function error($log){

        $logger = $this->logger(Level::Error);

        $logger->error($log);

        return;

    }

    public function warning($log){

        $logger = $this->logger(Level::warning);

        $logger->warning($log);

        return;
    }

    public function notice($log){

        $logger = $this->logger(Level::Notice);

        $logger->notice($log);

        return;

    }


    public function critical($log){

        $logger = $this->logger(Level::Critical);

        $logger->critical($log);

        return;

    }

    public function alert($log){

        $logger = $this->logger(Level::Alert);

        $logger->alert($log);

        return;

    }

    public function emergency($log){

        $logger = $this->logger(Level::Emergency);

        $logger->emergency($log);

        return;

    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73172833

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档