LogTest.php 18 KB

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