5

i want to create the error logs but not to display it in browser.

Currently when an error or warning/notice is occurs, then logs is created and error is displayed to the browser but i want to force the system to not show the error message in browser to the site visitor but create the logs for me.

Thanks

2
  • You need this: php.net/manual/en/… Commented Apr 24, 2013 at 6:32
  • to using the ini_set('display_errors','Off'); method will do it. Commented Apr 24, 2013 at 6:34

5 Answers 5

7

Add this to the top of your script:

//don't display errors
ini_set('display_errors', 0);
//write errors to log
ini_set('log_errors', 1);
//error log file name
ini_set('error_log', '/var/log/php/error.log');

error_reporting(E_ALL);
Sign up to request clarification or add additional context in comments.

Comments

6

Make sure you have something like this in your php.ini:

display_errors = Off
error_log = /var/log/php/error.log # Assuming you have /var/log/php directory and it's writable by httpd
error_reporting = E_ALL & ~E_DEPRECATED

Or set them as run-time options with ini_set()

1 Comment

I uses ini_set() but still cannot logs the errors without showing it on browser. I also cannot log the error to other custom file
0

May be try

error_reporting(E_ERROR | E_PARSE);

Comments

0

If you want log custom errors:

try {
    // your code here
} catch (Exception $e){
    error_log($e->getMessage(), $e->getCode(), $e->getFile().' Line '.$e->getLine());
}

else use user4035 way.

Comments

0

In case someone is looking for how to do this in 2023 onwards, this is a quick note to say that all the ini_set() answers will only work in runtime from the time they are executed onwards.

This means that errors generated during file parse may still be sent to the browser, and of course those are probably the most common errors.

The usual fix for this varies widely depending on your version of PHP and how it is called; but it's always a variation of a file containing php settings; for instance, a php.ini. You could add this fragment to such a php.ini file:

display_errors = Off
log_errors = On

This would suppress browser errors while enabling logging to a file. You may also need to add the line error_log = error_log

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.