1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Writing to Server Error Log

Discussion in 'XenForo Development Discussions' started by AndyB, Oct 3, 2014.

  1. AndyB

    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.
     
  2. Nobita.Kun

    Nobita.Kun Well-Known Member

    Maybe is good:
    PHP:
    if ($failed)
    {
    XenForo_Error::logError("your_message_here");
    }
     
    Thomas.B and AndyB like this.
  3. AndyB

    AndyB Well-Known Member

    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.
     
  4. SneakyDave

    SneakyDave Well-Known Member

    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: Oct 3, 2014
  5. AndyB

    AndyB Well-Known Member

    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.
     
  6. Nobita.Kun

    Nobita.Kun Well-Known Member

    XenForo_Error::logException its didn't require debug mode. :D
     
  7. AndyB

    AndyB Well-Known Member

    Hi Nobita.Kun,

    Unfortunately using XenForo_Error::logException gives this error:

    pic001.jpg
     
  8. Nobita.Kun

    Nobita.Kun Well-Known Member

    Try with this code:

    PHP:
    XenForo_Error::logException(
    new 
    XenForo_Exception("your_message")
    );
     
    wasif, Thomas.B and 0ptima like this.
  9. AndyB

    AndyB Well-Known Member

    Hi Nobita.Kun,

    You're a genius! That works perfect.

    PHP:
    XenForo_Error::logException(new XenForo_Exception("your_message"));
    Thank you.
     
    CrispinP and Nobita.Kun like this.

Share This Page