简体   繁体   English

如何使用Monolog关闭调试日志

[英]How to Turn off Debugging Logs with Monolog

I'm using Monolog in a project, it's not Symfony, just my own application that uses the stand-alone Monolog composer package. 我在一个项目中使用Monolog,它不是Symfony,只是我自己的应用程序使用独立的Monolog组合器包。

What I'd like to do is programmatically turn off debugging logs. 我想做的是以编程方式关闭调试日志。 I'm writing to a log file and I'm using the Monolog::StreamHandler. 我正在写一个日志文件,我正在使用Monolog :: StreamHandler。 I'm controlling whether the application is in debug mode or not with a Configuration class that gets the debug value from a configuration file. 我正在使用从配置文件获取调试值的Configuration类来控制应用程序是否处于调试模式。 So when someone changes that value to debugging is false, debug logging should turn off. 因此,当有人将该值更改为调试为false时,调试日志记录应该关闭。

I felt like the easiest way to do this would be to extend StreamHandler and override StreamHandler's write method like this. 我觉得最简单的方法是扩展StreamHandler并覆盖StreamHandler的write方法。

class DurpLogger extends StreamHandler {
    protected function write(array $record) {
        if ($this->getLevel() == Durp::Debug && !Configuration::debug()) {
            return;
        }
        parent::write($record);
    }
}

So if a log request comes in and the log level for the handler is set to DEBUG and the application's Configuration::debug() is FALSE then just return without writing the log message. 因此,如果日志请求进入并且处理程序的日志级别设置为DEBUG且应用程序的Configuration :: debug()为FALSE,则只返回而不写入日志消息。 Otherwise, StreamHandler will do its thing. 否则,StreamHandler会做它的事情。

I'm wondering if this is the best way to use Monolog or if there's perhaps a cleaner way to do this. 我想知道这是否是使用Monolog的最佳方式,或者是否有更简洁的方法来做到这一点。

I envision there being a handler in my application for DEBUG, INFO, ERROR and whatever levels I might need for my application. 我设想在我的应用程序中有一个处理程序,用于DEBUG,INFO,ERROR以及我的应用程序可能需要的任何级别。 Perhaps it makes sense to not rely on a Configuration::debug() that can only be TRUE or FALSE, but rather a Configuration::logLevel() that will allow me to more granularly control logging output. 也许有理由不依赖于只能为TRUE或FALSE的Configuration :: debug(),而是依赖于Configuration :: logLevel(),这将允许我更精细地控制日志记录输出。

But even still, does extending StreamHandler make the most sense when controlling Monolog at the application level? 但即便如此,在应用程序级别控制Monolog时,扩展StreamHandler是否最有意义?

UPDATE UPDATE

Now, I'm thinking something like this, that uses level rather than just boolean debug. 现在,我在想这样的事情,它使用的是水平而不仅仅是布尔调试。

class DurpLogger extends StreamHandler {
    public function __construct() {
        parent::__construct(Configuration::logFile(), Configuration::logLevel());
    }

    protected function write(array $record) {
        if (!($this->getLevel() >= Configuration::logLevel())) {
            return;
        }
        parent::write($record);
    }
}

Then I'd use it in the application like this. 然后我会在这样的应用程序中使用它。

class Durp {
  private $logger;
  public function __construct() {
        $this->logger = new Logger('durp-service');
        $this->logger->pushHandler(new DurpLogger());
        $this->logger->addDebug('Debugging enabled');
        $this->logger->addInfo('Starting Durp');
    }
}

I figured the StreamHandler handles the file writing stuff, so that's why I'm extending it. 我认为StreamHandler处理文件写入的东西,所以这就是我扩展它的原因。 And if I turn up the log level in Configuration to Logger::INFO, the "Debugging enabled" message doesn't get logged. 如果我将Configuration中的日志级别调高到Logger :: INFO,则不会记录“已启用调试”消息。

Open to suggestions to make this better. 接受建议,以使这更好。

A common alternative would be to use the NullHandler instead of the StreamHandler. 一种常见的替代方法是使用NullHandler而不是StreamHandler。

Maybe switch between them depending on your condition like follows: 也许根据您的情况在它们之间切换如下:

if (!Configuration::debug()) {
    $logger->pushHandler(new \Monolog\Handler\NullHandler());
}

I would like to give you an example that is more adapted to your usage, 我想给你一个更适合你用途的例子,
but I need to see some code in order to know how you use it. 但我需要看一些代码才能知道你是如何使用它的。

Update 更新

For the question about default format, the empty [] at end represent the extra data that can be added with log entries. 对于有关默认格式的问题,空的[]结尾表示可以添加日志条目的额外数据。

From @Seldaek (Monolog's owner) : 来自@Seldaek(Monolog的所有者):

The default format of the LineFormatter is: "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\\n". LineFormatter的默认格式为:“[%datetime%]%channel%。%level_name%:%message %% context %% extra%\\ n”。 the username/age is the context, and extra that is typically empty results in this empty array []. 用户名/年龄是上下文,而额外的通常是空的结果是这个空数组[]。

If you use processors to attach data to log records they typically write it to the extra key to avoid conflicts with context info. 如果使用处理器将数据附加到日志记录,则通常将其写入额外的密钥以避免与上下文信息冲突。 If it really is an issue for you you can change the default format and omit %extra%. 如果它确实是您的问题,您可以更改默认格式并省略%extra%。

Edit: As of Monolog 1.11 the LineFormatter has a $ignoreEmptyContextAndExtra parameter in the constructor that lets you remove these, so you can use this: 编辑:从Monolog 1.11开始,LineFormatter在构造函数中有一个$ ignoreEmptyContextAndExtra参数,允许您删除这些参数,因此您可以使用:

// the last "true" here tells it to remove empty []'s

$formatter = new LineFormatter(null, null, false, true);
$handler->setFormatter($formatter);

See How not to show last bracket in a monolog log line? 请参见如何不显示monolog日志行中的最后一个括号? and Symfony2 : use Processors while logging in different files about the processors which @Seldaek is talking about. Symfony2:在登录 @Seldaek正在谈论的处理器的不同文件时使用处理器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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