How to change the way php errors are written to the error log file?

I have been working on my website for over a year now and I really want to finally release it for people. However, it has become quite large - I almost want to say out of my control, and besides, I'm really just an amateur programmer.

Therefore, I want to be sure that any errors that php produces are logged in the file, so I can access this file and track errors.

Currently my settings are as follows:

<?php error_reporting(E_ALL); ini_set('display_errors', '0'); ini_set('log_errors', 1); ini_set('error_log', 'errors.log'); ?> 

Works fine so far, my error.log file will contain things like this:

[14-May-2013 00:16:26] PHP note: Undefined variable: nonexistentvariable in /home/www/dir/index.php on line 14 [May 14, 2013 00:16:28] PHP note: Undefined variable : nonexistentvariable in /home/www/dir/index.php on line 14

Great, errors are logged.

But now I have a problem:

  • They are all on the same line, without interruptions. Hard to read. How to get each error on a new line?

  • I see that there is a timestamp. Awesome! How can I add things like user IP or any other user things?

Again, my questions are:

How to change the way php errors are written to the error log file? In particular, how can I create a new line after each registered error, so that the error.log file is easier to read. And how can I add user data and values ​​like IP addresses?

ANSWER: I ended up doing the following - it seems to reproduce somewhat what php does by standard, and can be changed.

 <?php function my_error_handler($type, $message, $file, $line, $vars) { switch($type) { case 1: // 1 // $type_str = 'ERROR'; break; case 2: // 2 // $type_str = 'WARNING'; break; case 4: // 4 // $type_str = 'PARSE'; break; case 8: // 8 // $type_str = 'NOTICE'; break; case 16: // 16 // $type_str = 'CORE_ERROR'; break; case 32: // 32 // $type_str = 'CORE_WARNING'; break; case 64: // 64 // $type_str = 'COMPILE_ERROR'; break; case 128: // 128 // $type_str = 'COMPILE_WARNING'; break; case 256: // 256 // $type_str = 'USER_ERROR'; break; case 512: // 512 // $type_str = 'USER_WARNING'; break; case 1024: // 1024 // $type_str = 'USER_NOTICE'; break; case 2048: // 2048 // $type_str = 'STRICT'; break; case 4096: // 4096 // $type_str = 'RECOVERABLE_ERROR'; break; case 8192: // 8192 // $type_str = 'DEPRECATED'; break; case 16384: // 16384 // $type_str = 'USER_DEPRECATED'; break; } $errormessage = '[ '.date(r).' ] '.$type_str.': '.$message.' in '.$file.' on line '.$line."\n"; // for development simply ECHO $errormessage; $file = 'my_errors.log'; file_put_contents($file, $errormessage, FILE_APPEND); } error_reporting(E_ALL); ini_set('display_errors', '0'); set_error_handler('my_error_handler'); ?> 
+4
source share
1 answer

You cannot perform any additional configuration or implement any of this logic with a built-in error handler; you will need to set your own error handler using set_error_handler() .

This function performs a callback that will be called when PHP encounters an error, and you can act accordingly.

See the example in the docs for how to use this function and how to implement the correct callback.

+3
source

Source: https://habr.com/ru/post/1480599/


All Articles