PHPUnit picking up on syslog messages?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-23 19:18:06

问题


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();

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  

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

Edit:

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

Wikipedia states:

This level should not be used by applications.

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.


回答1:


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();
}


来源:https://stackoverflow.com/questions/41163822/phpunit-picking-up-on-syslog-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!