Writing to Server Error Log

AndyB

Well-known member
In my add-on I would like to add a line of code in the PHP file that would write an error message to the Server Error Log, is there any specific XenForo command I can use to do that? So for example the add-on finds a problem and would only write to the Server Error Log, no message is shown to the member at the front end.
 
Hi Nobita.Kun,

That works great! Thank you very much.

The only downside is the forum has to have debug enabled to see the message in the Server Error Log.
 
There is a XenForo log file helper (Xenforo/Helper/File/log), but the file path/name isn't customizable, IIRC. It has to be in "internal_data".

I've use my own debug method and option in my addons, and my own log file helper for it.

It looks a little like this, nothing too fancy, you just send it the contents you want to write, and the status, such as "INFO", "ERROR", or whatever.

$this->debug is a flag indicating if a debug option is enabled.
$this->path is the absolute location to the log file.

If this is a batch type operation, then you can just use the 'a' to append to the log file on fopen(), or if you want to interrogate the file each time for an error, you could use "w" to rewrite the file each time.
Code:
public function write($logEntry, $status = "INFO")
{
    if( !$this->debug )
    {
        return true;
    }

    if ($fp = @fopen($this->path, 'a'))
    {
        fwrite($fp, date('Y-m-d H:i:s') . ' | ' . $status . ' | ' . $logEntry . "\n");
        fclose($fp);

        return true;
    }

    return false;
}

}

Then if if you've instantiated a $log object, I just do:
$log->write("This is the log entry");

or
$log->write("Woah, something just got manhandled", "ERROR");
 
Last edited:
Hi Dave,

Thank you for sharing. But it looks like this also requires debug mode to be enabled.

The reason for my request is to allow admins to easily see what error is causing my add-on to not function. The Server Error Log is ideal for this assuming I don't want to throw up an error in the front end. Unfortunately this doesn't appear possible unless debug mode is enabled.
 
Hi Dave,

Thank you for sharing. But it looks like this also requires debug mode to be enabled.

The reason for my request is to allow admins to easily see what error is causing my add-on to not function. The Server Error Log is ideal for this assuming I don't want to throw up an error in the front end. Unfortunately this doesn't appear possible unless debug mode is enabled.
XenForo_Error::logException its didn't require debug mode. :D
 
Back
Top Bottom