LogTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. <?php
  2. /**
  3. * CakePHP(tm) <http://book.cakephp.org/3.0/en/development/testing.html>
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 1.2.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Log;
  15. use Cake\Core\Configure;
  16. use Cake\Core\Plugin;
  17. use Cake\Log\Engine\FileLog;
  18. use Cake\Log\Log;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * LogTest class
  22. */
  23. class LogTest extends TestCase
  24. {
  25. public function setUp()
  26. {
  27. parent::setUp();
  28. Log::reset();
  29. }
  30. public function tearDown()
  31. {
  32. parent::tearDown();
  33. Log::reset();
  34. }
  35. /**
  36. * test importing loggers from app/libs and plugins.
  37. *
  38. * @return void
  39. */
  40. public function testImportingLoggers()
  41. {
  42. Configure::write('App.namespace', 'TestApp');
  43. Plugin::load('TestPlugin');
  44. Log::config('libtest', [
  45. 'engine' => 'TestApp'
  46. ]);
  47. Log::config('plugintest', [
  48. 'engine' => 'TestPlugin.TestPlugin'
  49. ]);
  50. $result = Log::engine('libtest');
  51. $this->assertInstanceOf('TestApp\Log\Engine\TestAppLog', $result);
  52. $this->assertContains('libtest', Log::configured());
  53. $result = Log::engine('plugintest');
  54. $this->assertInstanceOf('TestPlugin\Log\Engine\TestPluginLog', $result);
  55. $this->assertContains('libtest', Log::configured());
  56. $this->assertContains('plugintest', Log::configured());
  57. Log::write(LOG_INFO, 'TestPluginLog is not a BaseLog descendant');
  58. Plugin::unload();
  59. }
  60. /**
  61. * test all the errors from failed logger imports
  62. *
  63. * @expectedException \RuntimeException
  64. * @return void
  65. */
  66. public function testImportingLoggerFailure()
  67. {
  68. Log::config('fail', []);
  69. Log::engine('fail');
  70. }
  71. /**
  72. * test config() with valid key name
  73. *
  74. * @return void
  75. */
  76. public function testValidKeyName()
  77. {
  78. Log::config('valid', ['engine' => 'File']);
  79. $stream = Log::engine('valid');
  80. $this->assertInstanceOf('Cake\Log\Engine\FileLog', $stream);
  81. }
  82. /**
  83. * test that loggers have to implement the correct interface.
  84. *
  85. * @expectedException \RuntimeException
  86. * @return void
  87. */
  88. public function testNotImplementingInterface()
  89. {
  90. Log::config('fail', ['engine' => '\stdClass']);
  91. Log::engine('fail');
  92. }
  93. /**
  94. * explicit tests for drop()
  95. *
  96. * @return void
  97. */
  98. public function testDrop()
  99. {
  100. Log::config('file', [
  101. 'engine' => 'File',
  102. 'path' => LOGS
  103. ]);
  104. $result = Log::configured();
  105. $this->assertContains('file', $result);
  106. $this->assertTrue(Log::drop('file'), 'Should be dropped');
  107. $this->assertFalse(Log::drop('file'), 'Already gone');
  108. $result = Log::configured();
  109. $this->assertNotContains('file', $result);
  110. }
  111. /**
  112. * test config() with valid key name
  113. *
  114. * @expectedException \InvalidArgumentException
  115. * @return void
  116. */
  117. public function testInvalidLevel()
  118. {
  119. Log::config('myengine', ['engine' => 'File']);
  120. Log::write('invalid', 'This will not be logged');
  121. }
  122. /**
  123. * Provider for config() tests.
  124. *
  125. * @return array
  126. */
  127. public static function configProvider()
  128. {
  129. return [
  130. 'Array of data using engine key.' => [[
  131. 'engine' => 'File',
  132. 'path' => TMP . 'tests',
  133. ]],
  134. 'Array of data using classname key.' => [[
  135. 'className' => 'File',
  136. 'path' => TMP . 'tests',
  137. ]],
  138. 'Direct instance' => [new FileLog(['path' => LOGS])],
  139. ];
  140. }
  141. /**
  142. * Test the various config call signatures.
  143. *
  144. * @dataProvider configProvider
  145. * @return void
  146. */
  147. public function testConfigVariants($settings)
  148. {
  149. Log::config('test', $settings);
  150. $this->assertContains('test', Log::configured());
  151. $this->assertInstanceOf('Cake\Log\Engine\FileLog', Log::engine('test'));
  152. Log::drop('test');
  153. }
  154. /**
  155. * Test the various setConfig call signatures.
  156. *
  157. * @dataProvider configProvider
  158. * @return void
  159. */
  160. public function testSetConfigVariants($settings)
  161. {
  162. Log::setConfig('test', $settings);
  163. $this->assertContains('test', Log::configured());
  164. $this->assertInstanceOf('Cake\Log\Engine\FileLog', Log::engine('test'));
  165. Log::drop('test');
  166. }
  167. /**
  168. * Test that config() throws an exception when adding an
  169. * adapter with the wrong type.
  170. *
  171. * @expectedException \RuntimeException
  172. * @return void
  173. */
  174. public function testConfigInjectErrorOnWrongType()
  175. {
  176. Log::config('test', new \StdClass);
  177. Log::info('testing');
  178. }
  179. /**
  180. * Test that setConfig() throws an exception when adding an
  181. * adapter with the wrong type.
  182. *
  183. * @expectedException \RuntimeException
  184. * @return void
  185. */
  186. public function testSetConfigInjectErrorOnWrongType()
  187. {
  188. Log::setConfig('test', new \StdClass);
  189. Log::info('testing');
  190. }
  191. /**
  192. * Test that config() can read data back
  193. *
  194. * @return void
  195. */
  196. public function testConfigRead()
  197. {
  198. $config = [
  199. 'engine' => 'File',
  200. 'path' => LOGS
  201. ];
  202. Log::config('tests', $config);
  203. $expected = $config;
  204. $expected['className'] = $config['engine'];
  205. unset($expected['engine']);
  206. $this->assertSame($expected, Log::config('tests'));
  207. }
  208. /**
  209. * Ensure you cannot reconfigure a log adapter.
  210. *
  211. * @expectedException \BadMethodCallException
  212. * @return void
  213. */
  214. public function testConfigErrorOnReconfigure()
  215. {
  216. Log::config('tests', ['engine' => 'File', 'path' => TMP]);
  217. Log::config('tests', ['engine' => 'Apc']);
  218. }
  219. /**
  220. * testLogFileWriting method
  221. *
  222. * @return void
  223. */
  224. public function testLogFileWriting()
  225. {
  226. $this->_resetLogConfig();
  227. if (file_exists(LOGS . 'error.log')) {
  228. unlink(LOGS . 'error.log');
  229. }
  230. $result = Log::write(LOG_WARNING, 'Test warning');
  231. $this->assertTrue($result);
  232. $this->assertFileExists(LOGS . 'error.log');
  233. unlink(LOGS . 'error.log');
  234. Log::write(LOG_WARNING, 'Test warning 1');
  235. Log::write(LOG_WARNING, 'Test warning 2');
  236. $result = file_get_contents(LOGS . 'error.log');
  237. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 1/', $result);
  238. $this->assertRegExp('/2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 2$/', $result);
  239. unlink(LOGS . 'error.log');
  240. }
  241. /**
  242. * test selective logging by level/type
  243. *
  244. * @return void
  245. */
  246. public function testSelectiveLoggingByLevel()
  247. {
  248. if (file_exists(LOGS . 'spam.log')) {
  249. unlink(LOGS . 'spam.log');
  250. }
  251. if (file_exists(LOGS . 'eggs.log')) {
  252. unlink(LOGS . 'eggs.log');
  253. }
  254. Log::config('spam', [
  255. 'engine' => 'File',
  256. 'path' => LOGS,
  257. 'levels' => 'debug',
  258. 'file' => 'spam',
  259. ]);
  260. Log::config('eggs', [
  261. 'engine' => 'File',
  262. 'path' => LOGS,
  263. 'levels' => ['eggs', 'debug', 'error', 'warning'],
  264. 'file' => 'eggs',
  265. ]);
  266. $testMessage = 'selective logging';
  267. Log::write('warning', $testMessage);
  268. $this->assertFileExists(LOGS . 'eggs.log');
  269. $this->assertFileNotExists(LOGS . 'spam.log');
  270. Log::write('debug', $testMessage);
  271. $this->assertFileExists(LOGS . 'spam.log');
  272. $contents = file_get_contents(LOGS . 'spam.log');
  273. $this->assertContains('Debug: ' . $testMessage, $contents);
  274. $contents = file_get_contents(LOGS . 'eggs.log');
  275. $this->assertContains('Debug: ' . $testMessage, $contents);
  276. if (file_exists(LOGS . 'spam.log')) {
  277. unlink(LOGS . 'spam.log');
  278. }
  279. if (file_exists(LOGS . 'eggs.log')) {
  280. unlink(LOGS . 'eggs.log');
  281. }
  282. }
  283. /**
  284. * test selective logging by level using the `types` attribute
  285. *
  286. * @return void
  287. */
  288. public function testSelectiveLoggingByLevelUsingTypes()
  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. Log::config('spam', [
  297. 'engine' => 'File',
  298. 'path' => LOGS,
  299. 'types' => 'debug',
  300. 'file' => 'spam',
  301. ]);
  302. Log::config('eggs', [
  303. 'engine' => 'File',
  304. 'path' => LOGS,
  305. 'types' => ['eggs', 'debug', 'error', 'warning'],
  306. 'file' => 'eggs',
  307. ]);
  308. $testMessage = 'selective logging';
  309. Log::write('warning', $testMessage);
  310. $this->assertFileExists(LOGS . 'eggs.log');
  311. $this->assertFileNotExists(LOGS . 'spam.log');
  312. Log::write('debug', $testMessage);
  313. $this->assertFileExists(LOGS . 'spam.log');
  314. $contents = file_get_contents(LOGS . 'spam.log');
  315. $this->assertContains('Debug: ' . $testMessage, $contents);
  316. $contents = file_get_contents(LOGS . 'eggs.log');
  317. $this->assertContains('Debug: ' . $testMessage, $contents);
  318. if (file_exists(LOGS . 'spam.log')) {
  319. unlink(LOGS . 'spam.log');
  320. }
  321. if (file_exists(LOGS . 'eggs.log')) {
  322. unlink(LOGS . 'eggs.log');
  323. }
  324. }
  325. protected function _resetLogConfig()
  326. {
  327. Log::config('debug', [
  328. 'engine' => 'File',
  329. 'path' => LOGS,
  330. 'levels' => ['notice', 'info', 'debug'],
  331. 'file' => 'debug',
  332. ]);
  333. Log::config('error', [
  334. 'engine' => 'File',
  335. 'path' => LOGS,
  336. 'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
  337. 'file' => 'error',
  338. ]);
  339. }
  340. protected function _deleteLogs()
  341. {
  342. if (file_exists(LOGS . 'shops.log')) {
  343. unlink(LOGS . 'shops.log');
  344. }
  345. if (file_exists(LOGS . 'error.log')) {
  346. unlink(LOGS . 'error.log');
  347. }
  348. if (file_exists(LOGS . 'debug.log')) {
  349. unlink(LOGS . 'debug.log');
  350. }
  351. if (file_exists(LOGS . 'bogus.log')) {
  352. unlink(LOGS . 'bogus.log');
  353. }
  354. if (file_exists(LOGS . 'spam.log')) {
  355. unlink(LOGS . 'spam.log');
  356. }
  357. if (file_exists(LOGS . 'eggs.log')) {
  358. unlink(LOGS . 'eggs.log');
  359. }
  360. }
  361. /**
  362. * test scoped logging
  363. *
  364. * @return void
  365. */
  366. public function testScopedLogging()
  367. {
  368. $this->_deleteLogs();
  369. $this->_resetLogConfig();
  370. Log::config('shops', [
  371. 'engine' => 'File',
  372. 'path' => LOGS,
  373. 'levels' => ['info', 'debug', 'warning'],
  374. 'scopes' => ['transactions', 'orders'],
  375. 'file' => 'shops',
  376. ]);
  377. Log::write('debug', 'debug message', 'transactions');
  378. $this->assertFileNotExists(LOGS . 'error.log');
  379. $this->assertFileExists(LOGS . 'shops.log');
  380. $this->assertFileExists(LOGS . 'debug.log');
  381. $this->_deleteLogs();
  382. Log::write('warning', 'warning message', 'orders');
  383. $this->assertFileExists(LOGS . 'error.log');
  384. $this->assertFileExists(LOGS . 'shops.log');
  385. $this->assertFileNotExists(LOGS . 'debug.log');
  386. $this->_deleteLogs();
  387. Log::write('error', 'error message', ['scope' => 'orders']);
  388. $this->assertFileExists(LOGS . 'error.log');
  389. $this->assertFileNotExists(LOGS . 'debug.log');
  390. $this->assertFileNotExists(LOGS . 'shops.log');
  391. $this->_deleteLogs();
  392. Log::drop('shops');
  393. }
  394. /**
  395. * Test scoped logging without the default loggers catching everything
  396. *
  397. * @return void
  398. */
  399. public function testScopedLoggingStrict()
  400. {
  401. $this->_deleteLogs();
  402. Log::config('debug', [
  403. 'engine' => 'File',
  404. 'path' => LOGS,
  405. 'levels' => ['notice', 'info', 'debug'],
  406. 'file' => 'debug',
  407. 'scopes' => false
  408. ]);
  409. Log::config('shops', [
  410. 'engine' => 'File',
  411. 'path' => LOGS,
  412. 'levels' => ['info', 'debug', 'warning'],
  413. 'file' => 'shops',
  414. 'scopes' => ['transactions', 'orders'],
  415. ]);
  416. Log::write('debug', 'debug message');
  417. $this->assertFileNotExists(LOGS . 'shops.log');
  418. $this->assertFileExists(LOGS . 'debug.log');
  419. $this->_deleteLogs();
  420. Log::write('debug', 'debug message', 'orders');
  421. $this->assertFileExists(LOGS . 'shops.log');
  422. $this->assertFileNotExists(LOGS . 'debug.log');
  423. $this->_deleteLogs();
  424. Log::drop('shops');
  425. }
  426. /**
  427. * test scoped logging with convenience methods
  428. */
  429. public function testConvenienceScopedLogging()
  430. {
  431. if (file_exists(LOGS . 'shops.log')) {
  432. unlink(LOGS . 'shops.log');
  433. }
  434. if (file_exists(LOGS . 'error.log')) {
  435. unlink(LOGS . 'error.log');
  436. }
  437. if (file_exists(LOGS . 'debug.log')) {
  438. unlink(LOGS . 'debug.log');
  439. }
  440. $this->_resetLogConfig();
  441. Log::config('shops', [
  442. 'engine' => 'File',
  443. 'path' => LOGS,
  444. 'levels' => ['info', 'debug', 'notice', 'warning'],
  445. 'scopes' => ['transactions', 'orders'],
  446. 'file' => 'shops',
  447. ]);
  448. Log::info('info message', 'transactions');
  449. $this->assertFileNotExists(LOGS . 'error.log');
  450. $this->assertFileExists(LOGS . 'shops.log');
  451. $this->assertFileExists(LOGS . 'debug.log');
  452. $this->_deleteLogs();
  453. Log::error('error message', 'orders');
  454. $this->assertFileExists(LOGS . 'error.log');
  455. $this->assertFileNotExists(LOGS . 'debug.log');
  456. $this->assertFileNotExists(LOGS . 'shops.log');
  457. $this->_deleteLogs();
  458. Log::warning('warning message', 'orders');
  459. $this->assertFileExists(LOGS . 'error.log');
  460. $this->assertFileExists(LOGS . 'shops.log');
  461. $this->assertFileNotExists(LOGS . 'debug.log');
  462. $this->_deleteLogs();
  463. Log::drop('shops');
  464. }
  465. /**
  466. * Test that scopes are exclusive and don't bleed.
  467. *
  468. * @return void
  469. */
  470. public function testScopedLoggingExclusive()
  471. {
  472. $this->_deleteLogs();
  473. Log::config('shops', [
  474. 'engine' => 'File',
  475. 'path' => LOGS,
  476. 'levels' => ['debug', 'notice', 'warning'],
  477. 'scopes' => ['transactions', 'orders'],
  478. 'file' => 'shops.log',
  479. ]);
  480. Log::config('eggs', [
  481. 'engine' => 'File',
  482. 'path' => LOGS,
  483. 'levels' => ['debug', 'notice', 'warning'],
  484. 'scopes' => ['eggs'],
  485. 'file' => 'eggs.log',
  486. ]);
  487. Log::write('debug', 'transactions message', 'transactions');
  488. $this->assertFileNotExists(LOGS . 'eggs.log');
  489. $this->assertFileExists(LOGS . 'shops.log');
  490. $this->_deleteLogs();
  491. Log::write('debug', 'eggs message', ['scope' => ['eggs']]);
  492. $this->assertFileExists(LOGS . 'eggs.log');
  493. $this->assertFileNotExists(LOGS . 'shops.log');
  494. }
  495. /**
  496. * testPassingScopeToEngine method
  497. */
  498. public function testPassingScopeToEngine()
  499. {
  500. Configure::write('App.namespace', 'TestApp');
  501. Log::reset();
  502. Log::config('scope_test', [
  503. 'engine' => 'TestApp',
  504. 'path' => LOGS,
  505. 'levels' => ['notice', 'info', 'debug'],
  506. 'scopes' => ['foo', 'bar'],
  507. ]);
  508. $engine = Log::engine('scope_test');
  509. $this->assertNull($engine->passedScope);
  510. Log::write('debug', 'test message', 'foo');
  511. $this->assertEquals(['scope' => ['foo']], $engine->passedScope);
  512. Log::write('debug', 'test message', ['foo', 'bar']);
  513. $this->assertEquals(['scope' => ['foo', 'bar']], $engine->passedScope);
  514. $result = Log::write('debug', 'test message');
  515. $this->assertFalse($result);
  516. }
  517. /**
  518. * test convenience methods
  519. */
  520. public function testConvenienceMethods()
  521. {
  522. $this->_deleteLogs();
  523. Log::config('debug', [
  524. 'engine' => 'File',
  525. 'path' => LOGS,
  526. 'levels' => ['notice', 'info', 'debug'],
  527. 'file' => 'debug',
  528. ]);
  529. Log::config('error', [
  530. 'engine' => 'File',
  531. 'path' => LOGS,
  532. 'levels' => ['emergency', 'alert', 'critical', 'error', 'warning'],
  533. 'file' => 'error',
  534. ]);
  535. $testMessage = 'emergency message';
  536. Log::emergency($testMessage);
  537. $contents = file_get_contents(LOGS . 'error.log');
  538. $this->assertRegExp('/(Emergency|Critical): ' . $testMessage . '/', $contents);
  539. $this->assertFileNotExists(LOGS . 'debug.log');
  540. $this->_deleteLogs();
  541. $testMessage = 'alert message';
  542. Log::alert($testMessage);
  543. $contents = file_get_contents(LOGS . 'error.log');
  544. $this->assertRegExp('/(Alert|Critical): ' . $testMessage . '/', $contents);
  545. $this->assertFileNotExists(LOGS . 'debug.log');
  546. $this->_deleteLogs();
  547. $testMessage = 'critical message';
  548. Log::critical($testMessage);
  549. $contents = file_get_contents(LOGS . 'error.log');
  550. $this->assertContains('Critical: ' . $testMessage, $contents);
  551. $this->assertFileNotExists(LOGS . 'debug.log');
  552. $this->_deleteLogs();
  553. $testMessage = 'error message';
  554. Log::error($testMessage);
  555. $contents = file_get_contents(LOGS . 'error.log');
  556. $this->assertContains('Error: ' . $testMessage, $contents);
  557. $this->assertFileNotExists(LOGS . 'debug.log');
  558. $this->_deleteLogs();
  559. $testMessage = 'warning message';
  560. Log::warning($testMessage);
  561. $contents = file_get_contents(LOGS . 'error.log');
  562. $this->assertContains('Warning: ' . $testMessage, $contents);
  563. $this->assertFileNotExists(LOGS . 'debug.log');
  564. $this->_deleteLogs();
  565. $testMessage = 'notice message';
  566. Log::notice($testMessage);
  567. $contents = file_get_contents(LOGS . 'debug.log');
  568. $this->assertRegExp('/(Notice|Debug): ' . $testMessage . '/', $contents);
  569. $this->assertFileNotExists(LOGS . 'error.log');
  570. $this->_deleteLogs();
  571. $testMessage = 'info message';
  572. Log::info($testMessage);
  573. $contents = file_get_contents(LOGS . 'debug.log');
  574. $this->assertRegExp('/(Info|Debug): ' . $testMessage . '/', $contents);
  575. $this->assertFileNotExists(LOGS . 'error.log');
  576. $this->_deleteLogs();
  577. $testMessage = 'debug message';
  578. Log::debug($testMessage);
  579. $contents = file_get_contents(LOGS . 'debug.log');
  580. $this->assertContains('Debug: ' . $testMessage, $contents);
  581. $this->assertFileNotExists(LOGS . 'error.log');
  582. $this->_deleteLogs();
  583. }
  584. /**
  585. * Test that write() returns false on an unhandled message.
  586. *
  587. * @return void
  588. */
  589. public function testWriteUnhandled()
  590. {
  591. Log::drop('error');
  592. Log::drop('debug');
  593. $result = Log::write('error', 'Bad stuff', 'impossible');
  594. $this->assertFalse($result);
  595. }
  596. /**
  597. * Tests using a callable for creating a Log engine
  598. *
  599. * @return void
  600. */
  601. public function testCreateLoggerWithCallable()
  602. {
  603. $instance = new FileLog;
  604. Log::config('default', function ($alias) use ($instance) {
  605. $this->assertEquals('default', $alias);
  606. return $instance;
  607. });
  608. $this->assertSame($instance, Log::engine('default'));
  609. }
  610. }