PHPUnit收到系统日志消息

PHPUnit收到系统日志消息

本文介绍了PHPUnit收到系统日志消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一种打开日志的方法来测试记录器类:

I am testing a logger class with a method that opens a log like so:

openlog($this->identifier, $this->option, $this->facility);
syslog($level, $message)
closelog();

我的记录器写入的$facility当前设置为LOCAL0

The $facility that my logger writes to is currently set as LOCAL0

当我对记录器进行单元测试时,会收到以下消息:

When I unit test my logger I get the following message:

Broadcast message from systemd-journald@myWS:

phpserver7.0[9125]: Logger message

如何使用PHPUnit或代码隐藏此消息?

How can I suppress this message with PHPUnit or in my code?

这似乎仅在我记录严重性为紧急"(意味着严重性级别为0)的消息时发生.

This only seems to happen when I log a message with a severity of "emergency" meaning a severity level of 0.

https://en.wikipedia.org/wiki/Syslog#Severity_level

维基百科指出:

尽管如此,它仍然是PSR-3记录器摘要的一部分,因此我希望能够使用PHPUnit禁止显示该消息.

Still, it is part of the PSR-3 logger abstract though so I would just like to be able to suppress the message with PHPUnit.

推荐答案

在测试方法中,可以通过将输出缓冲区包装在产生输出的调用周围来抑制输出.示例:

In your test method, you can suppress output by wrapping an output buffer around your call that produces output. Example:

/**
 * @preserveGlobalState disabled
 * @runInSeparateProcess
 */
public function testOutputCanSend()
{
    ob_start();

    // Do some stuff here that outputs directly, e.g
    openlog($this->identifier, $this->option, $this->facility);
    syslog($level, $message)
    closelog();

    ob_end_clean();
}

这篇关于PHPUnit收到系统日志消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 18:21