log = ezcLog::getInstance(); date_default_timezone_set("UTC"); } public function __destruct() { //$this->removeTempDir(); } public function setUp() { set_error_handler(array( $this, "TestErrorHandler")); $this->log = ezcLog::getInstance(); $this->createTempDir( "ezcLogTest_" ); } public function tearDown() { //$this->cleanTempDir(); $this->removeTempDir(); } public function TestErrorHandler($errno, $errstr, $errfile, $errline) { switch ($errno) { case E_USER_ERROR: case E_USER_WARNING: case E_USER_NOTICE: ezcLog::LogHandler($errno, $errstr, $errfile, $errline); break; default: print( "$errstr in $errfile on line $errline\n" ); break; } } public function testSimpleMessage() { $this->log->attach( new ezcLogFilter(), $a = new ezcLogWriterUnixFile( $this->getTempDir(), "default.log" ) ); $this->log->log("Writing a simple message.", ezcLog::DEBUG, array( "source" => "ezComponents Log", "category" => "Testing", "file" => "myFile", "line" => "myLine") ); clearstatcache(); $this->assertTrue( file_exists( $this->getTempDir()."/default.log"), "Expected the file: ".$this->getTempDir()."/default.log" ); $this->assertTrue( filesize( $this->getTempDir() . "/default.log" ) > 0, "default.log is still empty." ); $regExp = "/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d+ \d+:\d+:\d+ \[Debug\] \[ezComponents Log\] \[Testing\] Writing a simple message. \(file: myFile, line: myLine\)/"; $this->assertRegExp($regExp, file_get_contents( $this->getTempDir() . "/default.log") ); } public function testMultipleWriters() { $this->log->attach(new ezcLogFilter(), $a = new ezcLogWriterUnixFile( $this->getTempDir(), "default.log" ) ); $a->logTo( ezcLog::DEBUG | ezcLog::INFO | ezcLog::WARNING | ezcLog::ERROR | ezcLog::FATAL, array(), array(), "logging.log" ); $a->logTo( ezcLog::SUCCESS_AUDIT | ezcLog::FAILED_AUDIT, array(), array(), "auditing.log" ); $this->log->log("Bernard, float over here so I can punch you.", ezcLog::DEBUG, array( "source" => "ezComponents Log", "category" => "Testing", "file" => "myFile", "line" => "myLine") ); $this->assertFalse( filesize( $this->getTempDir() . "/default.log" ) > 0, "default.log should be empty." ); $this->assertFalse( filesize( $this->getTempDir() . "/auditing.log" ) > 0, "auditing.log should be empty." ); $regExp = "/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d+ \d+:\d+:\d+ \[Debug\] \[ezComponents Log\] \[Testing\] Bernard, float over here so I can punch you. \(file: myFile, line: myLine\)/"; $this->assertRegExp($regExp, file_get_contents( $this->getTempDir() . "/logging.log") ); $this->log->log("To save the world, you have to push a few old ladies down the stairs", ezcLog::SUCCESS_AUDIT, array( "source" => "ezComponents Log", "category" => "Testing", "file" => "myFile", "line" => "myLine") ); $regExp2 = "/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d+ \d+:\d+:\d+ \[Success audit\] \[ezComponents Log\] \[Testing\] To save the world, you have to push a few old ladies down the stairs \(file: myFile, line: myLine\)/"; $this->assertNotRegExp($regExp2, file_get_contents( $this->getTempDir() . "/logging.log") ); // Not in this log. $this->assertRegExp($regExp2, file_get_contents( $this->getTempDir() . "/auditing.log") ); // But in this one. } public function testReAttach() { $this->log->attach( new ezcLogFilter(), $writer = new ezcLogWriterUnixFile( $this->getTempDir() ) ); $writer->logTo( ezcLog::WARNING | ezcLog::ERROR, array(), array(), "logging.log" ); $this->log->log( "HI", ezcLog::ERROR, array( "source" => "content", "category" => "templates") ); $this->log->log( "HI", ezcLog::ERROR, array( "source" => "content", "category" => "templates") ); $this->assertEquals(2, sizeof(file($this->getTempDir() . "/logging.log"))); $writer->logTo( ezcLog::FATAL, array(), array(), "logging.log" ); $this->log->log( "HI", ezcLog::ERROR, array( "source" => "content", "category" => "templates") ); $this->assertEquals(3, sizeof(file($this->getTempDir() . "/logging.log"))); } public function testLargeApplicationLog() { $this->log->reset(); // Don't write to a default.log file. $this->log->attach( new ezcLogFilter(), $writer = new ezcLogWriterUnixFile( $this->getTempDir() ) ); // General logging $writer->logTo( ezcLog::WARNING | ezcLog::ERROR | ezcLog::FATAL, array(), array(), "logging.log" ); $writer->logTo( ezcLog::ERROR | ezcLog::FATAL, array(), array(), "critical.log" ); // Debug and Info messages are not kept. // We really want to know everything about the payments. $writer->logTo( 0, array("Paynet"), array(), "paynet.log" ); // But we are not interested in the debug and template messages. $writer->logNotTo( ezcLog::DEBUG, array("Paynet"), array(), "paynet.log" ); $writer->logNotTo( 0, array("Paynet"), array("templates"), "paynet.log" ); // Keep a security log. $writer->logTo( ezcLog::SUCCESS_AUDIT | ezcLog::FAILED_AUDIT, array(), array("Login/Logoff"), "security.log" ); // Store everything except the Login/Logoff $writer->logTo( ezcLog::SUCCESS_AUDIT | ezcLog::FAILED_AUDIT, array(), array(), "content_modifications.log" ); $writer->logNotTo( ezcLog::SUCCESS_AUDIT | ezcLog::FAILED_AUDIT, array(), array("Login/Logoff"), "content_modifications.log" ); // The templates from the content module seems to have a problem, extra logging required: $writer->logTo( 0, array("content"), array("templates"), "content_templates.log"); $this->log->log( "Cannot read template: list_content.tpl", ezcLog::ERROR, array("source" => "content", "category" => "templates", "file" => __FILE__, "line" => __LINE__)); $this->assertTrue(file_exists( $this->getTempDir() . "/logging.log")); $this->assertTrue(file_exists( $this->getTempDir() . "/critical.log")); $this->assertTrue(file_exists( $this->getTempDir() . "/content_templates.log")); $this->assertEquals(1, sizeof( file($this->getTempDir() . "/logging.log")) ); $this->assertEquals(1, sizeof( file($this->getTempDir() . "/critical.log")) ); $this->assertEquals(1, sizeof( file($this->getTempDir() . "/content_templates.log")) ); $this->assertEquals(0, filesize($this->getTempDir() . "/paynet.log")); // No message should be written to this log. $this->log->log( "User signed in to the system", ezcLog::SUCCESS_AUDIT, array( "source" => "Authentication", "category" => "Login/Logoff") ); $this->assertEquals(1, sizeof(file($this->getTempDir() . "/logging.log"))); $this->assertEquals(1, sizeof(file($this->getTempDir() . "/critical.log"))); $this->assertEquals(0, filesize($this->getTempDir() . "/paynet.log")); // No message should be written to this log. $this->assertEquals(1, sizeof(file($this->getTempDir() . "/content_templates.log"))); $this->assertEquals(1, sizeof(file($this->getTempDir() . "/security.log"))); $this->assertFalse( file_exists($this->getTempDir() . "/content_modifictions.log") ); $this->log->log( "Using 42 Kilobytes of memory", ezcLog::INFO, array( "source" => "Paynet", "category" => "Security") ); $this->assertEquals(1, sizeof(file($this->getTempDir() . "/paynet.log"))); $this->log->log( "Using 42 Kilobytes of memory", ezcLog::INFO, array( "source" => "Paynet", "category" => "Security") ); $this->assertEquals(2, sizeof(file($this->getTempDir() . "/paynet.log"))); $this->log->log( "sysstat: 1 + 1 = 2", ezcLog::DEBUG, array( "source" => "Paynet", "category" => "Security") ); $this->assertEquals(2, sizeof(file($this->getTempDir() . "/paynet.log"))); // Nothing written. } public function testTriggerError() { $this->log->reset(); $this->log->attach(new ezcLogFilter(), $writer = new ezcLogWriterUnixFile( $this->getTempDir(), "default.log" )); trigger_error("Bernard, looking at all the quarters that fell out of the vending machine he broke with the crowbar.", E_USER_WARNING); $regExp = "|\[Warning\] \[default\] \[default\] Bern.* \(file: .*/log_test.php, line: \d+\)|"; $this->assertRegExp($regExp, file_get_contents( $this->getTempDir() . "/default.log") ); ezcLog::getInstance()->source = "Hoagie"; ezcLog::getInstance()->category = "Male"; trigger_error("Bernard, looking at all the quarters that fell out of the vending machine he broke with the crowbar.", E_USER_WARNING); $lines = file( $this->getTempDir() . "/default.log"); $this->assertNotRegExp($regExp, $lines[count($lines) - 1] ); $regExp = "|\[Warning\] \[Hoagie\] \[Male\] Bernard|"; $this->assertRegExp($regExp, $lines[count($lines) - 1] ); } public function testLogFilter() { $filter = new ezcLogFilter(); $writer = new ezcLogWriterUnixFile( $this->getTempDir(), "default.log" ); $this->log->attach($filter, $writer); } public function testLogDatabase() { $db = ezcDbInstance::get(); try { $db->exec("DROP TABLE `audits`"); } catch (Exception $e) { } $db->exec("CREATE TABLE `audits` ( `id` INT NOT NULL AUTO_INCREMENT , `time` DATETIME NOT NULL ,". "`message` VARCHAR( 255 ) NOT NULL , `severity` VARCHAR( 40 ) NOT NULL , `source` VARCHAR( 100 ) NOT NULL ,". "`category` VARCHAR( 100 ) NOT NULL , `name` VARCHAR( 100 ) NOT NULL, PRIMARY KEY ( `id` ))"); $filter = new ezcLogFilter(); $filter->severity = ezcLog::SUCCESS_AUDIT | ezcLog::FAILED_AUDIT; $this->log->reset(); // Setup the default database writer. $this->log->attach($filter, $dbWriter = new ezcLogWriterDatabase("audits")); $this->log->log("Hoagie logged in.", ezcLog::SUCCESS_AUDIT, array( "source" => "administration interface", "category" => "security", "name" => "Hoagie") ); $a = $db->query("SELECT * FROM `audits`")->fetch(); $this->assertEquals("Hoagie logged in.", $a["message"], "Message doesn't match"); $this->assertEquals("administration interface", $a["source"], "Source doesn't match"); $this->assertEquals("security", $a["category"], "Category doesn't match"); $this->assertEquals("Success audit", $a["severity"], "Severity doesn't match"); $this->assertEquals("Hoagie", $a["name"], "Extra info doesn't match"); $db->exec("DROP TABLE `audits`"); } public function testLogDatabaseAndFile() { $db = ezcDbInstance::get(); try { $db->exec("DROP TABLE `audits`"); } catch ( Exception $e ) { } $db->exec("CREATE TABLE `audits` ( `id` INT NOT NULL AUTO_INCREMENT , `time` DATETIME NOT NULL ,". "`message` VARCHAR( 255 ) NOT NULL , `severity` VARCHAR( 40 ) NOT NULL , `source` VARCHAR( 100 ) NOT NULL ,". "`category` VARCHAR( 100 ) NOT NULL , `name` VARCHAR( 100 ) NOT NULL, PRIMARY KEY ( `id` ))"); $filter = new ezcLogFilter(); $filter->severity = ezcLog::SUCCESS_AUDIT | ezcLog::FAILED_AUDIT; $this->log->reset(); // Setup the default database writer. $this->log->attach($filter, $dbWriter = new ezcLogWriterDatabase("audits")); $filter->severity = ezcLog::DEBUG | ezcLog::INFO | ezcLog::NOTICE | ezcLog::WARNING | ezcLog::ERROR | ezcLog::FATAL; $this->log->attach($filter, $fileWriter = new ezcLogWriterUnixFile( $this->getTempDir(), "default.log" ) ); $this->log->log("Hoagie logged in.", ezcLog::SUCCESS_AUDIT, array( "source" => "administration interface", "category" => "security", "name" => "Hoagie") ); $a = $db->query("SELECT * FROM `audits`")->fetch(); $this->assertEquals("Hoagie logged in.", $a["message"], "Message doesn't match"); $this->assertEquals("administration interface", $a["source"], "Source doesn't match"); $this->assertEquals("security", $a["category"], "Category doesn't match"); $this->assertEquals("Success audit", $a["severity"], "Severity doesn't match"); $this->assertEquals("Hoagie", $a["name"], "Extra info doesn't match"); $this->log->log("Error", ezcLog::ERROR, array( "source" => "administration interface", "category" => "security", "line" => "100", "file" => "bla") ); $lines = file( $this->getTempDir() . "/default.log"); $this->assertRegExp("/Error/", $lines[0]); $this->assertRegExp("/administration interface/", $lines[0]); $this->assertRegExp("/security/", $lines[0]); $this->assertRegExp("/line/", $lines[0]); $this->assertRegExp("/100/", $lines[0]); $this->assertRegExp("/file/", $lines[0]); $this->assertRegExp("/bla/", $lines[0]); $db->exec("DROP TABLE `audits`"); } public function testException() { $this->log->reset(); $this->log->attach( new ezcLogFilter, $writer = new ezcLogWriterDatabase( "log" ) ); try { $this->log->log( "msg", ezcLog::WARNING, array( "source" => "src", "category" => "cat" ) ); } catch ( ezcLogWriterException $e ) { return; } $this->fail("Expected an ezcLogWriterException."); } public function testDisableExceptions() { $this->log->reset(); $this->log->attach(new ezcLogFilter, $writer = new ezcLogWriterDatabase("log")); $this->log->throwWriterExceptions(false); try { $this->log->log("msg", ezcLog::WARNING, array("source" => "src", "category" => "cat") ); return; } catch (ezcLogWriterException $e) { $this->fail("Didn't expect an ezcLogWriterException."); } } public function testDetach() { $db = ezcDbInstance::get(); try { $db->exec("DROP TABLE `audits`"); } catch ( Exception $e ) { } $db->exec("CREATE TABLE `audits` ( `id` INT NOT NULL AUTO_INCREMENT , `time` DATETIME NOT NULL ,". "`message` VARCHAR( 255 ) NOT NULL , `severity` VARCHAR( 40 ) NOT NULL , `source` VARCHAR( 100 ) NOT NULL ,". "`category` VARCHAR( 100 ) NOT NULL , `name` VARCHAR( 100 ) NOT NULL, PRIMARY KEY ( `id` ))"); $filter = new ezcLogFilter(); $filter->severity = ezcLog::SUCCESS_AUDIT | ezcLog::FAILED_AUDIT; $this->log->reset(); // Setup the default database writer. // HERE is the test: $this->log->attach($filter, $dbWriter = new ezcLogWriterDatabase("audits")); $this->log->attach(new ezcLogFilter, $fileWriter = new ezcLogWriterUnixFile( $this->getTempDir(), "default.log" ) ); $this->log->detach($filter, $fileWriter); //////////// $this->log->log("Hoagie logged in.", ezcLog::SUCCESS_AUDIT, array( "source" => "administration interface", "category" => "security", "name" => "Hoagie")); $a = $db->query("SELECT * FROM `audits`")->fetch(); $this->assertEquals("Hoagie logged in.", $a["message"], "Message doesn't match"); $this->assertEquals("administration interface", $a["source"], "Source doesn't match"); $this->assertEquals("security", $a["category"], "Category doesn't match"); $this->assertEquals("Success audit", $a["severity"], "Severity doesn't match"); $this->assertEquals("Hoagie", $a["name"], "Extra info doesn't match"); $this->log->log("Error", ezcLog::ERROR, array( "source" => "administration interface", "category" => "security", "line" => "100", "file" => "bla" )); $lines = file( $this->getTempDir() . "/default.log"); $this->assertRegExp("/Error/", $lines[0]); $this->assertRegExp("/administration interface/", $lines[0]); $this->assertRegExp("/security/", $lines[0]); $this->assertRegExp("/line/", $lines[0]); $this->assertRegExp("/100/", $lines[0]); $this->assertRegExp("/file/", $lines[0]); $this->assertRegExp("/bla/", $lines[0]); $db->exec("DROP TABLE `audits`"); } public function testDefaultProperties() { // Setting up the log. $this->log->source = "MySource"; $this->log->category = "MyCategory"; $dir = $this->getTempDir(); $file = "default.log"; $this->log->attach( new ezcLogFilter, $writer = new ezcLogWriterUnixFile( $dir, $file ) ); $this->log->log("Error", ezcLog::ERROR ); $str = file_get_contents( "$dir/$file" ); // We should have a file with the categories: MySource and MyDirectory. $this->assertTrue( strstr( $str, "MySource" ) !== false ); $this->assertTrue( strstr( $str, "MyCategory" ) !== false ); } public static function suite() { return new ezcTestSuite("ezcLogTest"); } } ?>