public function testActivationDoesNotCreateTheFile() { $path = PHPUNIT_TEMP_DIR . "/doesnotexisthopefully.log"; @unlink($path); $appender = new LoggerAppenderFile(); $appender->setFile($path); $appender->activateOptions(); self::assertFalse(file_exists($path)); $event = LoggerTestHelper::getInfoEvent('bla'); $appender->append($event); self::assertTrue(file_exists($path)); }
/** * Initialize objects for logging. We have two logs files: * - gid-last-request.log: to see logger for the last request * - gid-all-requests.log: to see all logger for all request * * @param \LoggerHierarchy Object to handle objects for logging. * @param mixed Either path to the config file or the configuration as * an array. * @return void */ public function configure(\LoggerHierarchy $hierarchy, $input = null) { //Create a logger layout $layout = new \LoggerLayoutPattern(); $layout->setConversionPattern("%d{Y-m-d H:i:s}[%r] %-5level %C.%M[%L] %msg%n"); $layout->activateOptions(); // Create an appender which logs to file $appLog = new \LoggerAppenderRollingFile('main'); $appLog->setFile($this->log_path . 'gid-all-requests.log'); $appLog->setAppend(true); $appLog->setMaxFileSize('2MB'); $appLog->setMaxBackupIndex(5); $appLog->setThreshold($this->threshold); $appLog->setLayout($layout); $appLog->activateOptions(); //Create an appender which logs Console $appConsole = new \LoggerAppenderFile('console'); $appConsole->setFile($this->log_path . 'gid-last-request.log'); $appConsole->setAppend(false); $appConsole->setThreshold($this->threshold); $appConsole->setLayout($layout); $appConsole->activateOptions(); // Add appenders to the root logger $root = $hierarchy->getRootLogger(); $root->addAppender($appLog); $root->addAppender($appConsole); }
/** * The File property takes a string value which should be the name of the file to append to. * Sets and opens the file where the log output will go. * * @see LoggerAppenderFile::setFile() */ public function setFile() { $numargs = func_num_args(); $args = func_get_args(); if ($numargs == 1 and is_string($args[0])) { parent::setFile(sprintf((string) $args[0], date($this->getDatePattern()))); } elseif ($numargs == 2 and is_string($args[0]) and is_bool($args[1])) { parent::setFile(sprintf((string) $args[0], date($this->getDatePattern())), $args[1]); } }
public function testSimpleLogging() { $layout = new LoggerLayoutSimple(); $event = new LoggerLoggingEvent('LoggerAppenderFileTest', new Logger('mycategory'), LoggerLevel::getLevelWarn(), "my message"); $appender = new LoggerAppenderFile("mylogger"); $appender->setFileName('../../../target/temp/phpunit/TEST.txt'); $appender->setLayout($layout); $appender->activateOptions(); $appender->append($event); $appender->close(); $v = file_get_contents('../../../target/temp/phpunit/TEST.txt'); $e = "WARN - my message" . PHP_EOL; self::assertEquals($e, $v); }
public function activateOptions() { parent::activateOptions(); if ($this->compress && !extension_loaded('zlib')) { $this->warn("The 'zlib' extension is required for file compression. Disabling compression."); $this->compression = false; } }
/** * @param LoggerLoggingEvent $event */ public function append(LoggerLoggingEvent $event) { parent::append($event); if (ftell($this->fp) > $this->getMaximumFileSize()) { $this->rollOver(); } }
/** * @param LoggerLoggingEvent $event */ function append($event) { if ($this->fp) { parent::append($event); if (ftell($this->fp) > $this->getMaximumFileSize()) { $this->rollOver(); } } }
/** * Appends a logging event. * * If the target file changes because of passage of time (e.g. at midnight) * the current file is closed. A new file, with the new date, will be * opened by the write() method. */ public function append(LoggerLoggingEvent $event) { $eventDate = $this->getDate($event->getTimestamp()); // Initial setting of current date if (!isset($this->currentDate)) { $this->currentDate = $eventDate; } else { if ($this->currentDate !== $eventDate) { $this->currentDate = $eventDate; // Close the file if it's open. // Note: $this->close() is not called here because it would set // $this->closed to true and the appender would not recieve // any more logging requests if (is_resource($this->fp)) { $this->write($this->layout->getFooter()); fclose($this->fp); } $this->fp = null; } } parent::append($event); }
/** Additional validation for the date pattern. */ public function activateOptions() { parent::activateOptions(); if (empty($this->datePattern)) { $this->warn("Required parameter 'datePattern' not set. Closing appender."); $this->closed = true; return; } }
public function __Destruct() { parent::__Destruct(); }
/** * Similar to the parent method, but replaces "%s" in the file name with * the current date in format specified by $datePattern. * * @see LoggerAppenderFile::setFile() */ public function setFile($file) { $date = date($this->getDatePattern()); $file = sprintf($file, $date); parent::setFile(sprintf($file, $date)); }
public function testActualSubstituteConstants() { $a = new LoggerAppenderFile(); $a->setFile('${PHPUNIT_TEMP_DIR}/log.txt'); $actual = $a->getFile(); $expected = PHPUNIT_TEMP_DIR . '/log.txt'; self::assertSame($expected, $actual); }
public function testRequiresLayout() { $appender = new LoggerAppenderFile(); self::assertTrue($appender->requiresLayout()); }
/** * Similar to the parent method, but replaces "%s" in the file name with * the current date in format specified by $datePattern. * * @see LoggerAppenderFile::setFile() */ public function setFile($file) { //add by jiangli 2014-6-11 //设置日志目录可配置 $config =& get_config(); $log_path_dir = $config['log_path_dir']; if (isset($log_path_dir) && !empty($log_path_dir)) { $log_path_dir = rtrim($log_path_dir, '/') . '/'; } else { $log_path_dir = ''; } $file = $log_path_dir . $file; $date = date($this->getDatePattern()); $file = sprintf($file, $date); parent::setFile(sprintf($file, $date)); }