Change formatted date format in error_log

PHP 5.6

Is there a way to determine the format of the date shown when using error_log ?

I use it to log messages and errors from a specific script. It seems that set_error_handler will not handle all error messages .

In some examples, people add the date date directly to the error_log arguments. Using this approach would lead me to a duplicate date string in my log file.

I want to output the local time of the date in ISO format, but the date added to the CLI ( php -f script.php ) and Apache I get with

 ini_set('log_errors', 1); ini_set('error_log', 'file.log'); error_log('Test'); 

Something like that:

 [14-Feb-2016 18:07:31 America/Mexico_City] Test 

Noting that in most code examples, the date format usually does not include the time zone. And if I run it from the console, I do not get the added date:

 $ php -r 'error_log("Test");' Test 

The time zone is not set either at runtime or in php.ini, but correctly presented. I get this behavior in php5.6.18 package and compiled from php5.6.13 source. On the production server, the script is executed through the CLI, there is no apache installation.

Decision

It seems that the date displayed using error_log is not configurable. Starting with set_error_handler , as suggested by @ fusion3k, I put together this error handler that will format date, message and error levels with a more computer-friendly / human format

 // set_trigger_error only accepts E_USER_ constants values define('ERROR', 256); // Before E_USER_ERROR define('INFO', 512); // Before E_USER_WARNING define('DEBUG', 1024); // Before E_USER_NOTICE function errorHandler ($errType, $errStr, $errFile, $errLine, $errContext) { $displayErrors = ini_get( 'display_errors' ); $logErrors = ini_get( 'log_errors' ); $errorLog = ini_get( 'error_log' ); if( $displayErrors ) echo $errStr.PHP_EOL; if( $logErrors ) { $line = ""; switch ($errType) { case DEBUG: $type = 'DEBUG'; break; case INFO: $type = 'INFO'; break; case ERROR: $type = 'ERROR'; $line = " (Line:".$errLine.")"; break; default: $type = 'WARN'; $line = " (Line:".$errLine.")"; break; } $message = date('Ymd H:i:sO').";".$type.";".$errStr.$line; file_put_contents($errorLog, $message.PHP_EOL, FILE_APPEND); } } set_error_handler('errorHandler'); error_reporting(E_ALL); ini_set('log_errors', 1); ini_set('error_log', 'test.log'); trigger_error('Test', DEBUG); 

Exit

 2016-02-15 14:20:05-0600;DEBUG;Test 
+7
php error-logging
source share
1 answer

If you use an error log file, you can simply add your preferred date format for the log message.

If you use the standard Apache log file , you cannot change the log format via php.

Instead, you should change the entire format of the Apache error log in httpd.conf via the LogFormat keyword: replace the existing format with the desired format.

Here you can find the complete syntax guide for LogFormat .

Edit:

After your last comment, I looked over the Internet and I performed some tests, then I understand that my previous answer is incorrect: there is no way to change the behavior of error_log() in php. In my case (I have Apache installed), the error_log() ) function outputs the Apache log format even to the user log file (defined via ini_set('error_log') ).

In your case, I think php uses the OS log format (are you sure you don't have Apache installed, even if you don't use it?).

Alternative: trigger_error ()

You can set your fully custom error log format using trigger_error() - along with set_error_handler() - instead of error_log() :

 function myErrorHandler( $errType, $errStr, $errFile, $errLine, $errContext ) { $displayErrors = ini_get( 'display_errors' ); $logErrors = ini_get( 'log_errors' ); $errorLog = ini_get( 'error_log' ); if( $displayErrors ) echo $errStr.PHP_EOL; if( $logErrors ) { $message = sprintf( '[%s] %s (%s, %s)', date('dm H:i'), $errStr, $errFile, $errLine ); file_put_contents( $errorLog, $message.PHP_EOL, FILE_APPEND ); } } ini_set( 'log_errors', 1 ); ini_set( 'error_log', 'file.log' ); set_error_handler( 'myErrorHandler' ); trigger_error( 'Test' ); 

In the above example, trigger_error will add a line like this to the .log file:

 [15-02 09:43] Test (Script/File/Path.php, 12) 

The error_handler all system errors is handled by the user-defined function. To simulate a system error, you should use trigger_error() instead of error_log() .

To maximize tuning, you can go to trigger_error optional error_type parameter:

 trigger_error( 'Test', E_WARNING ); 

will result in a warning level error; The default value of error_type is E_USER_NOTICE . The type of error is the first parameter ( $errType ) in the user-defined function, therefore, inside the user-defined function of the error handler, you can change the behavior depending on the type of error.

In my previous simple example, a user-defined function also processes some ini directives, so you can use ini_set in combination with set_error_handler . Note that you can change the variable names of custom function parameters, but not the order , because this is determined by the php internal error system.

If you decide to use set_error_handler() , read the documentation carefully to understand the behavior and exceptions.


+4
source share

All Articles