PageRenderTime 63ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/vgarcias/expediamedicus
PHP | 694 lines | 482 code | 88 blank | 124 comment | 16 complexity | 9cc1e48a6d39356a62de5554d9931cd8 MD5 | raw file
  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. public function testScopedLoggingExclusive() {
  339. $this->_deleteLogs();
  340. CakeLog::config('shops', array(
  341. 'engine' => 'FileLog',
  342. 'types' => array('info', 'notice', 'warning'),
  343. 'scopes' => array('transactions', 'orders'),
  344. 'file' => 'shops.log',
  345. ));
  346. CakeLog::config('eggs', array(
  347. 'engine' => 'FileLog',
  348. 'types' => array('info', 'notice', 'warning'),
  349. 'scopes' => array('eggs'),
  350. 'file' => 'eggs.log',
  351. ));
  352. CakeLog::write('info', 'transactions message', 'transactions');
  353. $this->assertFalse(file_exists(LOGS . 'eggs.log'));
  354. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  355. $this->_deleteLogs();
  356. CakeLog::write('info', 'eggs message', 'eggs');
  357. $this->assertTrue(file_exists(LOGS . 'eggs.log'));
  358. $this->assertFalse(file_exists(LOGS . 'shops.log'));
  359. }
  360. /**
  361. * test scoped logging
  362. *
  363. * @return void
  364. */
  365. public function testScopedLogging() {
  366. $this->_resetLogConfig();
  367. $this->_deleteLogs();
  368. CakeLog::config('shops', array(
  369. 'engine' => 'FileLog',
  370. 'types' => array('info', 'notice', 'warning'),
  371. 'scopes' => array('transactions', 'orders'),
  372. 'file' => 'shops.log',
  373. ));
  374. CakeLog::write('info', 'info message', 'transactions');
  375. $this->assertFalse(file_exists(LOGS . 'error.log'));
  376. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  377. $this->assertTrue(file_exists(LOGS . 'debug.log'));
  378. $this->_deleteLogs();
  379. CakeLog::write('transactions', 'transaction message', 'orders');
  380. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  381. $this->assertFalse(file_exists(LOGS . 'transactions.log'));
  382. $this->assertFalse(file_exists(LOGS . 'error.log'));
  383. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  384. $this->_deleteLogs();
  385. CakeLog::write('error', 'error message', 'orders');
  386. $this->assertTrue(file_exists(LOGS . 'error.log'));
  387. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  388. $this->assertFalse(file_exists(LOGS . 'shops.log'));
  389. $this->_deleteLogs();
  390. CakeLog::write('orders', 'order message', 'transactions');
  391. $this->assertFalse(file_exists(LOGS . 'error.log'));
  392. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  393. $this->assertFalse(file_exists(LOGS . 'orders.log'));
  394. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  395. $this->_deleteLogs();
  396. CakeLog::write('warning', 'warning message', 'orders');
  397. $this->assertTrue(file_exists(LOGS . 'error.log'));
  398. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  399. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  400. $this->_deleteLogs();
  401. CakeLog::drop('shops');
  402. }
  403. /**
  404. * test bogus type and scope
  405. *
  406. */
  407. public function testBogusTypeAndScope() {
  408. $this->_resetLogConfig();
  409. $this->_deleteLogs();
  410. CakeLog::write('bogus', 'bogus message');
  411. $this->assertTrue(file_exists(LOGS . 'bogus.log'));
  412. $this->assertFalse(file_exists(LOGS . 'error.log'));
  413. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  414. $this->_deleteLogs();
  415. CakeLog::write('bogus', 'bogus message', 'bogus');
  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('error', 'bogus message', 'bogus');
  421. $this->assertFalse(file_exists(LOGS . 'bogus.log'));
  422. $this->assertTrue(file_exists(LOGS . 'error.log'));
  423. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  424. $this->_deleteLogs();
  425. }
  426. /**
  427. * test scoped logging with convenience methods
  428. */
  429. public function testConvenienceScopedLogging() {
  430. if (file_exists(LOGS . 'shops.log')) {
  431. unlink(LOGS . 'shops.log');
  432. }
  433. if (file_exists(LOGS . 'error.log')) {
  434. unlink(LOGS . 'error.log');
  435. }
  436. if (file_exists(LOGS . 'debug.log')) {
  437. unlink(LOGS . 'debug.log');
  438. }
  439. $this->_resetLogConfig();
  440. CakeLog::config('shops', array(
  441. 'engine' => 'FileLog',
  442. 'types' => array('info', 'debug', 'notice', 'warning'),
  443. 'scopes' => array('transactions', 'orders'),
  444. 'file' => 'shops',
  445. ));
  446. CakeLog::info('info message', 'transactions');
  447. $this->assertFalse(file_exists(LOGS . 'error.log'));
  448. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  449. $this->assertTrue(file_exists(LOGS . 'debug.log'));
  450. $this->_deleteLogs();
  451. CakeLog::error('error message', 'orders');
  452. $this->assertTrue(file_exists(LOGS . 'error.log'));
  453. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  454. $this->assertFalse(file_exists(LOGS . 'shops.log'));
  455. $this->_deleteLogs();
  456. CakeLog::warning('warning message', 'orders');
  457. $this->assertTrue(file_exists(LOGS . 'error.log'));
  458. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  459. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  460. $this->_deleteLogs();
  461. CakeLog::drop('shops');
  462. }
  463. /**
  464. * test convenience methods
  465. */
  466. public function testConvenienceMethods() {
  467. $this->_deleteLogs();
  468. CakeLog::config('debug', array(
  469. 'engine' => 'FileLog',
  470. 'types' => array('notice', 'info', 'debug'),
  471. 'file' => 'debug',
  472. ));
  473. CakeLog::config('error', array(
  474. 'engine' => 'FileLog',
  475. 'types' => array('emergency', 'alert', 'critical', 'error', 'warning'),
  476. 'file' => 'error',
  477. ));
  478. $testMessage = 'emergency message';
  479. CakeLog::emergency($testMessage);
  480. $contents = file_get_contents(LOGS . 'error.log');
  481. $this->assertRegExp('/(Emergency|Critical): ' . $testMessage . '/', $contents);
  482. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  483. $this->_deleteLogs();
  484. $testMessage = 'alert message';
  485. CakeLog::alert($testMessage);
  486. $contents = file_get_contents(LOGS . 'error.log');
  487. $this->assertRegExp('/(Alert|Critical): ' . $testMessage . '/', $contents);
  488. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  489. $this->_deleteLogs();
  490. $testMessage = 'critical message';
  491. CakeLog::critical($testMessage);
  492. $contents = file_get_contents(LOGS . 'error.log');
  493. $this->assertContains('Critical: ' . $testMessage, $contents);
  494. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  495. $this->_deleteLogs();
  496. $testMessage = 'error message';
  497. CakeLog::error($testMessage);
  498. $contents = file_get_contents(LOGS . 'error.log');
  499. $this->assertContains('Error: ' . $testMessage, $contents);
  500. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  501. $this->_deleteLogs();
  502. $testMessage = 'warning message';
  503. CakeLog::warning($testMessage);
  504. $contents = file_get_contents(LOGS . 'error.log');
  505. $this->assertContains('Warning: ' . $testMessage, $contents);
  506. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  507. $this->_deleteLogs();
  508. $testMessage = 'notice message';
  509. CakeLog::notice($testMessage);
  510. $contents = file_get_contents(LOGS . 'debug.log');
  511. $this->assertRegExp('/(Notice|Debug): ' . $testMessage . '/', $contents);
  512. $this->assertFalse(file_exists(LOGS . 'error.log'));
  513. $this->_deleteLogs();
  514. $testMessage = 'info message';
  515. CakeLog::info($testMessage);
  516. $contents = file_get_contents(LOGS . 'debug.log');
  517. $this->assertRegExp('/(Info|Debug): ' . $testMessage . '/', $contents);
  518. $this->assertFalse(file_exists(LOGS . 'error.log'));
  519. $this->_deleteLogs();
  520. $testMessage = 'debug message';
  521. CakeLog::debug($testMessage);
  522. $contents = file_get_contents(LOGS . 'debug.log');
  523. $this->assertContains('Debug: ' . $testMessage, $contents);
  524. $this->assertFalse(file_exists(LOGS . 'error.log'));
  525. $this->_deleteLogs();
  526. }
  527. /**
  528. * test levels customization
  529. */
  530. public function testLevelCustomization() {
  531. $this->skipIf(DIRECTORY_SEPARATOR === '\\', 'Log level tests not supported on Windows.');
  532. $levels = CakeLog::defaultLevels();
  533. $this->assertNotEmpty($levels);
  534. $result = array_keys($levels);
  535. $this->assertEquals(array(0, 1, 2, 3, 4, 5, 6, 7), $result);
  536. $levels = CakeLog::levels(array('foo', 'bar'));
  537. CakeLog::defaultLevels();
  538. $this->assertEquals('foo', $levels[8]);
  539. $this->assertEquals('bar', $levels[9]);
  540. $levels = CakeLog::levels(array(11 => 'spam', 'bar' => 'eggs'));
  541. CakeLog::defaultLevels();
  542. $this->assertEquals('spam', $levels[8]);
  543. $this->assertEquals('eggs', $levels[9]);
  544. $levels = CakeLog::levels(array(11 => 'spam', 'bar' => 'eggs'), false);
  545. CakeLog::defaultLevels();
  546. $this->assertEquals(array('spam', 'eggs'), $levels);
  547. $levels = CakeLog::levels(array('ham', 9 => 'spam', '12' => 'fam'), false);
  548. CakeLog::defaultLevels();
  549. $this->assertEquals(array('ham', 'spam', 'fam'), $levels);
  550. }
  551. /**
  552. * Test writing log files with custom levels
  553. */
  554. public function testCustomLevelWrites() {
  555. $this->_deleteLogs();
  556. $this->_resetLogConfig();
  557. $levels = CakeLog::levels(array('spam', 'eggs'));
  558. $testMessage = 'error message';
  559. CakeLog::write('error', $testMessage);
  560. CakeLog::defaultLevels();
  561. $this->assertTrue(file_exists(LOGS . 'error.log'));
  562. $contents = file_get_contents(LOGS . 'error.log');
  563. $this->assertContains('Error: ' . $testMessage, $contents);
  564. CakeLog::config('spam', array(
  565. 'engine' => 'FileLog',
  566. 'file' => 'spam.log',
  567. 'types' => 'spam',
  568. ));
  569. CakeLog::config('eggs', array(
  570. 'engine' => 'FileLog',
  571. 'file' => 'eggs.log',
  572. 'types' => array('spam', 'eggs'),
  573. ));
  574. $testMessage = 'spam message';
  575. CakeLog::write('spam', $testMessage);
  576. CakeLog::defaultLevels();
  577. $this->assertTrue(file_exists(LOGS . 'spam.log'));
  578. $this->assertTrue(file_exists(LOGS . 'eggs.log'));
  579. $contents = file_get_contents(LOGS . 'spam.log');
  580. $this->assertContains('Spam: ' . $testMessage, $contents);
  581. $testMessage = 'egg message';
  582. CakeLog::write('eggs', $testMessage);
  583. CakeLog::defaultLevels();
  584. $contents = file_get_contents(LOGS . 'spam.log');
  585. $this->assertNotContains('Eggs: ' . $testMessage, $contents);
  586. $contents = file_get_contents(LOGS . 'eggs.log');
  587. $this->assertContains('Eggs: ' . $testMessage, $contents);
  588. CakeLog::drop('spam');
  589. CakeLog::drop('eggs');
  590. $this->_deleteLogs();
  591. }
  592. }