errors - error_log is not working as expected in functions.php file

admin2025-01-07  3

Error log on my index file get logged in the error_log.txt file but the same error log is not working in the functions.php file. I am using the latest WordPress and PHP 7.0. I am not using any plugin as well.

error_log("Hello there is an error");

Error log on my index file get logged in the error_log.txt file but the same error log is not working in the functions.php file. I am using the latest WordPress and PHP 7.0. I am not using any plugin as well.

error_log("Hello there is an error");
Share Improve this question edited Mar 8, 2019 at 14:55 Gufran Hasan 6857 silver badges20 bronze badges asked Mar 8, 2019 at 12:59 Amit PandeyAmit Pandey 411 silver badge2 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 6

I had a similar problem: calling error_log() from wp-config.php works, but calling it from functions.php does not work for me.

I couldn't solve this issue but I've found a workaround that allowed me to do some sort of debugging at least. I gave up on error_log() and just wrote an own function that logs into a given file:

function mylog($txt) {
    file_put_contents('/home/myuser/logs/mylog.txt', $txt.PHP_EOL , FILE_APPEND | LOCK_EX);
}

If you have this set in your wp-config.php:

define( 'WP_DEBUG_LOG', true );

it will save the logs to wp-content/debug.log file instead of the default Apache error.log.

WP_DEBUG_LOG may also be set to a specific path on the filesystem, in which case the log will be written there.

See the WP_DEBUG_LOG Wordpress documentation.

In order for error_log() to do anything, you do need to have the WP_DEBUG and WP_DEBUG_LOG constants set to true in your wp-config.php. (See: Debugging in WordPress in the Codex for more information.)

According to this answer on Stackoverflow, you should also set WP_DEBUG_DISPLAY for error_log() to function. Note that you can set it to true OR false, but it evidently needs to be defined.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736263769a916.html

最新回复(0)