我有一个用于monolog的包装类,如下所示
log.php
<?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);
}
}这很好用。但是我得到了每一行的日志增量,在下面的例子中
$log = new Log('test-monolog');
$log->debug('debug');
$log->info('info');
$log->warning('warning');实际输出
[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 预期输出
[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如果我的代码如下所示,我将得到预期的输出
$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类?
发布于 2022-07-31 02:39:52
看起来,monolog没有清除之前的实例化,而是在冒泡。所以下面的代码起作用了。
<?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;
}
}https://stackoverflow.com/questions/73172833
复制相似问题