Logging from Add-ons

Garani

Active member
I would like to log some results from the run of my add on. I have noticed that Xenforo has a log of sorts and I would like to take advantage of it.

Any hints on where to get some code examples?

Thanks a lot!

ivan
 
Code:
XenForo_Helper_File::log(string, string, boolean);

An example can be found in this file:
/library/XenForo/SabreDav/ErrorHandler.php

Code:
XenForo_Helper_File::log(
	'webdav-error',
	sprintf("%s:\n\t%s\n\t%s", $dataType, $dw->get('title'), $errorString),
	false
);

This logs all the errors to the log file: {internal_data}/webdav-error.log
The third parameter "false" tells it to overwrite the file for each logged message, instead of appending it.
 
Ok, I am sure that once the 1.0 comes out all this will be properly documented, but in the meanwhile I may just do some reverse engeneering of sorts.

You seems to have 3 different Error reporting tool:

1) $this->responseError()
PHP:
    /**
    * Controller response for when you want to throw an error and display it to the user.
    *
    * @param string|array  Error text to be use
    * @param integer An optional HTTP response code to output
    * @param array   Key-value pairs of parameters to pass to the container view
    *
    * @return XenForo_ControllerResponse_Error
    */

2) XenForo_Helper_File::log()
PHP:
    /**
     * Method for writing out a file log.
     *
     * @param string $logName 'foo' will write to {internalDataPath}/foo.log
     * @param string $logEntry The string to write into the log. Line break not required.
     * @param boolean $append Append the log entry to the end of the existing log. Otherwise, start again.
     *
     * @return boolean True on successful log write
     */


3) $this->_params['exception']
This method catches automatically server error 500

Given that the third, even if lovely, really works with thrown exceptions, we seems to be just left with option 1 to talk to the user, and option 2 to write down a meaninful log for the admin/developer.

I'll do some testing and report back.

ivan
 
The server error log, I believe, is reserved for logging of uncaught exceptions generated at the application level. Although you could insert records into the log table (xf_error_log) yourself, the log format is very specific and you may not have the relevant info required to be inserted.

So yes, if you want to show the error to the end-user, return an Error response.
Otherwise use the file helper to log the message.
 
I now have to get the parameter's for responseError/responseMessage. If I try a
PHP:
return $this->responseError('My Personal Message', 200, array('message' => $aBufferOfText));

I do get a correct error window with the My Personal Message string, but I don't get the further buffer. True, I can just concatename the buffer to the actual message string, but if there was a "cleaner" method of doing it, then would have been lovely.
 
Third parameter to responseError is the container parameter.
It's not used when rendering the error itself.

Any message that you want to show to the user needs to be passed as the first param.

PHP:
return $this->responseError(new XenForo_Phrase('your_exact_error_phrase'));
 
Well, things are getting pretty interesting. Loking at the error.html template there is the following code:

HTML:
    <xen:if is="{xen:count $error, false} == 1">

        <xen:if is="{$showHeading}"><h2 class="heading">{xen:phrase following_error_occurred}:</h2></xen:if>

        <div class="baseHtml">
        <xen:foreach loop="$error" key="$key" value="$value">
            <label for="ctrl_{$key}" class="OverlayCloser">{xen:raw $value}</label>
        </xen:foreach>
        </div>
    <xen:else />
        <xen:if is="{$showHeading}"><h2 class="heading">{xen:phrase please_correct_following_errors}:</h2></xen:if>

        <div class="baseHtml">
            <ul>
            <xen:foreach loop="$error" key="$key" value="$value">
                <li><label for="ctrl_{$key}" class="OverlayCloser">{xen:raw $value}</label></li>
            </xen:foreach>
            </ul>
        </div>
    </xen:if>

So it seems to imply that you can send off more then one error in one window.

And in which css do I activate the scrollbar? This is getting trublesome ;)
 
So it seems to imply that you can send off more then one error in one window.
Yep. Useful in those cases when multiple validation processes have failed. For example, in a registration form.

Code:
$errors = array(
    new XenForo_Phrase('error_phrase1'),
    new XenForo_Phrase('error_phrase2'),
    new XenForo_Phrase('error_phraseN'),
);
return $this->responseError($errors);


And in which css do I activate the scrollbar?
:confused:
 
Ah! There you go. So you can pass a text of a xenphrase object or an array containing test and /or objects.

This actually solves my problem and I can simply thrw a single error for multiple submissions issues.

Regarding the scrollbar, I am (improperly) using the output for variable dumping. The ajax error window doesn't seem to display a scrollbar, so you can read the content 'till the end of the screen and that's all.
 
Top Bottom