Logging PHP to Windows Event Log

Categories:

PHP allows you two methods of logging PHP events using the error_log directive in php.ini. A typical set up would be to configure PHP to log to a flat file, by setting the error_log value to the full path and file name to your php log file. For smaller environments this might be fine, however if you're using a event log monitoring agent to report and monitor server operations centrally this may not be ideal as all you PHP related events will be missed. In this guide we'll look at how you can configure PHP so it logs events to your Windows application event log rather than to a flat file.

In previous versions of PHP it was could be quite troublesome to get PHP to log to the Windows Event Log, and often required alterating the permissions on the event log files themselves so IIS could write to them. With PHP 5.2 it is actually very easy nowadays to get PHP to log to your Windows Event Log. Simply open your php.ini file with your favourite text editor and replace;

log_errors = Off

With;

log_errors = On

This will turn on PHP logging in PHP. Next step is to direct PHP to log events to your Windows Event Log by finding this;

;error_log = syslog

And replacing it with;

error_log = syslog

Although syslog is actually the *nix equivalent of the Windows Event Log we still need to specify it here as PHP does not differentiate between the two, however PHP will know to log to the Windows Event Log when being used on a Windows System. With that done save your php.ini file and recycle the IIS application pools using PHP, and you should now start seeing PHP events being logged to your Windows Application Log in Event Viewer.

One final setting you may wish to tweak in your php.ini file is the error_reporting directive. This setting specifies the level of logging PHP does, and by default is set to show all errors except coding standards warnings. There are several levels of logging that can be set, and they can be combined or explicitly denied depending on your requirements. Refer to the notes in your php.ini file for more information.

Hopefully this guide has helped someone. If you have any comments or feedback on this article, please feel free to post here. If you have a specific support requirement please post in the forums for free support (best effort support only), or use the contact form if you wish to contact me regarding fee based consulting.

Average rating
(5 votes)

Comments

Anonymous's picture

PHP Error Logging for IIS

Hi,

Thanks for the advice - however, after making this change, Windows does still not log PHP errors (or errors that occur as a result of executing a PHP file).

I have located php.ini for this server (actually located in
\PHP\

Have you any more suggestions on what else needs to be done or what I can try?

Many thanks for your help,

Kind Regards,

Roger Davies

Brashquido's picture

IIS has been restarted to

IIS has been restarted to make the changes effective? Does phpinfo report that these settings are effective as you expect?

----------------
Dominic Ryan
6 x Microsoft IIS MVP, MCSE, MCSA
IIS Aid owner/webmaster

Anonymous's picture

Description for event cannot be found

Hi,

The settings in PHP to log in the Application log are working fine with your description, but all the logs have this text:

The description for Event ID 2 from source PHP-5.4.30 cannot be found.

It would seem that we need to tell Windows how to propperly recognize PHP messages to avoid this, right? Any clue or hint on how to do this would be appreciated!

Thanks!

Jose