Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

add new SyslogHandler class #4

Merged
merged 2 commits into from Mar 23, 2011

Conversation

Projects
None yet
2 participants
Contributor

subsven commented Mar 22, 2011

Since there is currently no syslog support in Monolog (and it even was on your todo list :-) ) here is a new SyslogHandler class.

A basic usage example:

   $log = new Logger('application');
   $syslog = new SyslogHandler('myfacility', 'local6');
   $formatter = new LineFormatter("%channel%.%level_name%: %message% %extra%");
   $syslog->setFormatter($formatter);
   $log->pushHandler($syslog);

The special formatter is needed since the timestamp is included automatically in all syslog messages.
The facility can be given using PHP constants (LOG_LOCAL5) or by string ('local5') to simplify setting the values from a configuration file.

I couldn't think of any unit test approaches for actually writing the message since mocking the syslog() function isn't possible and all tests using the real syslog() function would depend on a special syslogd configuration. So currently the unit tests only cover the constructor.

@subsven subsven add SyslogHandler to Monolog
usage example:

   $log = new Logger('application');
   $syslog = new SyslogHandler('myfacility', 'local6');
   $formatter = new LineFormatter("%channel%.%level_name%: %message% %extra%");
   $syslog->setFormatter($formatter);
   $log->pushHandler($syslog);
d9d0f51

@Seldaek Seldaek commented on an outdated diff Mar 22, 2011

src/Monolog/Handler/SyslogHandler.php
+ 'user' => LOG_USER,
+ 'uucp' => LOG_UUCP,
+ );
+
+ public function __construct($ident, $facility = LOG_USER, $level = Logger::DEBUG, $bubble = true)
+ {
+ parent::__construct($level, $bubble);
+
+ // convert textual description of facility to syslog constant
+ if (array_key_exists(strtolower($facility), $this->facilities)) {
+ $facility = $this->facilities[strtolower($facility)];
+ } else if (!in_array($facility, array_values($this->facilities), true)) {
+ throw new \UnexpectedValueException('unknown facility value "'.$facility.'" given');
+ }
+
+ if (!openlog($ident, LOG_NDELAY|LOG_PID, $facility)) {
@Seldaek

Seldaek Mar 22, 2011

Owner

Is there a good reason for adding LOG_NDELAY here? Sounds to me like you want to lazy-load that stuff in case nothing is worth logging.

Contributor

subsven commented Mar 22, 2011

You're probably right, using LOG_NDELAY doesn't bring any benefits. I initially suspected that I would be able to catch connection problems (missing unix domain socket of syslogd etc.) initially when setting up logging - but a look at the openlog(3) manpage reveals the profile of the real openlog() libc function:
void openlog(const char *ident, int option, int facility);
So the behaviour "Returns TRUE on success or FALSE on failure" described in the openlog() PHP documentation is nonsense - the PHP source code shows that openlog() always returns TRUE.

So - yes, you are right, LOG_NDELAY is really useless in this case. I'll update the pull request.

@subsven subsven remove LOG_NDELAY from the openlog() call since the expected behaviour
to get a FALSE returned on connection problems isn't supported by
the underlying POSIX API.
38a4ddf
Owner

Seldaek commented Mar 23, 2011

Thanks, I'll merge this later today then.

@Seldaek Seldaek merged commit 38a4ddf into Seldaek:master Mar 23, 2011

Owner

Seldaek commented Mar 23, 2011

Merged, just had to adjust some details for coding style & windows support, thanks again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment