Changeset 146

Show
Ignore:
Timestamp:
01/24/07 17:03:47 (1 year ago)
Author:
mikey
Message:

finished net.stubbles.util.log

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/util/log/stubFileLogAppender.php

    r143 r146  
    11<?php 
     2/** 
     3 * A log appenders that writes log data to files. 
     4 * 
     5 * @author      Frank Kleine <mikey@stubbles.net> 
     6 * @package     stubbles 
     7 * @subpackage  util_log 
     8 */ 
     9stubClassLoader::load('net.stubbles.util.log.stubLogAppender', 
     10                      'net.stubbles.util.log.stubLogData' 
     11); 
     12/** 
     13 * A log appenders that writes log data to files. 
     14 *  
     15 * This log appender writes the log data into a logfile using the error_log()  
     16 * function of PHP. The logfile name will be [target]-[Y-m-d].log where target  
     17 * is the return value of stubLogData::getTarget(). 
     18 * 
     19 * @package     stubbles 
     20 * @subpackage  util_log 
     21 * @uses        http://php.net/error_log 
     22 */ 
    223class stubFileLogAppender extends stubBaseObject implements stubLogAppender 
    324{ 
     25    /** 
     26     * the directory to write the logfiles into 
     27     * 
     28     * @var  string 
     29     */ 
     30    protected $logDir = ''; 
     31     
     32    /** 
     33     * constructor 
     34     *  
     35     * The log director may have placeholders: {Y} will be replaced with the  
     36     * current year, {M} will be replaced with the current month, e.g. 
     37     * /path/to/logs/{Y}/{M} would become /path/to/logs/2007/01. 
     38     * 
     39     * @param  string  $logDir  the directory to write the logfiles into 
     40     */ 
     41    public function __construct($logDir) 
     42    { 
     43        $this->logDir = $logDir; 
     44    } 
     45     
     46    /** 
     47     * builds the log directory 
     48     * 
     49     * @return  string 
     50     */ 
     51    protected function buildLogDir() 
     52    { 
     53        return str_replace('{Y}', date('Y'), str_replace('{M}', date('m'), $this->logDir)); 
     54    } 
     55     
     56    /** 
     57     * append the log data to the log target 
     58     *  
     59     * The basename of the logfile will be [target]-[Y-m-d].log where target  
     60     * is the return value of stubLogData::getTarget(). 
     61     * 
     62     * @param  stubLogData  $logData 
     63     */ 
    464    public function append(stubLogData $logData) 
    565    { 
    6         $logFile = $logData->getTarget() . '-' . date('Y-m-d') . '.log'; 
    7         error_log($logData->get(), 3, $logFile); 
     66        $logDir  = $this->buildLogDir(); 
     67        if (file_exists($logDir) == false) { 
     68            mkdir($logDir, 0700, true); 
     69        } 
     70         
     71        error_log($logData->get() . "\n", 3, $logDir . '/' . $logData->getTarget() . '-' . date('Y-m-d') . '.log'); 
    872    } 
    973     
     74    /** 
     75     * finalize the log target 
     76     */ 
    1077    public function finalize() 
    1178    { 
  • trunk/src/main/php/net/stubbles/util/log/stubLogAppender.php

    r143 r146  
    11<?php 
     2/** 
     3 * Interface for log appenders. 
     4 * 
     5 * @author      Frank Kleine <mikey@stubbles.net> 
     6 * @package     stubbles 
     7 * @subpackage  util_log 
     8 */ 
     9/** 
     10 * Interface for log appenders. 
     11 *  
     12 * A log appender takes log data and writes it to the target. The target can be  
     13 * a file, a database or anything else. 
     14 * 
     15 * @package     stubbles 
     16 * @subpackage  util_log 
     17 */ 
    218interface stubLogAppender 
    319{ 
     20    /** 
     21     * append the log data to the log target 
     22     * 
     23     * @param  stubLogData  $logData 
     24     */ 
    425    public function append(stubLogData $logData); 
    526     
     27    /** 
     28     * finalize the log target 
     29     *  
     30     * This will be called in case a logger is destroyed and can be used 
     31     * to close file or database handlers or to write the log data if  
     32     * append() just collects the data. 
     33     */ 
    634    public function finalize(); 
    735} 
  • trunk/src/main/php/net/stubbles/util/log/stubLogData.php

    r143 r146  
    11<?php 
     2/** 
     3 * Interface for log data. 
     4 * 
     5 * @author      Frank Kleine <mikey@stubbles.net> 
     6 * @package     stubbles 
     7 * @subpackage  util_log 
     8 */ 
     9/** 
     10 * Interface for log data. 
     11 *  
     12 * The log data object is used to collect the data to log. 
     13 * 
     14 * @package     stubbles 
     15 * @subpackage  util_log 
     16 */ 
    217interface stubLogData 
    318{ 
     19    /** 
     20     * default seperator to be used to seperate the log fields 
     21     */ 
     22    const SEPERATOR = '|'; 
     23     
     24    /** 
     25     * adds data to the log object 
     26     *  
     27     * Each call to this method will add a new field. 
     28     * 
     29     * @param  string  $data 
     30     */ 
    431    public function addData($data); 
    532     
     33    /** 
     34     * returns the whole log data as one line 
     35     * 
     36     * @return  string 
     37     */ 
    638    public function get(); 
    739     
     40    /** 
     41     * returns the level of the log data 
     42     *  
     43     * @return  int 
     44     * @see     stubLogger::LEVEL_* 
     45     */ 
    846    public function getLevel(); 
    947     
     48    /** 
     49     * returns the target where the log data should go to 
     50     *  
     51     * How the target is interpreted depends on the log appender which 
     52     * takes the log data. A file log appender might use this as the basename  
     53     * of a file, while a database log appender might use this as the name  
     54     * of the table to write the log data into. Therefore it is advisable to  
     55     * only use ascii characters, numbers and underscores to be sure that the  
     56     * log appender will not mess up the log data. 
     57     *  
     58     * @return  string 
     59     */ 
    1060    public function getTarget(); 
    1161} 
  • trunk/src/main/php/net/stubbles/util/log/stubLogger.php

    r143 r146  
    11<?php 
    2 class stubLogger 
     2/** 
     3 * Class for logging. 
     4 * 
     5 * @author      Frank Kleine <mikey@stubbles.net> 
     6 * @package     stubbles 
     7 * @subpackage  util_log 
     8 */ 
     9stubClassLoader::load('net.stubbles.util.log.stubLogAppender', 
     10                      'net.stubbles.util.log.stubLogData' 
     11); 
     12/** 
     13 * Class for logging. 
     14 *  
     15 * The logger is the interface to log data into differant targets. The logger  
     16 * itself does not know where to write the log data - it just uses log appenders  
     17 * which in turn do the real work. A logger is a collection of such appenders. 
     18 * Even if it looks like a singleton it is possible to have differant instances 
     19 * of a logger. Each logger uses a differant id and can be applied on differant 
     20 * logging levels. For instance, you may have one logger that is applicable for 
     21 * debug logging and another one that is used for the other error levels. 
     22 * 
     23 * @package     stubbles 
     24 * @subpackage  util_log 
     25 */ 
     26class stubLogger extends stubBaseObject 
    327{ 
    428    /** 
     
    2751    const LEVEL_ERROR           = 8; 
    2852    /** 
     53     * log level: all 
     54     */ 
     55    const LEVEL_ALL             = 15; 
     56    /** 
    2957     * list of available logger instances 
    3058     * 
     
    5179    protected $logAppender      = array(); 
    5280     
    53     public function __static() 
    54     { 
    55         # create all loggers as defined in configuration 
    56     } 
    57      
    58     protected final function __construct($id, $level) 
     81    /** 
     82     * constructor 
     83     * 
     84     * If no level is specified the logger will be applicable for all log levels. 
     85     *  
     86     * @param  string  $id     id of the logger 
     87     * @param  int     $level  optional  level where the logger is applicable for 
     88     */ 
     89    protected final function __construct($id, $level = self::LEVEL_ALL) 
    5990    { 
    6091        $this->id    = $id; 
    61         if (null !== $level) { 
    62             $this->level = $level; 
    63         } else { 
    64             $this->level = self::LEVEL_DEBUG + self::LEVEL_INFO + self::LEVEL_WARN + self::LEVEL_ERROR; 
    65         } 
    66     } 
    67      
    68     protected final function __destruct() 
     92        $this->level = $level; 
     93    } 
     94     
     95    /** 
     96     * destructor 
     97     *  
     98     * Calls all log appenders that they should finalize their work. 
     99     */ 
     100    public final function __destruct() 
    69101    { 
    70102        foreach ($this->logAppender as $logAppender) { 
     
    73105    } 
    74106     
    75     public static function getInstance($id = self::DEFAULT_ID, $level = null) 
     107    /** 
     108     * returns a logger instance 
     109     *  
     110     * If the logger with the given id already exists this will be returned, 
     111     * else a new logger will be created using the given level and returned. 
     112     * If no level is specified and a new logger is created it will be 
     113     * applicable for all levels. 
     114     * 
     115     * @param   string      $id     id of the logger 
     116     * @param   int         $level  optional  level where the logger is applicable for 
     117     * @return  stubLogger 
     118     */ 
     119    public static function getInstance($id = self::DEFAULT_ID, $level = self::LEVEL_ALL) 
    76120    { 
    77121        if (isset(self::$instances[$id]) == false) { 
     
    82126    } 
    83127     
     128    /** 
     129     * destroys the logger with the given id 
     130     * 
     131     * @param  string  $id  id of the logger 
     132     */ 
    84133    public static function destroyInstance($id) 
    85134    { 
     
    89138    } 
    90139     
     140    /** 
     141     * clone is not supported 
     142     */ 
    91143    protected final function __clone() 
    92144    { 
     
    94146    } 
    95147     
     148    /** 
     149     * returns the id of the logger 
     150     * 
     151     * @return  string 
     152     */ 
    96153    public function getId() 
    97154    { 
     
    99156    } 
    100157     
     158    /** 
     159     * returns the level where the logger is applicable for 
     160     * 
     161     * @return  int 
     162     */ 
    101163    public function getLevel() 
    102164    { 
     
    104166    } 
    105167     
     168    /** 
     169     * checks whether the logger is applicable for the given level 
     170     * 
     171     * @param   int   $level 
     172     * @return  bool 
     173     */ 
    106174    public function isApplicable($level) 
    107175    { 
     176        if (self::LEVEL_ALL == $level && self::LEVEL_ALL > $this->level) { 
     177            return false; 
     178        } 
     179         
    108180        return (($this->level & $level) != 0); 
    109181    } 
    110182     
     183    /** 
     184     * adds a log appender to the logger 
     185     *  
     186     * A log appender is responsible for writing the log data. 
     187     * 
     188     * @param  stubLogAppender  $logAppender 
     189     */ 
    111190    public function addLogAppender(stubLogAppender $logAppender) 
    112191    { 
     
    114193    } 
    115194     
     195    /** 
     196     * log data to all loggers created 
     197     * 
     198     * @param  stubLogData  $logData  contains the data to log 
     199     */ 
    116200    public static function logToAll(stubLogData $logData) 
    117201    { 
    118202        foreach (self::$instances as $logger) { 
    119             if ($logger->isApplicable($logData->getLevel()) == false) { 
     203            if (null === $logger || $logger->isApplicable($logData->getLevel()) == false) { 
    120204                continue; 
    121205            } 
     
    125209    } 
    126210     
     211    /** 
     212     * sends log data to all registered log appenders 
     213     * 
     214     * @param  stubLogData  $logData  contains the data to log 
     215     */ 
    127216    public function log(stubLogData $logData) 
    128217    { 
  • trunk/src/test/run.php

    r121 r146  
    3333        $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/helper/HelperTestSuite.php'); 
    3434        $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/reflection/ReflectionTestSuite.php'); 
     35        $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/util/UtilTestSuite.php'); 
    3536        #$testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/websites/variantmanager/VariantManagerTestSuite.php'); 
    3637        $testSuite->addTestFile(TEST_SRC_PATH . '/php/net/stubbles/xml/XMLTestSuite.php');