Development

Changeset 7201

You must first sign up to be able to contribute.

Changeset 7201

Show
Ignore:
Timestamp:
01/28/08 15:52:55 (9 months ago)
Author:
fabien
Message:

made sfFileLogger more flexible (added format, time_format, and type options)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/lib/log/sfFileLogger.class.php

    r6767 r7201  
    2020{ 
    2121  protected 
    22     $fp = null; 
     22    $type       = 'symfony', 
     23    $format     = '%time% %type% [%priority%] %message%%EOL%', 
     24    $timeFormat = '%b %d %H:%M:%S', 
     25    $fp         = null; 
    2326 
    2427  /** 
     
    2730   * Available options: 
    2831   * 
    29    * - file:      The file path or a php wrapper to log messages 
    30    *              You can use any support php wrapper. To write logs to the Apache error log, use php://stderr 
    31    * - dir_mode:  The mode to use when creating a directory (default to 0777) 
    32    * - file_mode: The mode to use when creating a file (default to 0666) 
     32   * - file:        The file path or a php wrapper to log messages 
     33   *                You can use any support php wrapper. To write logs to the Apache error log, use php://stderr 
     34   * - format:      The log line format (default to %time% %type% [%priority%] %message%%EOL%) 
     35   * - time_format: The log time strftime format (default to %b %d %H:%M:%S) 
     36   * - dir_mode:    The mode to use when creating a directory (default to 0777) 
     37   * - file_mode:   The mode to use when creating a file (default to 0666) 
    3338   * 
    3439   * @param  sfEventDispatcher A sfEventDispatcher instance 
     
    4247    { 
    4348      throw new sfConfigurationException('You must provide a "file" parameter for this logger.'); 
     49    } 
     50 
     51    if (isset($options['format'])) 
     52    { 
     53      $this->format = $options['format']; 
     54    } 
     55 
     56    if (isset($options['time_format'])) 
     57    { 
     58      $this->timeFormat = $options['time_format']; 
     59    } 
     60 
     61    if (isset($options['type'])) 
     62    { 
     63      $this->type = $options['type']; 
    4464    } 
    4565 
     
    7494  { 
    7595    flock($this->fp, LOCK_EX); 
    76     fwrite($this->fp, sprintf("%s %s [%s] %s%s", strftime('%b %d %H:%M:%S'), 'symfony', sfLogger::getPriorityName($priority), $message, PHP_EOL)); 
     96    fwrite($this->fp, strtr($this->format, array( 
     97      '%type%'     => $this->type, 
     98      '%message%'  => $message, 
     99      '%time%'     => strftime($this->timeFormat), 
     100      '%priority%' => $this->getPriority($priority), 
     101      '%EOL%'      => PHP_EOL, 
     102    ))); 
    77103    flock($this->fp, LOCK_UN); 
     104  } 
     105 
     106  /** 
     107   * Returns the priority string to use in log messages. 
     108   * 
     109   * @param  string The priority constant 
     110   * 
     111   * @return string The priority to use in log messages 
     112   */ 
     113  protected function getPriority($priority) 
     114  { 
     115    return sfLogger::getPriorityName($priority); 
    78116  } 
    79117 
  • branches/1.1/test/unit/log/sfFileLoggerTest.php

    r4957 r7201  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(3, new lime_output_color()); 
     13$t = new lime_test(7, new lime_output_color()); 
    1414 
    1515require_once(dirname(__FILE__).'/../../../lib/util/sfToolkit.class.php'); 
     
    4444$t->like($lines[1], '/bar/', '->log() logs a message to the file'); 
    4545 
     46class TestLogger extends sfFileLogger 
     47{ 
     48  public function getTimeFormat() 
     49  { 
     50    return $this->timeFormat; 
     51  } 
     52 
     53  protected function getPriority($priority) 
     54  { 
     55    return '*'.$priority.'*'; 
     56  } 
     57} 
     58 
     59// option: format 
     60$t->diag('option: format'); 
     61unlink($file); 
     62$logger = new TestLogger($dispatcher, array('file' => $file)); 
     63$logger->log('foo'); 
     64$t->is(file_get_contents($file), strftime($logger->getTimeFormat()).' symfony [*6*] foo'.PHP_EOL, '->initialize() can take a format option'); 
     65 
     66unlink($file); 
     67$logger = new TestLogger($dispatcher, array('file' => $file, 'format' => '%message%')); 
     68$logger->log('foo'); 
     69$t->is(file_get_contents($file), 'foo', '->initialize() can take a format option'); 
     70 
     71// option: time_format 
     72$t->diag('option: time_format'); 
     73unlink($file); 
     74$logger = new TestLogger($dispatcher, array('file' => $file, 'time_format' => '%Y %m %d')); 
     75$logger->log('foo'); 
     76$t->is(file_get_contents($file), strftime($logger->getTimeFormat()).' symfony [*6*] foo'.PHP_EOL, '->initialize() can take a format option'); 
     77 
     78// option: type 
     79$t->diag('option: type'); 
     80unlink($file); 
     81$logger = new TestLogger($dispatcher, array('file' => $file, 'type' => 'foo')); 
     82$logger->log('foo'); 
     83$t->is(file_get_contents($file), strftime($logger->getTimeFormat()).' foo [*6*] foo'.PHP_EOL, '->initialize() can take a format option'); 
     84 
    4685// ->shutdown() 
    4786$t->diag('->shutdown()');