PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Test/Case/Log/CakeLogTest.php

https://bitbucket.org/linuxdream/agent-j
PHP | 699 lines | 482 code | 88 blank | 129 comment | 16 complexity | 7dde08e2d3900a76764ebb3370315f84 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, Apache-2.0, LGPL-3.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * CakeLogTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Log
  16. * @since CakePHP(tm) v 1.2.0.5432
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('CakeLog', 'Log');
  20. App::uses('FileLog', 'Log/Engine');
  21. /**
  22. * CakeLogTest class
  23. *
  24. * @package Cake.Test.Case.Log
  25. */
  26. class CakeLogTest extends CakeTestCase {
  27. /**
  28. * Start test callback, clears all streams enabled.
  29. *
  30. * @return void
  31. */
  32. public function setUp() {
  33. parent::setUp();
  34. $streams = CakeLog::configured();
  35. foreach ($streams as $stream) {
  36. CakeLog::drop($stream);
  37. }
  38. }
  39. /**
  40. * test importing loggers from app/libs and plugins.
  41. *
  42. * @return void
  43. */
  44. public function testImportingLoggers() {
  45. App::build(array(
  46. 'Lib' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
  47. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  48. ), App::RESET);
  49. CakePlugin::load('TestPlugin');
  50. $result = CakeLog::config('libtest', array(
  51. 'engine' => 'TestAppLog'
  52. ));
  53. $this->assertTrue($result);
  54. $this->assertEquals(CakeLog::configured(), array('libtest'));
  55. $result = CakeLog::config('plugintest', array(
  56. 'engine' => 'TestPlugin.TestPluginLog'
  57. ));
  58. $this->assertTrue($result);
  59. $this->assertEquals(CakeLog::configured(), array('libtest', 'plugintest'));
  60. CakeLog::write(LOG_INFO, 'TestPluginLog is not a BaseLog descendant');
  61. App::build();
  62. CakePlugin::unload();
  63. }
  64. /**
  65. * test all the errors from failed logger imports
  66. *
  67. * @expectedException CakeLogException
  68. * @return void
  69. */
  70. public function testImportingLoggerFailure() {
  71. CakeLog::config('fail', array());
  72. }
  73. /**
  74. * test config() with valid key name
  75. *
  76. * @return void
  77. */
  78. public function testValidKeyName() {
  79. CakeLog::config('valid', array('engine' => 'FileLog'));
  80. $stream = CakeLog::stream('valid');
  81. $this->assertInstanceOf('FileLog', $stream);
  82. CakeLog::drop('valid');
  83. }
  84. /**
  85. * test config() with invalid key name
  86. *
  87. * @expectedException CakeLogException
  88. * @return void
  89. */
  90. public function testInvalidKeyName() {
  91. CakeLog::config('1nv', array('engine' => 'FileLog'));
  92. }
  93. /**
  94. * test that loggers have to implement the correct interface.
  95. *
  96. * @expectedException CakeLogException
  97. * @return void
  98. */
  99. public function testNotImplementingInterface() {
  100. CakeLog::config('fail', array('engine' => 'stdClass'));
  101. }
  102. /**
  103. * Test that CakeLog autoconfigures itself to use a FileLogger with the LOGS dir.
  104. * When no streams are there.
  105. *
  106. * @return void
  107. */
  108. public function testAutoConfig() {
  109. if (file_exists(LOGS . 'error.log')) {
  110. unlink(LOGS . 'error.log');
  111. }
  112. CakeLog::write(LOG_WARNING, 'Test warning');
  113. $this->assertTrue(file_exists(LOGS . 'error.log'));
  114. $result = CakeLog::configured();
  115. $this->assertEquals(array('default'), $result);
  116. $testMessage = 'custom message';
  117. CakeLog::write('custom', $testMessage);
  118. $content = file_get_contents(LOGS . 'custom.log');
  119. $this->assertContains($testMessage, $content);
  120. unlink(LOGS . 'error.log');
  121. unlink(LOGS . 'custom.log');
  122. }
  123. /**
  124. * test configuring log streams
  125. *
  126. * @return void
  127. */
  128. public function testConfig() {
  129. CakeLog::config('file', array(
  130. 'engine' => 'FileLog',
  131. 'path' => LOGS
  132. ));
  133. $result = CakeLog::configured();
  134. $this->assertEquals(array('file'), $result);
  135. if (file_exists(LOGS . 'error.log')) {
  136. unlink(LOGS . 'error.log');
  137. }
  138. CakeLog::write(LOG_WARNING, 'Test warning');
  139. $this->assertTrue(file_exists(LOGS . 'error.log'));
  140. $result = file_get_contents(LOGS . 'error.log');
  141. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning/', $result);
  142. unlink(LOGS . 'error.log');
  143. }
  144. /**
  145. * explicit tests for drop()
  146. *
  147. * @return void
  148. */
  149. public function testDrop() {
  150. CakeLog::config('file', array(
  151. 'engine' => 'FileLog',
  152. 'path' => LOGS
  153. ));
  154. $result = CakeLog::configured();
  155. $this->assertEquals(array('file'), $result);
  156. CakeLog::drop('file');
  157. $result = CakeLog::configured();
  158. $this->assertSame(array(), $result);
  159. }
  160. /**
  161. * testLogFileWriting method
  162. *
  163. * @return void
  164. */
  165. public function testLogFileWriting() {
  166. if (file_exists(LOGS . 'error.log')) {
  167. unlink(LOGS . 'error.log');
  168. }
  169. $result = CakeLog::write(LOG_WARNING, 'Test warning');
  170. $this->assertTrue($result);
  171. $this->assertTrue(file_exists(LOGS . 'error.log'));
  172. unlink(LOGS . 'error.log');
  173. CakeLog::write(LOG_WARNING, 'Test warning 1');
  174. CakeLog::write(LOG_WARNING, 'Test warning 2');
  175. $result = file_get_contents(LOGS . 'error.log');
  176. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 1/', $result);
  177. $this->assertRegExp('/2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 2$/', $result);
  178. unlink(LOGS . 'error.log');
  179. }
  180. /**
  181. * test selective logging by level/type
  182. *
  183. * @return void
  184. */
  185. public function testSelectiveLoggingByLevel() {
  186. if (file_exists(LOGS . 'spam.log')) {
  187. unlink(LOGS . 'spam.log');
  188. }
  189. if (file_exists(LOGS . 'eggs.log')) {
  190. unlink(LOGS . 'eggs.log');
  191. }
  192. CakeLog::config('spam', array(
  193. 'engine' => 'FileLog',
  194. 'types' => 'debug',
  195. 'file' => 'spam',
  196. ));
  197. CakeLog::config('eggs', array(
  198. 'engine' => 'FileLog',
  199. 'types' => array('eggs', 'debug', 'error', 'warning'),
  200. 'file' => 'eggs',
  201. ));
  202. $testMessage = 'selective logging';
  203. CakeLog::write(LOG_WARNING, $testMessage);
  204. $this->assertTrue(file_exists(LOGS . 'eggs.log'));
  205. $this->assertFalse(file_exists(LOGS . 'spam.log'));
  206. CakeLog::write(LOG_DEBUG, $testMessage);
  207. $this->assertTrue(file_exists(LOGS . 'spam.log'));
  208. $contents = file_get_contents(LOGS . 'spam.log');
  209. $this->assertContains('Debug: ' . $testMessage, $contents);
  210. $contents = file_get_contents(LOGS . 'eggs.log');
  211. $this->assertContains('Debug: ' . $testMessage, $contents);
  212. if (file_exists(LOGS . 'spam.log')) {
  213. unlink(LOGS . 'spam.log');
  214. }
  215. if (file_exists(LOGS . 'eggs.log')) {
  216. unlink(LOGS . 'eggs.log');
  217. }
  218. }
  219. /**
  220. * test enable
  221. *
  222. * @expectedException CakeLogException
  223. */
  224. public function testStreamEnable() {
  225. CakeLog::config('spam', array(
  226. 'engine' => 'FileLog',
  227. 'file' => 'spam',
  228. ));
  229. $this->assertTrue(CakeLog::enabled('spam'));
  230. CakeLog::drop('spam');
  231. CakeLog::enable('bogus_stream');
  232. }
  233. /**
  234. * test disable
  235. *
  236. * @expectedException CakeLogException
  237. */
  238. public function testStreamDisable() {
  239. CakeLog::config('spam', array(
  240. 'engine' => 'FileLog',
  241. 'file' => 'spam',
  242. ));
  243. $this->assertTrue(CakeLog::enabled('spam'));
  244. CakeLog::disable('spam');
  245. $this->assertFalse(CakeLog::enabled('spam'));
  246. CakeLog::drop('spam');
  247. CakeLog::enable('bogus_stream');
  248. }
  249. /**
  250. * test enabled() invalid stream
  251. *
  252. * @expectedException CakeLogException
  253. */
  254. public function testStreamEnabledInvalid() {
  255. CakeLog::enabled('bogus_stream');
  256. }
  257. /**
  258. * test disable invalid stream
  259. *
  260. * @expectedException CakeLogException
  261. */
  262. public function testStreamDisableInvalid() {
  263. CakeLog::disable('bogus_stream');
  264. }
  265. protected function _resetLogConfig() {
  266. CakeLog::config('debug', array(
  267. 'engine' => 'FileLog',
  268. 'types' => array('notice', 'info', 'debug'),
  269. 'file' => 'debug',
  270. ));
  271. CakeLog::config('error', array(
  272. 'engine' => 'FileLog',
  273. 'types' => array('warning', 'error', 'critical', 'alert', 'emergency'),
  274. 'file' => 'error',
  275. ));
  276. }
  277. protected function _deleteLogs() {
  278. if (file_exists(LOGS . 'shops.log')) {
  279. unlink(LOGS . 'shops.log');
  280. }
  281. if (file_exists(LOGS . 'error.log')) {
  282. unlink(LOGS . 'error.log');
  283. }
  284. if (file_exists(LOGS . 'debug.log')) {
  285. unlink(LOGS . 'debug.log');
  286. }
  287. if (file_exists(LOGS . 'bogus.log')) {
  288. unlink(LOGS . 'bogus.log');
  289. }
  290. if (file_exists(LOGS . 'spam.log')) {
  291. unlink(LOGS . 'spam.log');
  292. }
  293. if (file_exists(LOGS . 'eggs.log')) {
  294. unlink(LOGS . 'eggs.log');
  295. }
  296. }
  297. /**
  298. * test backward compatible scoped logging
  299. *
  300. * @return void
  301. */
  302. public function testScopedLoggingBC() {
  303. $this->_resetLogConfig();
  304. CakeLog::config('shops', array(
  305. 'engine' => 'FileLog',
  306. 'types' => array('info', 'notice', 'warning'),
  307. 'scopes' => array('transactions', 'orders'),
  308. 'file' => 'shops',
  309. ));
  310. $this->_deleteLogs();
  311. CakeLog::write('info', 'info message');
  312. $this->assertFalse(file_exists(LOGS . 'error.log'));
  313. $this->assertTrue(file_exists(LOGS . 'debug.log'));
  314. $this->_deleteLogs();
  315. CakeLog::write('transactions', 'transaction message');
  316. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  317. $this->assertFalse(file_exists(LOGS . 'transactions.log'));
  318. $this->assertFalse(file_exists(LOGS . 'error.log'));
  319. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  320. $this->_deleteLogs();
  321. CakeLog::write('error', 'error message');
  322. $this->assertTrue(file_exists(LOGS . 'error.log'));
  323. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  324. $this->assertFalse(file_exists(LOGS . 'shops.log'));
  325. $this->_deleteLogs();
  326. CakeLog::write('orders', 'order message');
  327. $this->assertFalse(file_exists(LOGS . 'error.log'));
  328. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  329. $this->assertFalse(file_exists(LOGS . 'orders.log'));
  330. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  331. $this->_deleteLogs();
  332. CakeLog::write('warning', 'warning message');
  333. $this->assertTrue(file_exists(LOGS . 'error.log'));
  334. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  335. $this->_deleteLogs();
  336. CakeLog::drop('shops');
  337. }
  338. /**
  339. * Test that scopes are exclusive and don't bleed.
  340. *
  341. * @return void
  342. */
  343. public function testScopedLoggingExclusive() {
  344. $this->_deleteLogs();
  345. CakeLog::config('shops', array(
  346. 'engine' => 'FileLog',
  347. 'types' => array('info', 'notice', 'warning'),
  348. 'scopes' => array('transactions', 'orders'),
  349. 'file' => 'shops.log',
  350. ));
  351. CakeLog::config('eggs', array(
  352. 'engine' => 'FileLog',
  353. 'types' => array('info', 'notice', 'warning'),
  354. 'scopes' => array('eggs'),
  355. 'file' => 'eggs.log',
  356. ));
  357. CakeLog::write('info', 'transactions message', 'transactions');
  358. $this->assertFalse(file_exists(LOGS . 'eggs.log'));
  359. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  360. $this->_deleteLogs();
  361. CakeLog::write('info', 'eggs message', 'eggs');
  362. $this->assertTrue(file_exists(LOGS . 'eggs.log'));
  363. $this->assertFalse(file_exists(LOGS . 'shops.log'));
  364. }
  365. /**
  366. * test scoped logging
  367. *
  368. * @return void
  369. */
  370. public function testScopedLogging() {
  371. $this->_resetLogConfig();
  372. $this->_deleteLogs();
  373. CakeLog::config('shops', array(
  374. 'engine' => 'FileLog',
  375. 'types' => array('info', 'notice', 'warning'),
  376. 'scopes' => array('transactions', 'orders'),
  377. 'file' => 'shops.log',
  378. ));
  379. CakeLog::write('info', 'info message', 'transactions');
  380. $this->assertFalse(file_exists(LOGS . 'error.log'));
  381. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  382. $this->assertTrue(file_exists(LOGS . 'debug.log'));
  383. $this->_deleteLogs();
  384. CakeLog::write('transactions', 'transaction message', 'orders');
  385. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  386. $this->assertFalse(file_exists(LOGS . 'transactions.log'));
  387. $this->assertFalse(file_exists(LOGS . 'error.log'));
  388. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  389. $this->_deleteLogs();
  390. CakeLog::write('error', 'error message', 'orders');
  391. $this->assertTrue(file_exists(LOGS . 'error.log'));
  392. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  393. $this->assertFalse(file_exists(LOGS . 'shops.log'));
  394. $this->_deleteLogs();
  395. CakeLog::write('orders', 'order message', 'transactions');
  396. $this->assertFalse(file_exists(LOGS . 'error.log'));
  397. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  398. $this->assertFalse(file_exists(LOGS . 'orders.log'));
  399. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  400. $this->_deleteLogs();
  401. CakeLog::write('warning', 'warning message', 'orders');
  402. $this->assertTrue(file_exists(LOGS . 'error.log'));
  403. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  404. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  405. $this->_deleteLogs();
  406. CakeLog::drop('shops');
  407. }
  408. /**
  409. * test bogus type and scope
  410. *
  411. */
  412. public function testBogusTypeAndScope() {
  413. $this->_resetLogConfig();
  414. $this->_deleteLogs();
  415. CakeLog::write('bogus', 'bogus message');
  416. $this->assertTrue(file_exists(LOGS . 'bogus.log'));
  417. $this->assertFalse(file_exists(LOGS . 'error.log'));
  418. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  419. $this->_deleteLogs();
  420. CakeLog::write('bogus', 'bogus message', 'bogus');
  421. $this->assertTrue(file_exists(LOGS . 'bogus.log'));
  422. $this->assertFalse(file_exists(LOGS . 'error.log'));
  423. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  424. $this->_deleteLogs();
  425. CakeLog::write('error', 'bogus message', 'bogus');
  426. $this->assertFalse(file_exists(LOGS . 'bogus.log'));
  427. $this->assertTrue(file_exists(LOGS . 'error.log'));
  428. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  429. $this->_deleteLogs();
  430. }
  431. /**
  432. * test scoped logging with convenience methods
  433. */
  434. public function testConvenienceScopedLogging() {
  435. if (file_exists(LOGS . 'shops.log')) {
  436. unlink(LOGS . 'shops.log');
  437. }
  438. if (file_exists(LOGS . 'error.log')) {
  439. unlink(LOGS . 'error.log');
  440. }
  441. if (file_exists(LOGS . 'debug.log')) {
  442. unlink(LOGS . 'debug.log');
  443. }
  444. $this->_resetLogConfig();
  445. CakeLog::config('shops', array(
  446. 'engine' => 'FileLog',
  447. 'types' => array('info', 'debug', 'notice', 'warning'),
  448. 'scopes' => array('transactions', 'orders'),
  449. 'file' => 'shops',
  450. ));
  451. CakeLog::info('info message', 'transactions');
  452. $this->assertFalse(file_exists(LOGS . 'error.log'));
  453. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  454. $this->assertTrue(file_exists(LOGS . 'debug.log'));
  455. $this->_deleteLogs();
  456. CakeLog::error('error message', 'orders');
  457. $this->assertTrue(file_exists(LOGS . 'error.log'));
  458. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  459. $this->assertFalse(file_exists(LOGS . 'shops.log'));
  460. $this->_deleteLogs();
  461. CakeLog::warning('warning message', 'orders');
  462. $this->assertTrue(file_exists(LOGS . 'error.log'));
  463. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  464. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  465. $this->_deleteLogs();
  466. CakeLog::drop('shops');
  467. }
  468. /**
  469. * test convenience methods
  470. */
  471. public function testConvenienceMethods() {
  472. $this->_deleteLogs();
  473. CakeLog::config('debug', array(
  474. 'engine' => 'FileLog',
  475. 'types' => array('notice', 'info', 'debug'),
  476. 'file' => 'debug',
  477. ));
  478. CakeLog::config('error', array(
  479. 'engine' => 'FileLog',
  480. 'types' => array('emergency', 'alert', 'critical', 'error', 'warning'),
  481. 'file' => 'error',
  482. ));
  483. $testMessage = 'emergency message';
  484. CakeLog::emergency($testMessage);
  485. $contents = file_get_contents(LOGS . 'error.log');
  486. $this->assertRegExp('/(Emergency|Critical): ' . $testMessage . '/', $contents);
  487. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  488. $this->_deleteLogs();
  489. $testMessage = 'alert message';
  490. CakeLog::alert($testMessage);
  491. $contents = file_get_contents(LOGS . 'error.log');
  492. $this->assertRegExp('/(Alert|Critical): ' . $testMessage . '/', $contents);
  493. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  494. $this->_deleteLogs();
  495. $testMessage = 'critical message';
  496. CakeLog::critical($testMessage);
  497. $contents = file_get_contents(LOGS . 'error.log');
  498. $this->assertContains('Critical: ' . $testMessage, $contents);
  499. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  500. $this->_deleteLogs();
  501. $testMessage = 'error message';
  502. CakeLog::error($testMessage);
  503. $contents = file_get_contents(LOGS . 'error.log');
  504. $this->assertContains('Error: ' . $testMessage, $contents);
  505. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  506. $this->_deleteLogs();
  507. $testMessage = 'warning message';
  508. CakeLog::warning($testMessage);
  509. $contents = file_get_contents(LOGS . 'error.log');
  510. $this->assertContains('Warning: ' . $testMessage, $contents);
  511. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  512. $this->_deleteLogs();
  513. $testMessage = 'notice message';
  514. CakeLog::notice($testMessage);
  515. $contents = file_get_contents(LOGS . 'debug.log');
  516. $this->assertRegExp('/(Notice|Debug): ' . $testMessage . '/', $contents);
  517. $this->assertFalse(file_exists(LOGS . 'error.log'));
  518. $this->_deleteLogs();
  519. $testMessage = 'info message';
  520. CakeLog::info($testMessage);
  521. $contents = file_get_contents(LOGS . 'debug.log');
  522. $this->assertRegExp('/(Info|Debug): ' . $testMessage . '/', $contents);
  523. $this->assertFalse(file_exists(LOGS . 'error.log'));
  524. $this->_deleteLogs();
  525. $testMessage = 'debug message';
  526. CakeLog::debug($testMessage);
  527. $contents = file_get_contents(LOGS . 'debug.log');
  528. $this->assertContains('Debug: ' . $testMessage, $contents);
  529. $this->assertFalse(file_exists(LOGS . 'error.log'));
  530. $this->_deleteLogs();
  531. }
  532. /**
  533. * test levels customization
  534. */
  535. public function testLevelCustomization() {
  536. $this->skipIf(DIRECTORY_SEPARATOR === '\\', 'Log level tests not supported on Windows.');
  537. $levels = CakeLog::defaultLevels();
  538. $this->assertNotEmpty($levels);
  539. $result = array_keys($levels);
  540. $this->assertEquals(array(0, 1, 2, 3, 4, 5, 6, 7), $result);
  541. $levels = CakeLog::levels(array('foo', 'bar'));
  542. CakeLog::defaultLevels();
  543. $this->assertEquals('foo', $levels[8]);
  544. $this->assertEquals('bar', $levels[9]);
  545. $levels = CakeLog::levels(array(11 => 'spam', 'bar' => 'eggs'));
  546. CakeLog::defaultLevels();
  547. $this->assertEquals('spam', $levels[8]);
  548. $this->assertEquals('eggs', $levels[9]);
  549. $levels = CakeLog::levels(array(11 => 'spam', 'bar' => 'eggs'), false);
  550. CakeLog::defaultLevels();
  551. $this->assertEquals(array('spam', 'eggs'), $levels);
  552. $levels = CakeLog::levels(array('ham', 9 => 'spam', '12' => 'fam'), false);
  553. CakeLog::defaultLevels();
  554. $this->assertEquals(array('ham', 'spam', 'fam'), $levels);
  555. }
  556. /**
  557. * Test writing log files with custom levels
  558. */
  559. public function testCustomLevelWrites() {
  560. $this->_deleteLogs();
  561. $this->_resetLogConfig();
  562. CakeLog::levels(array('spam', 'eggs'));
  563. $testMessage = 'error message';
  564. CakeLog::write('error', $testMessage);
  565. CakeLog::defaultLevels();
  566. $this->assertTrue(file_exists(LOGS . 'error.log'));
  567. $contents = file_get_contents(LOGS . 'error.log');
  568. $this->assertContains('Error: ' . $testMessage, $contents);
  569. CakeLog::config('spam', array(
  570. 'engine' => 'FileLog',
  571. 'file' => 'spam.log',
  572. 'types' => 'spam',
  573. ));
  574. CakeLog::config('eggs', array(
  575. 'engine' => 'FileLog',
  576. 'file' => 'eggs.log',
  577. 'types' => array('spam', 'eggs'),
  578. ));
  579. $testMessage = 'spam message';
  580. CakeLog::write('spam', $testMessage);
  581. CakeLog::defaultLevels();
  582. $this->assertTrue(file_exists(LOGS . 'spam.log'));
  583. $this->assertTrue(file_exists(LOGS . 'eggs.log'));
  584. $contents = file_get_contents(LOGS . 'spam.log');
  585. $this->assertContains('Spam: ' . $testMessage, $contents);
  586. $testMessage = 'egg message';
  587. CakeLog::write('eggs', $testMessage);
  588. CakeLog::defaultLevels();
  589. $contents = file_get_contents(LOGS . 'spam.log');
  590. $this->assertNotContains('Eggs: ' . $testMessage, $contents);
  591. $contents = file_get_contents(LOGS . 'eggs.log');
  592. $this->assertContains('Eggs: ' . $testMessage, $contents);
  593. CakeLog::drop('spam');
  594. CakeLog::drop('eggs');
  595. $this->_deleteLogs();
  596. }
  597. }