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
|
|
|
*
|
2012-04-27 02:49:18 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-10-03 16:31:21 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://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
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
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() {
|
2013-06-21 13:04:27 +00:00
|
|
|
CakeLog::config('valid', array('engine' => 'File'));
|
|
|
|
$stream = CakeLog::stream('valid');
|
|
|
|
$this->assertInstanceOf('FileLog', $stream);
|
|
|
|
CakeLog::drop('valid');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test config() with valid key name including the deprecated Log suffix
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidKeyNameLogSuffix() {
|
2012-05-09 08:55:56 +00:00
|
|
|
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() {
|
2013-06-21 13:04:27 +00:00
|
|
|
CakeLog::config('1nv', array('engine' => 'File'));
|
2012-05-09 08:55:56 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
2013-09-13 00:28:25 +00:00
|
|
|
* Test that CakeLog does not auto create logs when no streams are there to listen.
|
2009-11-06 00:16:46 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2013-09-13 00:28:25 +00:00
|
|
|
public function testNoStreamListenting() {
|
2011-05-28 21:08:22 +00:00
|
|
|
if (file_exists(LOGS . 'error.log')) {
|
|
|
|
unlink(LOGS . 'error.log');
|
|
|
|
}
|
2013-09-13 00:28:25 +00:00
|
|
|
$res = CakeLog::write(LOG_WARNING, 'Test warning');
|
|
|
|
$this->assertFalse($res);
|
|
|
|
$this->assertFalse(file_exists(LOGS . 'error.log'));
|
2009-11-04 23:07:39 +00:00
|
|
|
|
2009-11-15 01:42:57 +00:00
|
|
|
$result = CakeLog::configured();
|
2013-09-13 00:28:25 +00:00
|
|
|
$this->assertEquals(array(), $result);
|
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(
|
2013-06-21 13:04:27 +00:00
|
|
|
'engine' => 'File',
|
2009-11-06 00:16:46 +00:00
|
|
|
'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')) {
|
2012-11-14 09:00:15 +00:00
|
|
|
unlink(LOGS . 'error.log');
|
2010-11-15 02:07:23 +00:00
|
|
|
}
|
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
|
2013-01-11 14:06:54 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testDrop() {
|
2009-11-15 01:44:42 +00:00
|
|
|
CakeLog::config('file', array(
|
2013-06-21 13:04:27 +00:00
|
|
|
'engine' => 'File',
|
2009-11-15 01:44:42 +00:00
|
|
|
'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() {
|
2013-09-13 00:28:25 +00:00
|
|
|
CakeLog::config('file', array(
|
|
|
|
'engine' => 'File',
|
|
|
|
'path' => LOGS
|
|
|
|
));
|
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(
|
2013-06-21 13:04:27 +00:00
|
|
|
'engine' => 'File',
|
2012-07-22 00:59:05 +00:00
|
|
|
'types' => 'debug',
|
2012-05-09 08:55:56 +00:00
|
|
|
'file' => 'spam',
|
|
|
|
));
|
|
|
|
CakeLog::config('eggs', array(
|
2013-06-21 13:04:27 +00:00
|
|
|
'engine' => 'File',
|
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
|
2014-04-02 01:02:37 +00:00
|
|
|
* @return void
|
2012-05-09 08:55:56 +00:00
|
|
|
*/
|
|
|
|
public function testStreamEnable() {
|
|
|
|
CakeLog::config('spam', array(
|
2013-06-21 13:04:27 +00:00
|
|
|
'engine' => 'File',
|
2012-05-09 08:55:56 +00:00
|
|
|
'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
|
2014-04-02 01:02:37 +00:00
|
|
|
* @return void
|
2012-05-09 08:55:56 +00:00
|
|
|
*/
|
|
|
|
public function testStreamDisable() {
|
|
|
|
CakeLog::config('spam', array(
|
2013-06-21 13:04:27 +00:00
|
|
|
'engine' => 'File',
|
2012-05-09 08:55:56 +00:00
|
|
|
'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
|
2014-04-02 01:02:37 +00:00
|
|
|
* @return void
|
2012-05-09 08:55:56 +00:00
|
|
|
*/
|
|
|
|
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
|
2014-04-02 01:02:37 +00:00
|
|
|
* @return void
|
2012-05-09 08:55:56 +00:00
|
|
|
*/
|
|
|
|
public function testStreamDisableInvalid() {
|
|
|
|
CakeLog::disable('bogus_stream');
|
|
|
|
}
|
|
|
|
|
2014-04-02 01:02:37 +00:00
|
|
|
/**
|
|
|
|
* resets log config
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-05-11 09:15:15 +00:00
|
|
|
protected function _resetLogConfig() {
|
|
|
|
CakeLog::config('debug', array(
|
2013-06-21 13:04:27 +00:00
|
|
|
'engine' => 'File',
|
2012-05-11 09:15:15 +00:00
|
|
|
'types' => array('notice', 'info', 'debug'),
|
|
|
|
'file' => 'debug',
|
|
|
|
));
|
|
|
|
CakeLog::config('error', array(
|
2013-06-21 13:04:27 +00:00
|
|
|
'engine' => 'File',
|
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',
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2014-04-02 01:02:37 +00:00
|
|
|
/**
|
|
|
|
* delete logs
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
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(
|
2013-06-21 13:04:27 +00:00
|
|
|
'engine' => 'File',
|
2012-05-10 15:40:23 +00:00
|
|
|
'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(
|
2013-06-21 13:04:27 +00:00
|
|
|
'engine' => 'File',
|
2012-10-08 16:57:02 +00:00
|
|
|
'types' => array('info', 'notice', 'warning'),
|
|
|
|
'scopes' => array('transactions', 'orders'),
|
|
|
|
'file' => 'shops.log',
|
|
|
|
));
|
|
|
|
CakeLog::config('eggs', array(
|
2013-06-21 13:04:27 +00:00
|
|
|
'engine' => 'File',
|
2012-10-08 16:57:02 +00:00
|
|
|
'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();
|
2013-02-12 18:40:32 +00:00
|
|
|
|
|
|
|
CakeLog::config('string-scope', array(
|
2013-06-21 13:04:27 +00:00
|
|
|
'engine' => 'File',
|
2013-02-12 18:40:32 +00:00
|
|
|
'types' => array('info', 'notice', 'warning'),
|
|
|
|
'scopes' => 'string-scope',
|
|
|
|
'file' => 'string-scope.log'
|
|
|
|
));
|
|
|
|
CakeLog::write('info', 'info message', 'string-scope');
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'string-scope.log'));
|
|
|
|
|
|
|
|
CakeLog::drop('string-scope');
|
|
|
|
|
2012-05-10 15:40:23 +00:00
|
|
|
CakeLog::config('shops', array(
|
2013-06-21 13:04:27 +00:00
|
|
|
'engine' => 'File',
|
2012-05-10 15:40:23 +00:00
|
|
|
'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
|
|
|
|
*
|
2014-04-02 01:16:03 +00:00
|
|
|
* @return void
|
2012-05-15 03:38:46 +00:00
|
|
|
*/
|
|
|
|
public function testBogusTypeAndScope() {
|
|
|
|
$this->_resetLogConfig();
|
|
|
|
$this->_deleteLogs();
|
|
|
|
|
2013-09-13 00:28:25 +00:00
|
|
|
CakeLog::config('file', array(
|
|
|
|
'engine' => 'File',
|
|
|
|
'path' => LOGS
|
|
|
|
));
|
|
|
|
|
2012-05-15 03:38:46 +00:00
|
|
|
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
|
2014-04-02 01:16:03 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2012-05-11 11:31:55 +00:00
|
|
|
*/
|
|
|
|
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(
|
2013-06-21 13:04:27 +00:00
|
|
|
'engine' => 'File',
|
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
|
2014-04-02 01:02:37 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2012-05-10 15:52:52 +00:00
|
|
|
*/
|
|
|
|
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(
|
2013-06-21 13:04:27 +00:00
|
|
|
'engine' => 'File',
|
2012-05-10 15:52:52 +00:00
|
|
|
'types' => array('notice', 'info', 'debug'),
|
|
|
|
'file' => 'debug',
|
|
|
|
));
|
|
|
|
CakeLog::config('error', array(
|
2013-06-21 13:04:27 +00:00
|
|
|
'engine' => 'File',
|
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
|
2014-04-02 01:02:37 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2012-05-11 11:31:55 +00:00
|
|
|
*/
|
|
|
|
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
|
2014-04-02 01:02:37 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2012-05-11 11:31:55 +00:00
|
|
|
*/
|
|
|
|
public function testCustomLevelWrites() {
|
2012-05-10 15:52:52 +00:00
|
|
|
$this->_deleteLogs();
|
2012-05-11 11:31:55 +00:00
|
|
|
$this->_resetLogConfig();
|
|
|
|
|
2013-01-23 12:45:50 +00:00
|
|
|
CakeLog::levels(array('spam', 'eggs'));
|
2012-05-11 11:31:55 +00:00
|
|
|
|
|
|
|
$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(
|
2013-06-21 13:04:27 +00:00
|
|
|
'engine' => 'File',
|
2012-05-11 11:31:55 +00:00
|
|
|
'file' => 'spam.log',
|
|
|
|
'types' => 'spam',
|
|
|
|
));
|
|
|
|
CakeLog::config('eggs', array(
|
2013-06-21 13:04:27 +00:00
|
|
|
'engine' => 'File',
|
2012-05-11 11:31:55 +00:00
|
|
|
'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
|
|
|
}
|