zetacomponents /EventLog/tests/log_test.php

Language PHP Lines 591
MD5 Hash 7852d3e9cd38c42f84bdd935330d3637
Repository https://github.com/Yannix/zetacomponents.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
<?php
/**
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
 * @version //autogentag//
 * @filesource
 * @package EventLog
 * @subpackage Tests
 */

/**
 * @package EventLog
 * @subpackage Tests
 */
class ezcLogTest extends ezcTestCase
{
    protected $log;

    public function __construct($string = "")
    {
        parent::__construct($string);

        // These instances yield for all these tests.
        // $this->log = ezcLog::getInstance();

        date_default_timezone_set("UTC");
   }

    public function __destruct()
    {
        // $this->removeTempDir();
    }

    protected function setUp()
    {
        set_error_handler(array( $this, "TestErrorHandler"));
        $this->log = ezcLog::getInstance();
        $this->log->reset();
        $this->createTempDir( "ezcLogTest_" );
    }

    protected function tearDown()
    {
        // $this->cleanTempDir();
        $this->removeTempDir();
        restore_error_handler();
    }

    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->getMapper()->appendRule( new ezcLogFilterRule( new ezcLogFilter(), $a = new ezcLogUnixFileWriter( $this->getTempDir(), "default.log" ), true ) );

        $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->getMapper()->appendRule( new ezcLogFilterRule(new ezcLogFilter(), $a = new ezcLogUnixFileWriter( $this->getTempDir(), "default.log" ), true ) );

        $filter = new ezcLogFilter();
        $filter->severity = ezcLog::DEBUG | ezcLog::INFO | ezcLog::WARNING | ezcLog::ERROR | ezcLog::FATAL;
        $a->setFile( $filter, "logging.log" );

        $filter->severity = ezcLog::SUCCESS_AUDIT | ezcLog::FAILED_AUDIT;
        $a->setFile( $filter, "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->getMapper()->appendRule( new ezcLogFilterRule( new ezcLogFilter(), $writer = new ezcLogUnixFileWriter( $this->getTempDir() ), true) );

        $filter = new ezcLogFilter();
        $filter->severity = ezcLog::WARNING | ezcLog::ERROR;
        $writer->setFile( $filter, "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"))); 

        $filter->severity = ezcLog::FATAL;
        $writer->setFile( $filter, "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->getMapper()->appendRule( new ezcLogFilterRule( new ezcLogFilter(), $writer = new ezcLogUnixFileWriter( $this->getTempDir() ), true ) );

        // General logging
        $filter = new ezcLogFilter();
        $filter->severity = ezcLog::WARNING | ezcLog::ERROR | ezcLog::FATAL;

        $writer->setFile( $filter, "logging.log" );

        $filter->severity = ezcLog::ERROR | ezcLog::FATAL;
        $writer->setFile( $filter, "critical.log" );
        // Debug and Info messages are not kept.


        // We really want to know everything about the payments.
        $filter = new ezcLogFilter();
        $filter->source = array("Paynet");
        $writer->map( $filter, "paynet.log" );

        // But we are not interested in the debug and template messages.
        $filter->severity = ezcLog::DEBUG;
        $writer->unmap( $filter,  "paynet.log" );

        $filter->severity = 0;
        $filter->category = array("templates");
        $writer->unmap( $filter, "paynet.log" );

        
        // Keep a security log.
        $filter = new ezcLogFilter();
        $filter->severity = ezcLog::SUCCESS_AUDIT | ezcLog::FAILED_AUDIT;
        $filter->category = array("Login/Logoff");
        $writer->map( $filter, "security.log" );

        // Store everything except the Login/Logoff
        $filter = new ezcLogFilter();
        $filter->severity = ezcLog::SUCCESS_AUDIT | ezcLog::FAILED_AUDIT;

        $writer->map( $filter, "content_modifications.log" );
        $filter->category = array( "Login/Logoff");
        $writer->unmap( $filter, "content_modifications.log" ); 


        // The templates from the content module seems to have a problem, extra logging required:
        $filter = new ezcLogFilter();
        $filter->source = array("content");
        $filter->category = array("templates");
        $writer->map( $filter, "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->map(new ezcLogFilter(), $writer = new ezcLogUnixFileWriter( $this->getTempDir(), "default.log" ));
//        $this->log->getMapper()->appendRule( new ezcLogFilterRule(new ezcLogFilter(), $a = new ezcLogUnixFileWriter( $this->getTempDir(), "default.log" ), true ) );
//        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 ezcLogUnixFileWriter( $this->getTempDir(), "default.log" );

        $this->log->map($filter, $writer);
    }

    public function testLogDatabase()
    {
        try
        {
            $db = ezcDbInstance::get();
        }
        catch ( Exception $e )
        {
            $this->markTestSkipped();
        }

        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->map($filter, $dbWriter = new ezcLogDatabaseWriter($db, "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()
    {
        try
        {
            $db = ezcDbInstance::get();
        }
        catch ( Exception $e )
        {
            $this->markTestSkipped();
        }

        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->map($filter, $dbWriter = new ezcLogDatabaseWriter($db, "audits"));
        $filter->severity = ezcLog::DEBUG | ezcLog::INFO | ezcLog::NOTICE | ezcLog::WARNING | ezcLog::ERROR | ezcLog::FATAL;

        $this->log->map($filter, $fileWriter = new ezcLogUnixFileWriter( $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()
    {
        try
        {
            $db = ezcDbInstance::get();
        }
        catch ( Exception $e )
        {
            $this->markTestSkipped( 'There is no database configured.' );
        }

        $this->log->reset();
        $this->log->getMapper()->appendRule( new ezcLogFilterRule(  new ezcLogFilter, $writer = new ezcLogDatabaseWriter( $db,  "log" ), true ) );

        try
        {
            $this->log->log( "msg", ezcLog::WARNING, array( "source" => "src", "category" => "cat" ) );
        } 
        catch ( ezcLogWriterException $e )
        {
            return;
        }
        $this->fail("Expected an ezcLogWriterException.");
    }

    public function testDisableExceptions()
    {
        try
        {
            $db = ezcDbInstance::get();
        }
        catch ( Exception $e )
        {
            $this->markTestSkipped( 'There is no database configured.' );
        }

        $this->log->reset();
        $this->log->getMapper()->appendRule( new ezcLogFilterRule( new ezcLogFilter, $writer = new ezcLogDatabaseWriter( $db, "log" ), true ) );
        $this->log->throwWriterExceptions( false );

        try
        {
            $this->log->log( "msg", ezcLog::WARNING, array( "source" => "src", "category" => "cat" ) );
        } 
        catch ( ezcLogWriterException $e )
        {
            $this->fail( "Didn't expect an ezcLogWriterException." );
        }
        // re-enable writer exceptions
        $this->log->throwWriterExceptions( true );
    }

    /*
    public function testDetach()
    {
        try
        {
            $db = ezcDbInstance::get();
        }
        catch ( Exception $e )
        {
            $this->markTestSkipped();
        }

        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->map($filter, $dbWriter = new ezcLogDatabaseWriter( $db, "audits"));
        $this->log->map(new ezcLogFilter, $fileWriter = new ezcLogUnixFileWriter( $this->getTempDir(), "default.log" ) );
        $this->log->unmap($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->getMapper()->appendRule( new ezcLogFilterRule(  new ezcLogFilter, $writer = new ezcLogUnixFileWriter( $dir, $file ), true ) );

        $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 function testAutoAttributes()
    {
        $dir = $this->getTempDir();
        $file = "default.log";

        $log = ezcLog::getInstance();
        $writer = new ezcLogUnixFileWriter( "$dir", "$file" );
        $log->getmapper()->appendRule( new ezcLogFilterRule( new ezcLogFilter, $writer, true ) );

        $username = "John Doe";
        $service = "Paynet Terminal";

        // Add automatically the username to the log message, when the log message is either a SUCCESS_AUDIT or a FAILED_AUDIT.
        $log->setSeverityAttributes( ezcLog::SUCCESS_AUDIT | ezcLog::FAILED_AUDIT, array( "username" => $username ) );

        // Same can be done with the source of the log message.
        $log->setSourceAttributes( array( "Payment" ), array( "service" => $service ) );

        // Writing some log messages.
        $log->log( "Authentication failed", ezcLog::FAILED_AUDIT, array( "source" => "security", "category" => "login/logoff" ) );

        $log->source = "Payment"; 
        $log->log( "Connecting with the server.", ezcLog::DEBUG, array( "category" => "external connections" ) );

        $log->log( "Payed with creditcard.", ezcLog::SUCCESS_AUDIT, array( "category" => "shop" ) );

        $lines = file( "$dir/$file" );

        $this->assertRegExp("/username: John Doe/", $lines[0]);

        $this->assertRegExp("/service: Paynet Terminal/", $lines[1]);

        $this->assertRegExp("/username: John Doe/", $lines[2]);
        $this->assertRegExp("/service: Paynet Terminal/", $lines[2]);
    }

    public function testOtherSeverities()
    {
        $dir = $this->getTempDir();
        $file = "default.log";

        $log = ezcLog::getInstance();
        $writer = new ezcLogUnixFileWriter( "$dir", "$file" );
        $log->getMapper()->appendRule( new ezcLogFilterRule( new ezcLogFilter, $writer, true ) );

        $username = "John Doe";
        $service = "Paynet Terminal";
        // Add automatically the username to the log message, when the log message is FATAL.
        $log->setSeverityAttributes( ezcLog::FATAL, array( "username" => $username ) );
        $log->log( "Hackers have breached the security!", ezcLog::FATAL, array( "source" => "security", "category" => "login" ) );
        $log->log( "Something unknown happened...", false, array( "source" => "security", "category" => "login" ) );

        $lines = file( "$dir/$file" );
        $this->assertRegExp("/username: John Doe/", $lines[0]);
    }

    public function testProperties()
    {
        $log = ezcLog::getInstance();
        $log->source = "Payment";
        $log->category = "Corporate";
        $this->assertEquals( "Payment", $log->source );
        $this->assertEquals( "Corporate", $log->category );
        try
        {
            $val = $log->no_such_property;
            $this->fail( "Expected exception was not thrown" );
        }
        catch ( ezcBasePropertyNotFoundException $e )
        {
            $this->assertEquals( "No such property name 'no_such_property'.", $e->getMessage() );
        }
        try
        {
            $log->no_such_property = "xxx";;
            $this->fail( "Expected exception was not thrown" );
        }
        catch ( ezcBasePropertyNotFoundException $e )
        {
            $this->assertEquals( "No such property name 'no_such_property'.", $e->getMessage() );
        }
    }

    public function testIsSet()
    {
        $log = ezcLog::getInstance();
        $this->assertEquals( true, isset( $log->source ) );
        $this->assertEquals( true, isset( $log->category ) );
        $this->assertEquals( false, isset( $log->no_such_property ) );
    }

    public static function suite()
    {
         return new PHPUnit_Framework_TestSuite("ezcLogTest");
    }
}
?>
Back to Top