LogTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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 that config() throws an exception when adding an
  156. * adapter with the wrong type.
  157. *
  158. * @expectedException \RuntimeException
  159. * @return void
  160. */
  161. public function testConfigInjectErrorOnWrongType()
  162. {
  163. Log::config('test', new \StdClass);
  164. Log::info('testing');
  165. }
  166. /**
  167. * Test that config() can read data back
  168. *
  169. * @return void
  170. */
  171. public function testConfigRead()
  172. {
  173. $config = [
  174. 'engine' => 'File',
  175. 'path' => LOGS
  176. ];
  177. Log::config('tests', $config);
  178. $expected = $config;
  179. $expected['className'] = $config['engine'];
  180. unset($expected['engine']);
  181. $this->assertSame($expected, Log::config('tests'));
  182. }
  183. /**
  184. * Ensure you cannot reconfigure a log adapter.
  185. *
  186. * @expectedException \BadMethodCallException
  187. * @return void
  188. */
  189. public function testConfigErrorOnReconfigure()
  190. {
  191. Log::config('tests', ['engine' => 'File', 'path' => TMP]);
  192. Log::config('tests', ['engine' => 'Apc']);
  193. }
  194. /**
  195. * testLogFileWriting method
  196. *
  197. * @return void
  198. */
  199. public function testLogFileWriting()
  200. {
  201. $this->_resetLogConfig();
  202. if (file_exists(LOGS . 'error.log')) {
  203. unlink(LOGS . 'error.log');
  204. }
  205. $result = Log::write(LOG_WARNING, 'Test warning');
  206. $this->assertTrue($result);
  207. $this->assertFileExists(LOGS . 'error.log');
  208. unlink(LOGS . 'error.log');
  209. Log::write(LOG_WARNING, 'Test warning 1');
  210. Log::write(LOG_WARNING, 'Test warning 2');
  211. $result = file_get_contents(LOGS . 'error.log');
  212. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 1/', $result);
  213. $this->assertRegExp('/2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 2$/', $result);
  214. unlink(LOGS . 'error.log');
  215. }
  216. /**
  217. * test selective logging by level/type
  218. *
  219. * @return void
  220. */
  221. public function testSelectiveLoggingByLevel()
  222. {
  223. if (file_exists(LOGS . 'spam.log')) {
  224. unlink(LOGS . 'spam.log');
  225. }
  226. if (file_exists(LOGS . 'eggs.log')) {
  227. unlink(LOGS . 'eggs.log');
  228. }
  229. Log::config('spam', [
  230. 'engine' => 'File',
  231. 'path' => LOGS,
  232. 'levels' => 'debug',
  233. 'file' => 'spam',
  234. ]);
  235. Log::config('eggs', [
  236. 'engine' => 'File',
  237. 'path' => LOGS,
  238. 'levels' => ['eggs', 'debug', 'error', 'warning'],
  239. 'file' => 'eggs',
  240. ]);
  241. $testMessage = 'selective logging';
  242. Log::write('warning', $testMessage);
  243. $this->assertFileExists(LOGS . 'eggs.log');
  244. $this->assertFileNotExists(LOGS . 'spam.log');
  245. Log::write('debug', $testMessage);
  246. $this->assertFileExists(LOGS . 'spam.log');
  247. $contents = file_get_contents(LOGS . 'spam.log');
  248. $this->assertContains('Debug: ' . $testMessage, $contents);
  249. $contents = file_get_contents(LOGS . 'eggs.log');
  250. $this->assertContains('Debug: ' . $testMessage, $contents);
  251. if (file_exists(LOGS . 'spam.log')) {
  252. unlink(LOGS . 'spam.log');
  253. }
  254. if (file_exists(LOGS . 'eggs.log')) {
  255. unlink(LOGS . 'eggs.log');
  256. }
  257. }
  258. /**
  259. * test selective logging by level using the `types` attribute
  260. *
  261. * @return void
  262. */
  263. public function testSelectiveLoggingByLevelUsingTypes()
  264. {
  265. if (file_exists(LOGS . 'spam.log')) {
  266. unlink(LOGS . 'spam.log');
  267. }
  268. if (file_exists(LOGS . 'eggs.log')) {
  269. unlink(LOGS . 'eggs.log');
  270. }
  271. Log::config('spam', [
  272. 'engine' => 'File',
  273. 'path' => LOGS,
  274. 'types' => 'debug',
  275. 'file' => 'spam',
  276. ]);
  277. Log::config('eggs', [
  278. 'engine' => 'File',
  279. 'path' => LOGS,
  280. 'types' => ['eggs', 'debug', 'error', 'warning'],
  281. 'file' => 'eggs',
  282. ]);
  283. $testMessage = 'selective logging';
  284. Log::write('warning', $testMessage);
  285. $this->assertFileExists(LOGS . 'eggs.log');
  286. $this->assertFileNotExists(LOGS . 'spam.log');
  287. Log::write('debug', $testMessage);
  288. $this->assertFileExists(LOGS . 'spam.log');
  289. $contents = file_get_contents(LOGS . 'spam.log');
  290. $this->assertContains('Debug: ' . $testMessage, $contents);
  291. $contents = file_get_contents(LOGS . 'eggs.log');
  292. $this->assertContains('Debug: ' . $testMessage, $contents);
  293. if (file_exists(LOGS . 'spam.log')) {
  294. unlink(LOGS . 'spam.log');
  295. }
  296. if (file_exists(LOGS . 'eggs.log')) {
  297. unlink(LOGS . 'eggs.log');
  298. }
  299. }
  300. protected function _resetLogConfig()
  301. {
  302. Log::config('debug', [
  303. 'engine' => 'File',
  304. 'path' => LOGS,
  305. 'levels' => ['notice', 'info', 'debug'],
  306. 'file' => 'debug',
  307. ]);
  308. Log::config('error', [
  309. 'engine' => 'File',
  310. 'path' => LOGS,
  311. 'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
  312. 'file' => 'error',
  313. ]);
  314. }
  315. protected function _deleteLogs()
  316. {
  317. if (file_exists(LOGS . 'shops.log')) {
  318. unlink(LOGS . 'shops.log');
  319. }
  320. if (file_exists(LOGS . 'error.log')) {
  321. unlink(LOGS . 'error.log');
  322. }
  323. if (file_exists(LOGS . 'debug.log')) {
  324. unlink(LOGS . 'debug.log');
  325. }
  326. if (file_exists(LOGS . 'bogus.log')) {
  327. unlink(LOGS . 'bogus.log');
  328. }
  329. if (file_exists(LOGS . 'spam.log')) {
  330. unlink(LOGS . 'spam.log');
  331. }
  332. if (file_exists(LOGS . 'eggs.log')) {
  333. unlink(LOGS . 'eggs.log');
  334. }
  335. }
  336. /**
  337. * test scoped logging
  338. *
  339. * @return void
  340. */
  341. public function testScopedLogging()
  342. {
  343. $this->_deleteLogs();
  344. $this->_resetLogConfig();
  345. Log::config('shops', [
  346. 'engine' => 'File',
  347. 'path' => LOGS,
  348. 'levels' => ['info', 'debug', 'warning'],
  349. 'scopes' => ['transactions', 'orders'],
  350. 'file' => 'shops',
  351. ]);
  352. Log::write('debug', 'debug message', 'transactions');
  353. $this->assertFileNotExists(LOGS . 'error.log');
  354. $this->assertFileExists(LOGS . 'shops.log');
  355. $this->assertFileExists(LOGS . 'debug.log');
  356. $this->_deleteLogs();
  357. Log::write('warning', 'warning message', 'orders');
  358. $this->assertFileExists(LOGS . 'error.log');
  359. $this->assertFileExists(LOGS . 'shops.log');
  360. $this->assertFileNotExists(LOGS . 'debug.log');
  361. $this->_deleteLogs();
  362. Log::write('error', 'error message', 'orders');
  363. $this->assertFileExists(LOGS . 'error.log');
  364. $this->assertFileNotExists(LOGS . 'debug.log');
  365. $this->assertFileNotExists(LOGS . 'shops.log');
  366. $this->_deleteLogs();
  367. Log::drop('shops');
  368. }
  369. /**
  370. * Test scoped logging without the default loggers catching everything
  371. *
  372. * @return void
  373. */
  374. public function testScopedLoggingStrict()
  375. {
  376. $this->_deleteLogs();
  377. Log::config('debug', [
  378. 'engine' => 'File',
  379. 'path' => LOGS,
  380. 'levels' => ['notice', 'info', 'debug'],
  381. 'file' => 'debug',
  382. 'scopes' => false
  383. ]);
  384. Log::config('shops', [
  385. 'engine' => 'File',
  386. 'path' => LOGS,
  387. 'levels' => ['info', 'debug', 'warning'],
  388. 'file' => 'shops',
  389. 'scopes' => ['transactions', 'orders'],
  390. ]);
  391. Log::write('debug', 'debug message');
  392. $this->assertFileNotExists(LOGS . 'shops.log');
  393. $this->assertFileExists(LOGS . 'debug.log');
  394. $this->_deleteLogs();
  395. Log::write('debug', 'debug message', 'orders');
  396. $this->assertFileExists(LOGS . 'shops.log');
  397. $this->assertFileNotExists(LOGS . 'debug.log');
  398. $this->_deleteLogs();
  399. Log::drop('shops');
  400. }
  401. /**
  402. * test scoped logging with convenience methods
  403. */
  404. public function testConvenienceScopedLogging()
  405. {
  406. if (file_exists(LOGS . 'shops.log')) {
  407. unlink(LOGS . 'shops.log');
  408. }
  409. if (file_exists(LOGS . 'error.log')) {
  410. unlink(LOGS . 'error.log');
  411. }
  412. if (file_exists(LOGS . 'debug.log')) {
  413. unlink(LOGS . 'debug.log');
  414. }
  415. $this->_resetLogConfig();
  416. Log::config('shops', [
  417. 'engine' => 'File',
  418. 'path' => LOGS,
  419. 'levels' => ['info', 'debug', 'notice', 'warning'],
  420. 'scopes' => ['transactions', 'orders'],
  421. 'file' => 'shops',
  422. ]);
  423. Log::info('info message', 'transactions');
  424. $this->assertFileNotExists(LOGS . 'error.log');
  425. $this->assertFileExists(LOGS . 'shops.log');
  426. $this->assertFileExists(LOGS . 'debug.log');
  427. $this->_deleteLogs();
  428. Log::error('error message', 'orders');
  429. $this->assertFileExists(LOGS . 'error.log');
  430. $this->assertFileNotExists(LOGS . 'debug.log');
  431. $this->assertFileNotExists(LOGS . 'shops.log');
  432. $this->_deleteLogs();
  433. Log::warning('warning message', 'orders');
  434. $this->assertFileExists(LOGS . 'error.log');
  435. $this->assertFileExists(LOGS . 'shops.log');
  436. $this->assertFileNotExists(LOGS . 'debug.log');
  437. $this->_deleteLogs();
  438. Log::drop('shops');
  439. }
  440. /**
  441. * Test that scopes are exclusive and don't bleed.
  442. *
  443. * @return void
  444. */
  445. public function testScopedLoggingExclusive()
  446. {
  447. $this->_deleteLogs();
  448. Log::config('shops', [
  449. 'engine' => 'File',
  450. 'path' => LOGS,
  451. 'levels' => ['debug', 'notice', 'warning'],
  452. 'scopes' => ['transactions', 'orders'],
  453. 'file' => 'shops.log',
  454. ]);
  455. Log::config('eggs', [
  456. 'engine' => 'File',
  457. 'path' => LOGS,
  458. 'levels' => ['debug', 'notice', 'warning'],
  459. 'scopes' => ['eggs'],
  460. 'file' => 'eggs.log',
  461. ]);
  462. Log::write('debug', 'transactions message', 'transactions');
  463. $this->assertFileNotExists(LOGS . 'eggs.log');
  464. $this->assertFileExists(LOGS . 'shops.log');
  465. $this->_deleteLogs();
  466. Log::write('debug', 'eggs message', 'eggs');
  467. $this->assertFileExists(LOGS . 'eggs.log');
  468. $this->assertFileNotExists(LOGS . 'shops.log');
  469. }
  470. /**
  471. * testPassingScopeToEngine method
  472. */
  473. public function testPassingScopeToEngine()
  474. {
  475. Configure::write('App.namespace', 'TestApp');
  476. Log::reset();
  477. Log::config('scope_test', [
  478. 'engine' => 'TestApp',
  479. 'path' => LOGS,
  480. 'levels' => ['notice', 'info', 'debug'],
  481. 'scopes' => ['foo', 'bar'],
  482. ]);
  483. $engine = Log::engine('scope_test');
  484. $this->assertNull($engine->passedScope);
  485. Log::write('debug', 'test message', 'foo');
  486. $this->assertEquals(['scope' => ['foo']], $engine->passedScope);
  487. Log::write('debug', 'test message', ['foo', 'bar']);
  488. $this->assertEquals(['scope' => ['foo', 'bar']], $engine->passedScope);
  489. $result = Log::write('debug', 'test message');
  490. $this->assertFalse($result);
  491. }
  492. /**
  493. * test convenience methods
  494. */
  495. public function testConvenienceMethods()
  496. {
  497. $this->_deleteLogs();
  498. Log::config('debug', [
  499. 'engine' => 'File',
  500. 'path' => LOGS,
  501. 'levels' => ['notice', 'info', 'debug'],
  502. 'file' => 'debug',
  503. ]);
  504. Log::config('error', [
  505. 'engine' => 'File',
  506. 'path' => LOGS,
  507. 'levels' => ['emergency', 'alert', 'critical', 'error', 'warning'],
  508. 'file' => 'error',
  509. ]);
  510. $testMessage = 'emergency message';
  511. Log::emergency($testMessage);
  512. $contents = file_get_contents(LOGS . 'error.log');
  513. $this->assertRegExp('/(Emergency|Critical): ' . $testMessage . '/', $contents);
  514. $this->assertFileNotExists(LOGS . 'debug.log');
  515. $this->_deleteLogs();
  516. $testMessage = 'alert message';
  517. Log::alert($testMessage);
  518. $contents = file_get_contents(LOGS . 'error.log');
  519. $this->assertRegExp('/(Alert|Critical): ' . $testMessage . '/', $contents);
  520. $this->assertFileNotExists(LOGS . 'debug.log');
  521. $this->_deleteLogs();
  522. $testMessage = 'critical message';
  523. Log::critical($testMessage);
  524. $contents = file_get_contents(LOGS . 'error.log');
  525. $this->assertContains('Critical: ' . $testMessage, $contents);
  526. $this->assertFileNotExists(LOGS . 'debug.log');
  527. $this->_deleteLogs();
  528. $testMessage = 'error message';
  529. Log::error($testMessage);
  530. $contents = file_get_contents(LOGS . 'error.log');
  531. $this->assertContains('Error: ' . $testMessage, $contents);
  532. $this->assertFileNotExists(LOGS . 'debug.log');
  533. $this->_deleteLogs();
  534. $testMessage = 'warning message';
  535. Log::warning($testMessage);
  536. $contents = file_get_contents(LOGS . 'error.log');
  537. $this->assertContains('Warning: ' . $testMessage, $contents);
  538. $this->assertFileNotExists(LOGS . 'debug.log');
  539. $this->_deleteLogs();
  540. $testMessage = 'notice message';
  541. Log::notice($testMessage);
  542. $contents = file_get_contents(LOGS . 'debug.log');
  543. $this->assertRegExp('/(Notice|Debug): ' . $testMessage . '/', $contents);
  544. $this->assertFileNotExists(LOGS . 'error.log');
  545. $this->_deleteLogs();
  546. $testMessage = 'info message';
  547. Log::info($testMessage);
  548. $contents = file_get_contents(LOGS . 'debug.log');
  549. $this->assertRegExp('/(Info|Debug): ' . $testMessage . '/', $contents);
  550. $this->assertFileNotExists(LOGS . 'error.log');
  551. $this->_deleteLogs();
  552. $testMessage = 'debug message';
  553. Log::debug($testMessage);
  554. $contents = file_get_contents(LOGS . 'debug.log');
  555. $this->assertContains('Debug: ' . $testMessage, $contents);
  556. $this->assertFileNotExists(LOGS . 'error.log');
  557. $this->_deleteLogs();
  558. }
  559. /**
  560. * Test that write() returns false on an unhandled message.
  561. *
  562. * @return void
  563. */
  564. public function testWriteUnhandled()
  565. {
  566. Log::drop('error');
  567. Log::drop('debug');
  568. $result = Log::write('error', 'Bad stuff', 'unpossible');
  569. $this->assertFalse($result);
  570. }
  571. /**
  572. * Tests using a callable for creating a Log engine
  573. *
  574. * @return void
  575. */
  576. public function testCreateLoggerWithCallable()
  577. {
  578. $instance = new FileLog;
  579. Log::config('default', function ($alias) use ($instance) {
  580. $this->assertEquals('default', $alias);
  581. return $instance;
  582. });
  583. $this->assertSame($instance, Log::engine('default'));
  584. }
  585. }