• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
  • This forum has been archived. New threads and replies may not be made. All add-ons/resources that are active should be migrated to the Resource Manager. See this thread for more information.

Ragtek Logger

R

ragtek

Guest
#1
I've created a Logger Class, to create Logentries while i'm developing Add-ons.
It's a nice, little helper

PHP:
<?php

/**
 * Ragtek Logger, creates a logfile which can be used to log messages,
 * and other "important stuff, it's nice for debugging;)
 *
 * @copyright ragtek
 * @version 1.0.0
 * @package Ragtek/Helper
 */
class Ragtek_Helper_Log
{
    /**
     * @var Zend_Log
     */
    private $logger = null;

    /**
     * @var Zend_Log_Writer_Stream
     */
    private $writer = null;
    
    static private $instance = null;

   public static function getInstance()
    {
        if (!self::$instance)
        {
            self::$instance = new self;
        }
        return self::$instance;
    }

    public function __construct($path = '')
    {
        if ($path == '')
        {
            $path = XenForo_Autoloader::getInstance()->getRootDir() . '\Ragtek\log.txt';
        }
        
        $this->writer = new Zend_Log_Writer_Stream($path);
        $this->logger = new Zend_Log($this->writer);
    }


    public function log($message, $priority = Zend_Log::INFO)
    {
        $this->logger->log($message, $priority);
    }
}
Usage:
PHP:
 Ragtek_Helper_Log::getInstance()->log('created contact thread');
or direct in the add-on i'm using this, to make sure the users have no problems, if they don't have the class
PHP:
                    if (XenForo_Application::debugMode() AND class_exists('Ragtek_Helper_Log'))
                    {
                            Ragtek_Helper_Log::getInstance()->log('created contact thread');
                    }
It's based on the Zend Logger, so you can also use FireBug or Zend Monitor and other providen methods.
For more infos check http://framework.zend.com/manual/de/zend.log.writers.html
 

compwhizii

Active member
#4
or direct in the add-on i'm using this, to make sure the users have no problems, if they don't have the class
PHP:
                    if (XenForo_Application::debugMode() AND class_exists('Ragtek_Helper_Log'))
                    {
                            Ragtek_Helper_Log::getInstance()->log('created contact thread');
                    }
Wouldn't it be easier to check if the class exists on addon load, and if it doesn't or it's in no debug mode, make a fake one that does nothing so I don't have to have an if statement every time I want to log something?
 
R

ragtek

Guest
#5
Wouldn't it be easier to check if the class exists on addon load, and if it doesn't or it's in no debug mode, make a fake one that does nothing so I don't have to have an if statement every time I want to log something?
How do you mean this? I don't understand what you mean with addon load.
 
R

ragtek

Guest
#6
Just a quick note:

This isn't necessary anymore.
XenForo includes a own log feature:)

XenForo_Helper_File
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
     */
    public static function log($logName, $logEntry, $append = true)