MyBB Community Forums

Full Version: error log timestamp in human readable form
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Because PHP 8 elevated many Notices to Warnings and it is desirable to improve the codebase to eliminate warnings and even with 1833 the error log now gets filled with such things like
<error>
	<dateline>1676818777</dateline>
	<script>search.php</script>
	<line>476</line>
	<type>2</type>
	<friendly_type>Warning</friendly_type>
	<message>Undefined array key "lastread"</message>
	<back_trace>#0  errorHandler->error() called at [/inc/class_error.php:153]
#1  errorHandler->error_callback() called at [/search.php:476]
</back_trace>
</error>

Rather than try to manually create warnings, I am logging them on my production forum, thereby letting my members travel the forum doing what they normally do. It is useful to read the error log without having to convert unix epoch time to help locate the event that caused the warning.

A core edit will add an extra human-readable line to the error log message.
The error log entry is defined in lines 375 through 383 of inc/class_error.php
Change
		$error_data = "<error>\n";
		$error_data .= "\t<dateline>".TIME_NOW."</dateline>\n";
		$error_data .= "\t<script>".$file."</script>\n";
		$error_data .= "\t<line>".$line."</line>\n";
		$error_data .= "\t<type>".$type."</type>\n";
		$error_data .= "\t<friendly_type>".$this->error_types[$type]."</friendly_type>\n";
		$error_data .= "\t<message>".$message."</message>\n";
		$error_data .= $back_trace;
		$error_data .= "</error>\n\n";
to
		$dl = TIME_NOW;
		$dt = date('Y-m-d H:i:s T', $dl);

		$error_data = "<error>\n";
		$error_data .= "\t<dateline>".$dl."</dateline>\n";
		$error_data .= "\t<datetime>".$dt."</datetime>\n";
		$error_data .= "\t<script>".$file."</script>\n";
		$error_data .= "\t<line>".$line."</line>\n";
		$error_data .= "\t<type>".$type."</type>\n";
		$error_data .= "\t<friendly_type>".$this->error_types[$type]."</friendly_type>\n";
		$error_data .= "\t<message>".$message."</message>\n";
		$error_data .= $back_trace;
		$error_data .= "</error>\n\n";

Essentially, add two variable definitions, change the dateline data and add an additional datetime line to $error_data.

Now the above error message is easier to read with the added line.
<datetime>2023-02-19 14:59:37 UTC</datetime>
In addition, if those log files are not otherwise restricted when going to forum/error.log, you can restrict all access except from your IP address. I am generating debug files ending with .txt which I also want to restrict access to.

Add
<FilesMatch "\.(txt|log)$">
Order allow,deny
allow from 69.xxx.xxx.xxx
</FilesMatch>
to your .htaccess file.

If your log files display, then you're good to go. If they download instead, try adding
<IfModule mod_mime.c>
    AddType text/plain .log
</IfModule>

This will help to provide easy access to your log files.