Changeset 1118

Show
Ignore:
Timestamp:
12/06/07 13:33:57 (1 year ago)
Author:
mikey
Message:

log error handler does not use logging component any more, should be more reliable now

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/lang/errorhandler/stubLogErrorHandler.php

    r1092 r1118  
    77 * @subpackage  lang_errorhandler 
    88 */ 
    9 stubClassLoader::load('net.stubbles.lang.errorhandler.stubErrorHandler', 
    10                       'net.stubbles.util.log.log' 
    11 ); 
     9stubClassLoader::load('net.stubbles.lang.errorhandler.stubErrorHandler'); 
    1210/** 
    1311 * Error handler that logs all errors. 
     
    4846     * @var  string 
    4947     */ 
    50     protected $logTarget     = 'php-error'; 
     48    protected $logTarget = 'php-error'; 
    5149    /** 
    52      * the error level to use for the log data 
     50     * directory to log errors into 
     51     * 
     52     * @var  string 
     53     */ 
     54    protected $logDir; 
     55    /** 
     56     * mode for new directories 
    5357     * 
    5458     * @var  int 
    5559     */ 
    56     protected $logLevel      = stubLogger::LEVEL_ERROR; 
     60    protected $mode      = 0700; 
     61 
     62    /** 
     63     * constructor 
     64     */ 
     65    public function __construct() 
     66    { 
     67        $this->logDir = stubConfig::getLogPath() . DIRECTORY_SEPARATOR . 'errors' . DIRECTORY_SEPARATOR . '{Y}' . DIRECTORY_SEPARATOR . '{M}'; 
     68    } 
    5769 
    5870    /** 
     
    6779 
    6880    /** 
    69      * sets the level of the log data 
     81     * sets the directory to log into 
    7082     * 
    71      * @param  int  $logLevel 
     83     * @param  string  $logDir 
    7284     */ 
    73     public function setLogLevel($logLevel
     85    public function setLogDirectory($logDir
    7486    { 
    75         $this->logLevel = $logLevel; 
     87        $this->logDir = $logDir; 
     88    } 
     89 
     90    /** 
     91     * sets the mode for new log directories 
     92     * 
     93     * @param  int  $mode 
     94     */ 
     95    public function setMode($mode) 
     96    { 
     97        $this->mode = $mode; 
    7698    } 
    7799 
     
    123145    public function handle($level, $message, $file = null, $line = null, array $context = array()) 
    124146    { 
    125         $logData = stubLogDataFactory::create($this->logTarget, $this->logLevel); 
    126         $logData->addData($level); 
    127         $logData->addData((isset(self::$levelStrings[$level]) === true) ? (self::$levelStrings[$level]) : ('unknown')); 
    128         $logData->addData($message); 
    129         $logData->addData($file); 
    130         $logData->addData($line); 
    131         stubLogger::logToAll($logData); 
     147        $logData  = date('Y-m-d H:i:s') . '|' . $level . '|'; 
     148        $logData .= '|' . (isset(self::$levelStrings[$level]) === true) ? (self::$levelStrings[$level]) : ('unknown'); 
     149        $logData .= '|' . $message; 
     150        $logData .= '|' . $file; 
     151        $logData .= '|' . $line; 
     152        $logDir   = $this->buildLogDir(); 
     153        if (file_exists($logDir) == false) { 
     154            mkdir($logDir, $this->mode, true); 
     155        } 
     156         
     157        error_log($logData . "\n", 3, $logDir . DIRECTORY_SEPARATOR . $this->logTarget . '-' . date('Y-m-d') . '.log'); 
    132158        return true; 
    133159    } 
     160 
     161    /** 
     162     * builds the log directory 
     163     * 
     164     * @return  string 
     165     */ 
     166    protected function buildLogDir() 
     167    { 
     168        return str_replace('{Y}', date('Y'), str_replace('{M}', date('m'), $this->logDir)); 
     169    } 
     170 
    134171} 
    135172?> 
  • trunk/src/test/php/net/stubbles/lang/errorhandler/stubLogErrorHandlerTestCase.php

    r1092 r1118  
    77 * @subpackage  lang_errorhandler_test 
    88 */ 
    9 stubClassLoader::load('net.stubbles.lang.errorhandler.stubLogErrorHandler', 
    10                       'net.stubbles.util.log.stubMemoryLogAppender' 
    11 ); 
     9stubClassLoader::load('net.stubbles.lang.errorhandler.stubLogErrorHandler'); 
    1210Mock::generate('stubSession'); 
    1311 
     
    3331    { 
    3432        $this->logErrorHandler = new stubLogErrorHandler(); 
    35         stubRegistry::setConfig('net.stubbles.util.log.class', 'net.stubbles.util.log.stubBaseLogData'); 
    36         $binder = new stubBinder(); 
    37         stubRegistry::set(stubBinder::REGISTRY_KEY, $binder); 
    38     } 
    39  
    40     /** 
    41      * clean up test environment 
    42      */ 
    43     public function tearDown() 
    44     { 
    45         $loggerIds = stubLogger::getInstanceList(); 
    46         foreach ($loggerIds as $loggerId) { 
    47             stubLogger::destroyInstance($loggerId); 
    48         } 
    49          
    50         stubRegistry::remove(stubBinder::REGISTRY_KEY); 
    51         stubRegistry::removeConfig('net.stubbles.util.log.class'); 
    5233    } 
    5334 
     
    6748        $this->assertFalse($this->logErrorHandler->isSupressable(E_NOTICE, 'foo')); 
    6849    } 
    69  
    70     /** 
    71      * assure that the error is logged 
    72      */ 
    73     public function testHandle() 
    74     { 
    75         $logger      = stubLogger::getInstance(__CLASS__); 
    76         $logAppender = new stubMemoryLogAppender(); 
    77         $logger->addLogAppender($logAppender); 
    78         $this->logErrorHandler->handle(E_USER_ERROR, 'foo', 'bar', 9, array('blub' => 'baz')); 
    79         $logData = $logAppender->getLogData(); 
    80         $this->assertEqual(count($logData), 1); 
    81         $this->assertEqual(count($logData['php-error']), 1); 
    82         $this->assertEqual($logData['php-error'][0]->getTarget(), 'php-error'); 
    83         $this->assertEqual($logData['php-error'][0]->getLevel(), stubLogger::LEVEL_ERROR); 
    84         $logDataContents = explode(stubLogData::SEPERATOR, $logData['php-error'][0]->get()); 
    85         $this->assertEqual($logDataContents[1], E_USER_ERROR); 
    86         $this->assertEqual($logDataContents[2], 'E_USER_ERROR'); 
    87         $this->assertEqual($logDataContents[3], 'foo'); 
    88         $this->assertEqual($logDataContents[4], 'bar'); 
    89         $this->assertEqual($logDataContents[5], 9); 
    90         stubLogger::destroyInstance(__CLASS__); 
    91     } 
    9250} 
    9351?>