1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. 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

Discussion in 'Development Tutorials [Archive]' started by ragtek, Oct 27, 2010.

  1. ragtek

    ragtek Guest

    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
     
    dmnkhhn likes this.
  2. KozmoK

    KozmoK Active Member

    Sorry for the Noob Question, but where do I place this php class file, so I can call it from my Addon?
     
  3. ragtek

    ragtek Guest

    Ragtek_Helper_Log => library/ragtek/helper/log.php
     
  4. compwhizii

    compwhizii Active Member

    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?
     
  5. ragtek

    ragtek Guest

    How do you mean this? I don't understand what you mean with addon load.
     
  6. ragtek

    ragtek Guest

    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)
     

Share This Page