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