How to set up error logging with PHP

So you got finished with your brand new website. It is completely PHP driven and looks very nice. But are you sure that everything works perfectly? Under every circumstances?

No. You can never be absolutely sure. That is why you need a log file to see if there where some errors. Well, if you are the kind of person that doesn’t care if some jerks who behaved wrong on you website get error messages, then you probably don’t need an error log file.

In case you aren’t using your own server, you normally won’t have access to automatically generated log files. That’s why we need to generate them especially for us.

And that’s how it’s done:

ini_set('error_reporting', E_ALL);
error_reporting(E_ALL);
ini_set('log_errors',TRUE);
ini_set('html_errors',FALSE);
ini_set('error_log','/home/htdocs/webXX/html/error_log.txt');
ini_set('display_errors',FALSE);

The first two lines tell PHP to report every error. Even the tiny ones. Line three says that those errors should be logged. The next line makes sure that these errors won’t include HTML and line five sets the path to the log file. The last line is to prevent the errors from being displayed to the user. Most users normally have no idea what’s going on when they get an error message presented. So that’s why.

Just include the code in scripts where you want to keep track of errors. Now you can see if anyone got any errors by checking the error_log.txt file.

Published by

Julian Bez

Julian Bez

Julian Bez is a software engineer and former startup founder from Berlin, Germany.

  • http://geckoblaster.de Martin

    In PHP 5 a new error level E_STRICT is available. As E_STRICT is not included within E_ALL you have to explicitly enable this kind of error level. Enabling E_STRICT during development has some benefits. STRICT messages will help you to use the latest and greatest suggested method of coding, for example warn you about using deprecated functions.

    To enable this, use error_reporting(E_ALL | E_STRICT);

  • http://www.ticketwood.com Ticket

    In PHP 5 a new error level E_STRICT is available. As E_STRICT is not included within E_ALL you have to explicitly enable this kind of error level. Enabling E_STRICT during development has some benefits. STRICT messages will help you to use the latest and greatest suggested method of coding, for example warn you about using deprecated functions.

  • Sean

    Do you need to create the error log file yourself first? Or does php create this for you when error logging is enabled?

    Also, if you need to create this file, does it need any special permissions?

  • Sinan

    i tried to use this, it works perfectly on ma locoal host WAMP Server, but it does not work on the internet hosted site, the error_log.txt does not get updated … i have even given the permission 777 to the file even. So what do i do?

  • David

    Thanks

  • Clement Sam

    Thanks a lot. It works perfectly on my xampp server.