Changeset 7201
- Timestamp:
- 01/28/08 15:52:55 (9 months ago)
- Files:
-
- branches/1.1/lib/log/sfFileLogger.class.php (modified) (4 diffs)
- branches/1.1/test/unit/log/sfFileLoggerTest.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.1/lib/log/sfFileLogger.class.php
r6767 r7201 20 20 { 21 21 protected 22 $fp = null; 22 $type = 'symfony', 23 $format = '%time% %type% [%priority%] %message%%EOL%', 24 $timeFormat = '%b %d %H:%M:%S', 25 $fp = null; 23 26 24 27 /** … … 27 30 * Available options: 28 31 * 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) 33 38 * 34 39 * @param sfEventDispatcher A sfEventDispatcher instance … … 42 47 { 43 48 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']; 44 64 } 45 65 … … 74 94 { 75 95 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 ))); 77 103 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); 78 116 } 79 117 branches/1.1/test/unit/log/sfFileLoggerTest.php
r4957 r7201 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 13 $t = new lime_test( 3, new lime_output_color());13 $t = new lime_test(7, new lime_output_color()); 14 14 15 15 require_once(dirname(__FILE__).'/../../../lib/util/sfToolkit.class.php'); … … 44 44 $t->like($lines[1], '/bar/', '->log() logs a message to the file'); 45 45 46 class 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'); 61 unlink($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 66 unlink($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'); 73 unlink($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'); 80 unlink($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 46 85 // ->shutdown() 47 86 $t->diag('->shutdown()');