public function testRotation()
 {
     if (file_exists('log')) {
         rmdir('log');
     }
     mkdir("log");
     $logger = Logger::create("log")->setDatetimeFormat("")->setEntrySeparator("")->setLineSeparator("")->setPrefix("l")->setSuffix("")->setMaxFileCount(4)->setMaxFileSize(10)->setHeader("");
     // Header is therefore one space that separates the (empty) date from the (empty) header text
     $logger->log("A23456789");
     $logger->flush();
     $logger->log("B23456789");
     $logger->flush();
     $logger->log("C23456789");
     $logger->flush();
     $logger->log("D23456789");
     $logger->flush();
     $logger->log("E23456789");
     $logger->flush();
     $logger->log("F23456789");
     $logger->flush();
     $logger->log("G23456789");
     $logger->flush();
     // We have filled 7 files, and maximum file count is 4.
     // It should have gone:
     // A l0
     // B l0, l1
     // C l0, l1, l2
     // D l0, l1, l2, l3
     // E l1, l2, l3, l4
     // F l0, l2, l3, l4
     // G l0, l1, l3, l4, where l1 is newest and l3 oldest
     $this->assertFileExists('log/l0');
     $this->assertFileExists('log/l1');
     $this->assertFileNotExists('log/l2');
     $this->assertFileExists('log/l3');
     $this->assertFileExists('log/l4');
     $this->assertFileNotExists('log/l5');
     $this->assertFileNotExists('log/l6');
     $this->assertFileNotExists('log/l7');
     $this->assertSame(' D23456789', file_get_contents('log/l3'));
     $this->assertSame(' E23456789', file_get_contents('log/l4'));
     $this->assertSame(' F23456789', file_get_contents('log/l0'));
     $this->assertSame(' G23456789', file_get_contents('log/l1'));
     \asm\utils\Filesystem::removeDir("log");
 }
Example #2
0
 /**
  * Creates and initializes logger instance if it doesn't exist yet.
  */
 protected static function initLogger()
 {
     if (!self::$logger) {
         $user = User::instance();
         $username = $user->isLogged() ? $user->getName() : '[not logged in]';
         $remoteAddr = $_SERVER['REMOTE_ADDR'] != '::1' ? $_SERVER['REMOTE_ADDR'] : '[localhost]';
         $remoteHost = isset($_SERVER['REMOTE_HOST']) ? $_SERVER['REMOTE_ADDR'] : '[no lookup]';
         self::$logger = Logger::create(Config::get('paths', 'log'))->setMaxFileSize(2097152)->setMaxFileCount(5)->setEntrySeparator("\n\n")->setLineSeparator("\n")->setDatetimeFormat('Y-m-d H:i:s')->setHeader("User " . $username . ", IP " . $remoteAddr . ", host " . $remoteHost . ", request " . self::$request);
     }
 }