Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new SyslogHandler class #4

Merged
merged 2 commits into from
Mar 23, 2011
Merged

add new SyslogHandler class #4

merged 2 commits into from
Mar 23, 2011

Conversation

subsven
Copy link
Contributor

@subsven 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.

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);
throw new \UnexpectedValueException('unknown facility value "'.$facility.'" given');
}

if (!openlog($ident, LOG_NDELAY|LOG_PID, $facility)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@subsven
Copy link
Contributor Author

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.

to get a FALSE returned on connection problems isn't supported by
the underlying POSIX API.
@Seldaek
Copy link
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
@Seldaek
Copy link
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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants