2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-19 21:10:13 +00:00
|
|
|
* CakeLogTest file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-04-27 02:49:18 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
2012-03-13 02:46:07 +00:00
|
|
|
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-03-13 02:46:07 +00:00
|
|
|
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2012-04-27 02:49:18 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Log
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.5432
|
2010-10-03 16:27:27 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-09 05:13:11 +00:00
|
|
|
|
|
|
|
App::uses('CakeLog', 'Log');
|
|
|
|
App::uses('FileLog', 'Log/Engine');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-03-19 21:10:13 +00:00
|
|
|
* CakeLogTest class
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Log
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2008-07-21 02:40:58 +00:00
|
|
|
class CakeLogTest extends CakeTestCase {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-11-04 23:07:39 +00:00
|
|
|
/**
|
2009-11-06 00:16:46 +00:00
|
|
|
* Start test callback, clears all streams enabled.
|
2009-11-04 23:07:39 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function setUp() {
|
2010-09-26 01:36:49 +00:00
|
|
|
parent::setUp();
|
2009-11-15 01:42:57 +00:00
|
|
|
$streams = CakeLog::configured();
|
2009-11-04 23:07:39 +00:00
|
|
|
foreach ($streams as $stream) {
|
2009-11-15 01:42:57 +00:00
|
|
|
CakeLog::drop($stream);
|
2009-11-04 23:07:39 +00:00
|
|
|
}
|
2009-11-06 00:16:46 +00:00
|
|
|
}
|
2009-11-04 23:07:39 +00:00
|
|
|
|
2009-11-06 01:13:15 +00:00
|
|
|
/**
|
|
|
|
* test importing loggers from app/libs and plugins.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testImportingLoggers() {
|
2009-11-06 01:13:15 +00:00
|
|
|
App::build(array(
|
2012-02-18 12:31:29 +00:00
|
|
|
'Lib' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
|
|
|
|
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
|
2012-02-18 12:04:54 +00:00
|
|
|
), App::RESET);
|
2011-05-09 05:01:38 +00:00
|
|
|
CakePlugin::load('TestPlugin');
|
2009-11-06 01:13:15 +00:00
|
|
|
|
|
|
|
$result = CakeLog::config('libtest', array(
|
|
|
|
'engine' => 'TestAppLog'
|
|
|
|
));
|
|
|
|
$this->assertTrue($result);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(CakeLog::configured(), array('libtest'));
|
2009-11-06 01:13:15 +00:00
|
|
|
|
|
|
|
$result = CakeLog::config('plugintest', array(
|
|
|
|
'engine' => 'TestPlugin.TestPluginLog'
|
|
|
|
));
|
|
|
|
$this->assertTrue($result);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(CakeLog::configured(), array('libtest', 'plugintest'));
|
2009-11-06 01:13:15 +00:00
|
|
|
|
2012-05-13 08:53:42 +00:00
|
|
|
CakeLog::write(LOG_INFO, 'TestPluginLog is not a BaseLog descendant');
|
|
|
|
|
2009-11-06 01:13:15 +00:00
|
|
|
App::build();
|
2011-05-09 05:01:38 +00:00
|
|
|
CakePlugin::unload();
|
2009-11-06 01:13:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test all the errors from failed logger imports
|
|
|
|
*
|
2010-12-12 00:01:07 +00:00
|
|
|
* @expectedException CakeLogException
|
2009-11-06 01:13:15 +00:00
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testImportingLoggerFailure() {
|
2009-11-06 01:13:15 +00:00
|
|
|
CakeLog::config('fail', array());
|
2010-04-24 02:31:21 +00:00
|
|
|
}
|
2009-11-06 01:13:15 +00:00
|
|
|
|
2012-05-09 08:55:56 +00:00
|
|
|
/**
|
|
|
|
* test config() with valid key name
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidKeyName() {
|
|
|
|
CakeLog::config('valid', array('engine' => 'FileLog'));
|
|
|
|
$stream = CakeLog::stream('valid');
|
|
|
|
$this->assertInstanceOf('FileLog', $stream);
|
|
|
|
CakeLog::drop('valid');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test config() with invalid key name
|
|
|
|
*
|
|
|
|
* @expectedException CakeLogException
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInvalidKeyName() {
|
|
|
|
CakeLog::config('1nv', array('engine' => 'FileLog'));
|
|
|
|
}
|
|
|
|
|
2010-04-24 02:31:21 +00:00
|
|
|
/**
|
|
|
|
* test that loggers have to implement the correct interface.
|
|
|
|
*
|
2010-12-12 00:01:07 +00:00
|
|
|
* @expectedException CakeLogException
|
2010-04-24 02:31:21 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testNotImplementingInterface() {
|
2009-11-06 01:13:15 +00:00
|
|
|
CakeLog::config('fail', array('engine' => 'stdClass'));
|
|
|
|
}
|
|
|
|
|
2009-11-06 00:16:46 +00:00
|
|
|
/**
|
|
|
|
* Test that CakeLog autoconfigures itself to use a FileLogger with the LOGS dir.
|
|
|
|
* When no streams are there.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAutoConfig() {
|
2011-05-28 21:08:22 +00:00
|
|
|
if (file_exists(LOGS . 'error.log')) {
|
|
|
|
unlink(LOGS . 'error.log');
|
|
|
|
}
|
2009-11-04 23:07:39 +00:00
|
|
|
CakeLog::write(LOG_WARNING, 'Test warning');
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'error.log'));
|
|
|
|
|
2009-11-15 01:42:57 +00:00
|
|
|
$result = CakeLog::configured();
|
2012-05-15 01:06:09 +00:00
|
|
|
$this->assertEquals(array('default'), $result);
|
|
|
|
|
|
|
|
$testMessage = 'custom message';
|
|
|
|
CakeLog::write('custom', $testMessage);
|
|
|
|
$content = file_get_contents(LOGS . 'custom.log');
|
|
|
|
$this->assertContains($testMessage, $content);
|
2009-11-06 00:16:46 +00:00
|
|
|
unlink(LOGS . 'error.log');
|
2012-05-15 01:06:09 +00:00
|
|
|
unlink(LOGS . 'custom.log');
|
2009-11-06 00:16:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test configuring log streams
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testConfig() {
|
2009-11-06 00:16:46 +00:00
|
|
|
CakeLog::config('file', array(
|
|
|
|
'engine' => 'FileLog',
|
|
|
|
'path' => LOGS
|
|
|
|
));
|
2009-11-15 01:42:57 +00:00
|
|
|
$result = CakeLog::configured();
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('file'), $result);
|
2009-11-06 00:16:46 +00:00
|
|
|
|
2010-11-15 02:07:23 +00:00
|
|
|
if (file_exists(LOGS . 'error.log')) {
|
|
|
|
@unlink(LOGS . 'error.log');
|
|
|
|
}
|
2009-11-06 00:16:46 +00:00
|
|
|
CakeLog::write(LOG_WARNING, 'Test warning');
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'error.log'));
|
|
|
|
|
|
|
|
$result = file_get_contents(LOGS . 'error.log');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning/', $result);
|
2009-11-06 00:16:46 +00:00
|
|
|
unlink(LOGS . 'error.log');
|
2009-11-04 23:07:39 +00:00
|
|
|
}
|
|
|
|
|
2009-11-15 01:44:42 +00:00
|
|
|
/**
|
2012-02-23 23:29:53 +00:00
|
|
|
* explicit tests for drop()
|
2009-11-15 01:44:42 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testDrop() {
|
2009-11-15 01:44:42 +00:00
|
|
|
CakeLog::config('file', array(
|
|
|
|
'engine' => 'FileLog',
|
|
|
|
'path' => LOGS
|
|
|
|
));
|
|
|
|
$result = CakeLog::configured();
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('file'), $result);
|
2009-11-15 01:44:42 +00:00
|
|
|
|
|
|
|
CakeLog::drop('file');
|
|
|
|
$result = CakeLog::configured();
|
2012-10-26 22:18:05 +00:00
|
|
|
$this->assertSame(array(), $result);
|
2009-11-15 01:44:42 +00:00
|
|
|
}
|
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testLogFileWriting method
|
2008-11-08 02:58:37 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testLogFileWriting() {
|
2011-05-28 21:08:22 +00:00
|
|
|
if (file_exists(LOGS . 'error.log')) {
|
|
|
|
unlink(LOGS . 'error.log');
|
|
|
|
}
|
2009-11-06 04:20:40 +00:00
|
|
|
$result = CakeLog::write(LOG_WARNING, 'Test warning');
|
|
|
|
$this->assertTrue($result);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue(file_exists(LOGS . 'error.log'));
|
|
|
|
unlink(LOGS . 'error.log');
|
|
|
|
|
|
|
|
CakeLog::write(LOG_WARNING, 'Test warning 1');
|
|
|
|
CakeLog::write(LOG_WARNING, 'Test warning 2');
|
|
|
|
$result = file_get_contents(LOGS . 'error.log');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 1/', $result);
|
|
|
|
$this->assertRegExp('/2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 2$/', $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
unlink(LOGS . 'error.log');
|
|
|
|
}
|
2009-09-08 03:59:31 +00:00
|
|
|
|
2012-05-09 08:55:56 +00:00
|
|
|
/**
|
2012-05-16 01:00:18 +00:00
|
|
|
* test selective logging by level/type
|
|
|
|
*
|
|
|
|
* @return void
|
2012-05-09 08:55:56 +00:00
|
|
|
*/
|
2012-05-16 01:00:18 +00:00
|
|
|
public function testSelectiveLoggingByLevel() {
|
2012-05-09 08:55:56 +00:00
|
|
|
if (file_exists(LOGS . 'spam.log')) {
|
|
|
|
unlink(LOGS . 'spam.log');
|
|
|
|
}
|
|
|
|
if (file_exists(LOGS . 'eggs.log')) {
|
|
|
|
unlink(LOGS . 'eggs.log');
|
|
|
|
}
|
|
|
|
CakeLog::config('spam', array(
|
|
|
|
'engine' => 'FileLog',
|
2012-07-22 00:59:05 +00:00
|
|
|
'types' => 'debug',
|
2012-05-09 08:55:56 +00:00
|
|
|
'file' => 'spam',
|
|
|
|
));
|
|
|
|
CakeLog::config('eggs', array(
|
|
|
|
'engine' => 'FileLog',
|
2012-07-22 00:59:05 +00:00
|
|
|
'types' => array('eggs', 'debug', 'error', 'warning'),
|
2012-05-09 08:55:56 +00:00
|
|
|
'file' => 'eggs',
|
|
|
|
));
|
|
|
|
|
|
|
|
$testMessage = 'selective logging';
|
|
|
|
CakeLog::write(LOG_WARNING, $testMessage);
|
|
|
|
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'eggs.log'));
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'spam.log'));
|
|
|
|
|
2012-07-22 00:59:05 +00:00
|
|
|
CakeLog::write(LOG_DEBUG, $testMessage);
|
2012-05-09 08:55:56 +00:00
|
|
|
$this->assertTrue(file_exists(LOGS . 'spam.log'));
|
|
|
|
|
|
|
|
$contents = file_get_contents(LOGS . 'spam.log');
|
2012-07-22 00:59:05 +00:00
|
|
|
$this->assertContains('Debug: ' . $testMessage, $contents);
|
2012-05-09 08:55:56 +00:00
|
|
|
$contents = file_get_contents(LOGS . 'eggs.log');
|
2012-07-22 00:59:05 +00:00
|
|
|
$this->assertContains('Debug: ' . $testMessage, $contents);
|
2012-05-09 08:55:56 +00:00
|
|
|
|
|
|
|
if (file_exists(LOGS . 'spam.log')) {
|
|
|
|
unlink(LOGS . 'spam.log');
|
|
|
|
}
|
|
|
|
if (file_exists(LOGS . 'eggs.log')) {
|
|
|
|
unlink(LOGS . 'eggs.log');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test enable
|
2012-05-16 01:00:18 +00:00
|
|
|
*
|
2012-05-09 08:55:56 +00:00
|
|
|
* @expectedException CakeLogException
|
|
|
|
*/
|
|
|
|
public function testStreamEnable() {
|
|
|
|
CakeLog::config('spam', array(
|
|
|
|
'engine' => 'FileLog',
|
|
|
|
'file' => 'spam',
|
|
|
|
));
|
|
|
|
$this->assertTrue(CakeLog::enabled('spam'));
|
|
|
|
CakeLog::drop('spam');
|
|
|
|
CakeLog::enable('bogus_stream');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test disable
|
2012-05-16 01:00:18 +00:00
|
|
|
*
|
2012-05-09 08:55:56 +00:00
|
|
|
* @expectedException CakeLogException
|
|
|
|
*/
|
|
|
|
public function testStreamDisable() {
|
|
|
|
CakeLog::config('spam', array(
|
|
|
|
'engine' => 'FileLog',
|
|
|
|
'file' => 'spam',
|
|
|
|
));
|
|
|
|
$this->assertTrue(CakeLog::enabled('spam'));
|
|
|
|
CakeLog::disable('spam');
|
|
|
|
$this->assertFalse(CakeLog::enabled('spam'));
|
|
|
|
CakeLog::drop('spam');
|
|
|
|
CakeLog::enable('bogus_stream');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test enabled() invalid stream
|
2012-05-16 01:00:18 +00:00
|
|
|
*
|
2012-05-09 08:55:56 +00:00
|
|
|
* @expectedException CakeLogException
|
|
|
|
*/
|
|
|
|
public function testStreamEnabledInvalid() {
|
|
|
|
CakeLog::enabled('bogus_stream');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test disable invalid stream
|
2012-05-16 01:00:18 +00:00
|
|
|
*
|
2012-05-09 08:55:56 +00:00
|
|
|
* @expectedException CakeLogException
|
|
|
|
*/
|
|
|
|
public function testStreamDisableInvalid() {
|
|
|
|
CakeLog::disable('bogus_stream');
|
|
|
|
}
|
|
|
|
|
2012-05-11 09:15:15 +00:00
|
|
|
protected function _resetLogConfig() {
|
|
|
|
CakeLog::config('debug', array(
|
|
|
|
'engine' => 'FileLog',
|
|
|
|
'types' => array('notice', 'info', 'debug'),
|
|
|
|
'file' => 'debug',
|
|
|
|
));
|
|
|
|
CakeLog::config('error', array(
|
|
|
|
'engine' => 'FileLog',
|
2012-05-11 11:31:55 +00:00
|
|
|
'types' => array('warning', 'error', 'critical', 'alert', 'emergency'),
|
2012-05-11 09:15:15 +00:00
|
|
|
'file' => 'error',
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2012-05-10 15:40:23 +00:00
|
|
|
protected function _deleteLogs() {
|
2012-05-11 09:15:15 +00:00
|
|
|
if (file_exists(LOGS . 'shops.log')) {
|
|
|
|
unlink(LOGS . 'shops.log');
|
|
|
|
}
|
|
|
|
if (file_exists(LOGS . 'error.log')) {
|
|
|
|
unlink(LOGS . 'error.log');
|
|
|
|
}
|
|
|
|
if (file_exists(LOGS . 'debug.log')) {
|
|
|
|
unlink(LOGS . 'debug.log');
|
|
|
|
}
|
2012-05-15 03:38:46 +00:00
|
|
|
if (file_exists(LOGS . 'bogus.log')) {
|
|
|
|
unlink(LOGS . 'bogus.log');
|
|
|
|
}
|
|
|
|
if (file_exists(LOGS . 'spam.log')) {
|
|
|
|
unlink(LOGS . 'spam.log');
|
|
|
|
}
|
|
|
|
if (file_exists(LOGS . 'eggs.log')) {
|
|
|
|
unlink(LOGS . 'eggs.log');
|
|
|
|
}
|
2012-05-10 15:40:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test backward compatible scoped logging
|
2012-10-08 16:57:02 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2012-05-10 15:40:23 +00:00
|
|
|
*/
|
|
|
|
public function testScopedLoggingBC() {
|
2012-05-11 09:15:15 +00:00
|
|
|
$this->_resetLogConfig();
|
2012-10-08 16:57:02 +00:00
|
|
|
|
2012-05-10 15:40:23 +00:00
|
|
|
CakeLog::config('shops', array(
|
|
|
|
'engine' => 'FileLog',
|
|
|
|
'types' => array('info', 'notice', 'warning'),
|
|
|
|
'scopes' => array('transactions', 'orders'),
|
|
|
|
'file' => 'shops',
|
2012-10-08 16:57:02 +00:00
|
|
|
));
|
|
|
|
$this->_deleteLogs();
|
2012-05-10 15:40:23 +00:00
|
|
|
|
|
|
|
CakeLog::write('info', 'info message');
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'error.log'));
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'debug.log'));
|
|
|
|
|
|
|
|
$this->_deleteLogs();
|
|
|
|
|
|
|
|
CakeLog::write('transactions', 'transaction message');
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'shops.log'));
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'transactions.log'));
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'error.log'));
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'debug.log'));
|
|
|
|
|
|
|
|
$this->_deleteLogs();
|
|
|
|
|
|
|
|
CakeLog::write('error', 'error message');
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'error.log'));
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'debug.log'));
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'shops.log'));
|
|
|
|
|
|
|
|
$this->_deleteLogs();
|
|
|
|
|
|
|
|
CakeLog::write('orders', 'order message');
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'error.log'));
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'debug.log'));
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'orders.log'));
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'shops.log'));
|
|
|
|
|
|
|
|
$this->_deleteLogs();
|
|
|
|
|
|
|
|
CakeLog::write('warning', 'warning message');
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'error.log'));
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'debug.log'));
|
|
|
|
|
|
|
|
$this->_deleteLogs();
|
|
|
|
|
|
|
|
CakeLog::drop('shops');
|
|
|
|
}
|
|
|
|
|
2012-10-09 16:35:21 +00:00
|
|
|
/**
|
|
|
|
* Test that scopes are exclusive and don't bleed.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-10-08 16:57:02 +00:00
|
|
|
public function testScopedLoggingExclusive() {
|
|
|
|
$this->_deleteLogs();
|
|
|
|
|
|
|
|
CakeLog::config('shops', array(
|
|
|
|
'engine' => 'FileLog',
|
|
|
|
'types' => array('info', 'notice', 'warning'),
|
|
|
|
'scopes' => array('transactions', 'orders'),
|
|
|
|
'file' => 'shops.log',
|
|
|
|
));
|
|
|
|
CakeLog::config('eggs', array(
|
|
|
|
'engine' => 'FileLog',
|
|
|
|
'types' => array('info', 'notice', 'warning'),
|
|
|
|
'scopes' => array('eggs'),
|
|
|
|
'file' => 'eggs.log',
|
|
|
|
));
|
|
|
|
|
|
|
|
CakeLog::write('info', 'transactions message', 'transactions');
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'eggs.log'));
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'shops.log'));
|
|
|
|
|
|
|
|
$this->_deleteLogs();
|
|
|
|
|
|
|
|
CakeLog::write('info', 'eggs message', 'eggs');
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'eggs.log'));
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'shops.log'));
|
|
|
|
}
|
|
|
|
|
2012-05-10 15:40:23 +00:00
|
|
|
/**
|
|
|
|
* test scoped logging
|
2012-05-16 01:00:18 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2012-05-10 15:40:23 +00:00
|
|
|
*/
|
|
|
|
public function testScopedLogging() {
|
2012-05-11 09:15:15 +00:00
|
|
|
$this->_resetLogConfig();
|
2012-10-08 16:57:02 +00:00
|
|
|
$this->_deleteLogs();
|
2012-05-10 15:40:23 +00:00
|
|
|
CakeLog::config('shops', array(
|
|
|
|
'engine' => 'FileLog',
|
|
|
|
'types' => array('info', 'notice', 'warning'),
|
|
|
|
'scopes' => array('transactions', 'orders'),
|
2012-10-08 16:57:02 +00:00
|
|
|
'file' => 'shops.log',
|
|
|
|
));
|
2012-05-10 15:40:23 +00:00
|
|
|
|
|
|
|
CakeLog::write('info', 'info message', 'transactions');
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'error.log'));
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'shops.log'));
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'debug.log'));
|
|
|
|
|
|
|
|
$this->_deleteLogs();
|
|
|
|
|
|
|
|
CakeLog::write('transactions', 'transaction message', 'orders');
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'shops.log'));
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'transactions.log'));
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'error.log'));
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'debug.log'));
|
|
|
|
|
|
|
|
$this->_deleteLogs();
|
|
|
|
|
|
|
|
CakeLog::write('error', 'error message', 'orders');
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'error.log'));
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'debug.log'));
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'shops.log'));
|
|
|
|
|
|
|
|
$this->_deleteLogs();
|
|
|
|
|
|
|
|
CakeLog::write('orders', 'order message', 'transactions');
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'error.log'));
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'debug.log'));
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'orders.log'));
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'shops.log'));
|
|
|
|
|
|
|
|
$this->_deleteLogs();
|
|
|
|
|
|
|
|
CakeLog::write('warning', 'warning message', 'orders');
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'error.log'));
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'shops.log'));
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'debug.log'));
|
|
|
|
|
|
|
|
$this->_deleteLogs();
|
|
|
|
|
|
|
|
CakeLog::drop('shops');
|
|
|
|
}
|
|
|
|
|
2012-05-15 03:38:46 +00:00
|
|
|
/**
|
|
|
|
* test bogus type and scope
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testBogusTypeAndScope() {
|
|
|
|
$this->_resetLogConfig();
|
|
|
|
$this->_deleteLogs();
|
|
|
|
|
|
|
|
CakeLog::write('bogus', 'bogus message');
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'bogus.log'));
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'error.log'));
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'debug.log'));
|
|
|
|
$this->_deleteLogs();
|
|
|
|
|
|
|
|
CakeLog::write('bogus', 'bogus message', 'bogus');
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'bogus.log'));
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'error.log'));
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'debug.log'));
|
|
|
|
$this->_deleteLogs();
|
|
|
|
|
|
|
|
CakeLog::write('error', 'bogus message', 'bogus');
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'bogus.log'));
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'error.log'));
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'debug.log'));
|
|
|
|
$this->_deleteLogs();
|
|
|
|
}
|
|
|
|
|
2012-05-11 11:31:55 +00:00
|
|
|
/**
|
|
|
|
* test scoped logging with convenience methods
|
|
|
|
*/
|
|
|
|
public function testConvenienceScopedLogging() {
|
|
|
|
if (file_exists(LOGS . 'shops.log')) {
|
|
|
|
unlink(LOGS . 'shops.log');
|
|
|
|
}
|
|
|
|
if (file_exists(LOGS . 'error.log')) {
|
|
|
|
unlink(LOGS . 'error.log');
|
|
|
|
}
|
|
|
|
if (file_exists(LOGS . 'debug.log')) {
|
|
|
|
unlink(LOGS . 'debug.log');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_resetLogConfig();
|
|
|
|
CakeLog::config('shops', array(
|
|
|
|
'engine' => 'FileLog',
|
2012-07-22 00:59:05 +00:00
|
|
|
'types' => array('info', 'debug', 'notice', 'warning'),
|
2012-05-11 11:31:55 +00:00
|
|
|
'scopes' => array('transactions', 'orders'),
|
|
|
|
'file' => 'shops',
|
2012-07-22 00:59:05 +00:00
|
|
|
));
|
2012-05-11 11:31:55 +00:00
|
|
|
|
|
|
|
CakeLog::info('info message', 'transactions');
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'error.log'));
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'shops.log'));
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'debug.log'));
|
|
|
|
|
|
|
|
$this->_deleteLogs();
|
|
|
|
|
|
|
|
CakeLog::error('error message', 'orders');
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'error.log'));
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'debug.log'));
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'shops.log'));
|
|
|
|
|
|
|
|
$this->_deleteLogs();
|
|
|
|
|
|
|
|
CakeLog::warning('warning message', 'orders');
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'error.log'));
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'shops.log'));
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'debug.log'));
|
|
|
|
|
|
|
|
$this->_deleteLogs();
|
|
|
|
|
|
|
|
CakeLog::drop('shops');
|
|
|
|
}
|
|
|
|
|
2012-05-10 15:52:52 +00:00
|
|
|
/**
|
|
|
|
* test convenience methods
|
|
|
|
*/
|
|
|
|
public function testConvenienceMethods() {
|
2012-05-11 09:15:15 +00:00
|
|
|
$this->_deleteLogs();
|
2012-05-10 15:52:52 +00:00
|
|
|
|
|
|
|
CakeLog::config('debug', array(
|
|
|
|
'engine' => 'FileLog',
|
|
|
|
'types' => array('notice', 'info', 'debug'),
|
|
|
|
'file' => 'debug',
|
|
|
|
));
|
|
|
|
CakeLog::config('error', array(
|
|
|
|
'engine' => 'FileLog',
|
2012-05-11 11:31:55 +00:00
|
|
|
'types' => array('emergency', 'alert', 'critical', 'error', 'warning'),
|
2012-05-10 15:52:52 +00:00
|
|
|
'file' => 'error',
|
|
|
|
));
|
|
|
|
|
2012-05-11 11:31:55 +00:00
|
|
|
$testMessage = 'emergency message';
|
|
|
|
CakeLog::emergency($testMessage);
|
|
|
|
$contents = file_get_contents(LOGS . 'error.log');
|
2012-07-22 00:59:05 +00:00
|
|
|
$this->assertRegExp('/(Emergency|Critical): ' . $testMessage . '/', $contents);
|
2012-05-11 11:31:55 +00:00
|
|
|
$this->assertFalse(file_exists(LOGS . 'debug.log'));
|
|
|
|
$this->_deleteLogs();
|
|
|
|
|
|
|
|
$testMessage = 'alert message';
|
|
|
|
CakeLog::alert($testMessage);
|
|
|
|
$contents = file_get_contents(LOGS . 'error.log');
|
2012-07-22 00:59:05 +00:00
|
|
|
$this->assertRegExp('/(Alert|Critical): ' . $testMessage . '/', $contents);
|
2012-05-11 11:31:55 +00:00
|
|
|
$this->assertFalse(file_exists(LOGS . 'debug.log'));
|
|
|
|
$this->_deleteLogs();
|
|
|
|
|
|
|
|
$testMessage = 'critical message';
|
|
|
|
CakeLog::critical($testMessage);
|
|
|
|
$contents = file_get_contents(LOGS . 'error.log');
|
|
|
|
$this->assertContains('Critical: ' . $testMessage, $contents);
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'debug.log'));
|
|
|
|
$this->_deleteLogs();
|
|
|
|
|
2012-05-10 15:52:52 +00:00
|
|
|
$testMessage = 'error message';
|
|
|
|
CakeLog::error($testMessage);
|
|
|
|
$contents = file_get_contents(LOGS . 'error.log');
|
2012-05-11 11:31:55 +00:00
|
|
|
$this->assertContains('Error: ' . $testMessage, $contents);
|
2012-05-10 15:52:52 +00:00
|
|
|
$this->assertFalse(file_exists(LOGS . 'debug.log'));
|
|
|
|
$this->_deleteLogs();
|
|
|
|
|
|
|
|
$testMessage = 'warning message';
|
|
|
|
CakeLog::warning($testMessage);
|
|
|
|
$contents = file_get_contents(LOGS . 'error.log');
|
2012-05-11 11:31:55 +00:00
|
|
|
$this->assertContains('Warning: ' . $testMessage, $contents);
|
2012-05-10 15:52:52 +00:00
|
|
|
$this->assertFalse(file_exists(LOGS . 'debug.log'));
|
|
|
|
$this->_deleteLogs();
|
|
|
|
|
2012-05-11 11:31:55 +00:00
|
|
|
$testMessage = 'notice message';
|
|
|
|
CakeLog::notice($testMessage);
|
|
|
|
$contents = file_get_contents(LOGS . 'debug.log');
|
2012-07-22 00:59:05 +00:00
|
|
|
$this->assertRegExp('/(Notice|Debug): ' . $testMessage . '/', $contents);
|
2012-05-11 11:31:55 +00:00
|
|
|
$this->assertFalse(file_exists(LOGS . 'error.log'));
|
|
|
|
$this->_deleteLogs();
|
|
|
|
|
2012-05-10 15:52:52 +00:00
|
|
|
$testMessage = 'info message';
|
|
|
|
CakeLog::info($testMessage);
|
|
|
|
$contents = file_get_contents(LOGS . 'debug.log');
|
2012-07-22 00:59:05 +00:00
|
|
|
$this->assertRegExp('/(Info|Debug): ' . $testMessage . '/', $contents);
|
2012-05-10 15:52:52 +00:00
|
|
|
$this->assertFalse(file_exists(LOGS . 'error.log'));
|
|
|
|
$this->_deleteLogs();
|
|
|
|
|
|
|
|
$testMessage = 'debug message';
|
|
|
|
CakeLog::debug($testMessage);
|
|
|
|
$contents = file_get_contents(LOGS . 'debug.log');
|
2012-05-11 11:31:55 +00:00
|
|
|
$this->assertContains('Debug: ' . $testMessage, $contents);
|
2012-05-10 15:52:52 +00:00
|
|
|
$this->assertFalse(file_exists(LOGS . 'error.log'));
|
|
|
|
$this->_deleteLogs();
|
2012-05-11 11:31:55 +00:00
|
|
|
}
|
2012-05-10 15:52:52 +00:00
|
|
|
|
2012-05-11 11:31:55 +00:00
|
|
|
/**
|
|
|
|
* test levels customization
|
|
|
|
*/
|
|
|
|
public function testLevelCustomization() {
|
|
|
|
$this->skipIf(DIRECTORY_SEPARATOR === '\\', 'Log level tests not supported on Windows.');
|
|
|
|
|
|
|
|
$levels = CakeLog::defaultLevels();
|
|
|
|
$this->assertNotEmpty($levels);
|
|
|
|
$result = array_keys($levels);
|
|
|
|
$this->assertEquals(array(0, 1, 2, 3, 4, 5, 6, 7), $result);
|
|
|
|
|
|
|
|
$levels = CakeLog::levels(array('foo', 'bar'));
|
|
|
|
CakeLog::defaultLevels();
|
|
|
|
$this->assertEquals('foo', $levels[8]);
|
|
|
|
$this->assertEquals('bar', $levels[9]);
|
|
|
|
|
|
|
|
$levels = CakeLog::levels(array(11 => 'spam', 'bar' => 'eggs'));
|
|
|
|
CakeLog::defaultLevels();
|
|
|
|
$this->assertEquals('spam', $levels[8]);
|
|
|
|
$this->assertEquals('eggs', $levels[9]);
|
|
|
|
|
|
|
|
$levels = CakeLog::levels(array(11 => 'spam', 'bar' => 'eggs'), false);
|
|
|
|
CakeLog::defaultLevels();
|
|
|
|
$this->assertEquals(array('spam', 'eggs'), $levels);
|
|
|
|
|
|
|
|
$levels = CakeLog::levels(array('ham', 9 => 'spam', '12' => 'fam'), false);
|
|
|
|
CakeLog::defaultLevels();
|
|
|
|
$this->assertEquals(array('ham', 'spam', 'fam'), $levels);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test writing log files with custom levels
|
|
|
|
*/
|
|
|
|
public function testCustomLevelWrites() {
|
2012-05-10 15:52:52 +00:00
|
|
|
$this->_deleteLogs();
|
2012-05-11 11:31:55 +00:00
|
|
|
$this->_resetLogConfig();
|
|
|
|
|
|
|
|
$levels = CakeLog::levels(array('spam', 'eggs'));
|
|
|
|
|
|
|
|
$testMessage = 'error message';
|
|
|
|
CakeLog::write('error', $testMessage);
|
|
|
|
CakeLog::defaultLevels();
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'error.log'));
|
|
|
|
$contents = file_get_contents(LOGS . 'error.log');
|
|
|
|
$this->assertContains('Error: ' . $testMessage, $contents);
|
|
|
|
|
|
|
|
CakeLog::config('spam', array(
|
|
|
|
'engine' => 'FileLog',
|
|
|
|
'file' => 'spam.log',
|
|
|
|
'types' => 'spam',
|
|
|
|
));
|
|
|
|
CakeLog::config('eggs', array(
|
|
|
|
'engine' => 'FileLog',
|
|
|
|
'file' => 'eggs.log',
|
|
|
|
'types' => array('spam', 'eggs'),
|
|
|
|
));
|
|
|
|
|
|
|
|
$testMessage = 'spam message';
|
|
|
|
CakeLog::write('spam', $testMessage);
|
|
|
|
CakeLog::defaultLevels();
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'spam.log'));
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'eggs.log'));
|
|
|
|
$contents = file_get_contents(LOGS . 'spam.log');
|
|
|
|
$this->assertContains('Spam: ' . $testMessage, $contents);
|
|
|
|
|
|
|
|
$testMessage = 'egg message';
|
|
|
|
CakeLog::write('eggs', $testMessage);
|
|
|
|
CakeLog::defaultLevels();
|
|
|
|
$contents = file_get_contents(LOGS . 'spam.log');
|
|
|
|
$this->assertNotContains('Eggs: ' . $testMessage, $contents);
|
|
|
|
$contents = file_get_contents(LOGS . 'eggs.log');
|
|
|
|
$this->assertContains('Eggs: ' . $testMessage, $contents);
|
|
|
|
|
|
|
|
CakeLog::drop('spam');
|
|
|
|
CakeLog::drop('eggs');
|
2012-05-15 03:38:46 +00:00
|
|
|
|
|
|
|
$this->_deleteLogs();
|
2012-05-10 15:52:52 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|