Search code examples
phploggingphpunitsyslog

PHPUnit picking up on syslog messages?


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.


Solution

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