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. 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. 'types' => 'debug',
  233. 'file' => 'spam',
  234. ]);
  235. Log::config('eggs', [
  236. 'engine' => 'File',
  237. 'path' => LOGS,
  238. 'types' => ['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. protected function _resetLogConfig()
  259. {
  260. Log::config('debug', [
  261. 'engine' => 'File',
  262. 'path' => LOGS,
  263. 'types' => ['notice', 'info', 'debug'],
  264. 'file' => 'debug',
  265. ]);
  266. Log::config('error', [
  267. 'engine' => 'File',
  268. 'path' => LOGS,
  269. 'types' => ['warning', 'error', 'critical', 'alert', 'emergency'],
  270. 'file' => 'error',
  271. ]);
  272. }
  273. protected function _deleteLogs()
  274. {
  275. if (file_exists(LOGS . 'shops.log')) {
  276. unlink(LOGS . 'shops.log');
  277. }
  278. if (file_exists(LOGS . 'error.log')) {
  279. unlink(LOGS . 'error.log');
  280. }
  281. if (file_exists(LOGS . 'debug.log')) {
  282. unlink(LOGS . 'debug.log');
  283. }
  284. if (file_exists(LOGS . 'bogus.log')) {
  285. unlink(LOGS . 'bogus.log');
  286. }
  287. if (file_exists(LOGS . 'spam.log')) {
  288. unlink(LOGS . 'spam.log');
  289. }
  290. if (file_exists(LOGS . 'eggs.log')) {
  291. unlink(LOGS . 'eggs.log');
  292. }
  293. }
  294. /**
  295. * test scoped logging
  296. *
  297. * @return void
  298. */
  299. public function testScopedLogging()
  300. {
  301. $this->_deleteLogs();
  302. $this->_resetLogConfig();
  303. Log::config('shops', [
  304. 'engine' => 'File',
  305. 'path' => LOGS,
  306. 'types' => ['info', 'debug', 'warning'],
  307. 'scopes' => ['transactions', 'orders'],
  308. 'file' => 'shops',
  309. ]);
  310. Log::write('debug', 'debug message', 'transactions');
  311. $this->assertFileNotExists(LOGS . 'error.log');
  312. $this->assertFileExists(LOGS . 'shops.log');
  313. $this->assertFileExists(LOGS . 'debug.log');
  314. $this->_deleteLogs();
  315. Log::write('warning', 'warning message', 'orders');
  316. $this->assertFileExists(LOGS . 'error.log');
  317. $this->assertFileExists(LOGS . 'shops.log');
  318. $this->assertFileNotExists(LOGS . 'debug.log');
  319. $this->_deleteLogs();
  320. Log::write('error', 'error message', 'orders');
  321. $this->assertFileExists(LOGS . 'error.log');
  322. $this->assertFileNotExists(LOGS . 'debug.log');
  323. $this->assertFileNotExists(LOGS . 'shops.log');
  324. $this->_deleteLogs();
  325. Log::drop('shops');
  326. }
  327. /**
  328. * Test scoped logging without the default loggers catching everything
  329. *
  330. * @return void
  331. */
  332. public function testScopedLoggingStrict()
  333. {
  334. $this->_deleteLogs();
  335. Log::config('debug', [
  336. 'engine' => 'File',
  337. 'path' => LOGS,
  338. 'types' => ['notice', 'info', 'debug'],
  339. 'file' => 'debug',
  340. 'scopes' => false
  341. ]);
  342. Log::config('shops', [
  343. 'engine' => 'File',
  344. 'path' => LOGS,
  345. 'types' => ['info', 'debug', 'warning'],
  346. 'file' => 'shops',
  347. 'scopes' => ['transactions', 'orders'],
  348. ]);
  349. Log::write('debug', 'debug message');
  350. $this->assertFileNotExists(LOGS . 'shops.log');
  351. $this->assertFileExists(LOGS . 'debug.log');
  352. $this->_deleteLogs();
  353. Log::write('debug', 'debug message', 'orders');
  354. $this->assertFileExists(LOGS . 'shops.log');
  355. $this->assertFileNotExists(LOGS . 'debug.log');
  356. $this->_deleteLogs();
  357. Log::drop('shops');
  358. }
  359. /**
  360. * test scoped logging with convenience methods
  361. */
  362. public function testConvenienceScopedLogging()
  363. {
  364. if (file_exists(LOGS . 'shops.log')) {
  365. unlink(LOGS . 'shops.log');
  366. }
  367. if (file_exists(LOGS . 'error.log')) {
  368. unlink(LOGS . 'error.log');
  369. }
  370. if (file_exists(LOGS . 'debug.log')) {
  371. unlink(LOGS . 'debug.log');
  372. }
  373. $this->_resetLogConfig();
  374. Log::config('shops', [
  375. 'engine' => 'File',
  376. 'path' => LOGS,
  377. 'types' => ['info', 'debug', 'notice', 'warning'],
  378. 'scopes' => ['transactions', 'orders'],
  379. 'file' => 'shops',
  380. ]);
  381. Log::info('info message', 'transactions');
  382. $this->assertFileNotExists(LOGS . 'error.log');
  383. $this->assertFileExists(LOGS . 'shops.log');
  384. $this->assertFileExists(LOGS . 'debug.log');
  385. $this->_deleteLogs();
  386. Log::error('error message', 'orders');
  387. $this->assertFileExists(LOGS . 'error.log');
  388. $this->assertFileNotExists(LOGS . 'debug.log');
  389. $this->assertFileNotExists(LOGS . 'shops.log');
  390. $this->_deleteLogs();
  391. Log::warning('warning message', 'orders');
  392. $this->assertFileExists(LOGS . 'error.log');
  393. $this->assertFileExists(LOGS . 'shops.log');
  394. $this->assertFileNotExists(LOGS . 'debug.log');
  395. $this->_deleteLogs();
  396. Log::drop('shops');
  397. }
  398. /**
  399. * Test that scopes are exclusive and don't bleed.
  400. *
  401. * @return void
  402. */
  403. public function testScopedLoggingExclusive()
  404. {
  405. $this->_deleteLogs();
  406. Log::config('shops', [
  407. 'engine' => 'File',
  408. 'path' => LOGS,
  409. 'types' => ['debug', 'notice', 'warning'],
  410. 'scopes' => ['transactions', 'orders'],
  411. 'file' => 'shops.log',
  412. ]);
  413. Log::config('eggs', [
  414. 'engine' => 'File',
  415. 'path' => LOGS,
  416. 'types' => ['debug', 'notice', 'warning'],
  417. 'scopes' => ['eggs'],
  418. 'file' => 'eggs.log',
  419. ]);
  420. Log::write('debug', 'transactions message', 'transactions');
  421. $this->assertFileNotExists(LOGS . 'eggs.log');
  422. $this->assertFileExists(LOGS . 'shops.log');
  423. $this->_deleteLogs();
  424. Log::write('debug', 'eggs message', 'eggs');
  425. $this->assertFileExists(LOGS . 'eggs.log');
  426. $this->assertFileNotExists(LOGS . 'shops.log');
  427. }
  428. /**
  429. * testPassingScopeToEngine method
  430. */
  431. public function testPassingScopeToEngine()
  432. {
  433. Configure::write('App.namespace', 'TestApp');
  434. Log::reset();
  435. Log::config('scope_test', [
  436. 'engine' => 'TestApp',
  437. 'path' => LOGS,
  438. 'types' => ['notice', 'info', 'debug'],
  439. 'scopes' => ['foo', 'bar'],
  440. ]);
  441. $engine = Log::engine('scope_test');
  442. $this->assertNull($engine->passedScope);
  443. Log::write('debug', 'test message', 'foo');
  444. $this->assertEquals(['scope' => ['foo']], $engine->passedScope);
  445. Log::write('debug', 'test message', ['foo', 'bar']);
  446. $this->assertEquals(['scope' => ['foo', 'bar']], $engine->passedScope);
  447. $result = Log::write('debug', 'test message');
  448. $this->assertFalse($result);
  449. }
  450. /**
  451. * test convenience methods
  452. */
  453. public function testConvenienceMethods()
  454. {
  455. $this->_deleteLogs();
  456. Log::config('debug', [
  457. 'engine' => 'File',
  458. 'path' => LOGS,
  459. 'types' => ['notice', 'info', 'debug'],
  460. 'file' => 'debug',
  461. ]);
  462. Log::config('error', [
  463. 'engine' => 'File',
  464. 'path' => LOGS,
  465. 'types' => ['emergency', 'alert', 'critical', 'error', 'warning'],
  466. 'file' => 'error',
  467. ]);
  468. $testMessage = 'emergency message';
  469. Log::emergency($testMessage);
  470. $contents = file_get_contents(LOGS . 'error.log');
  471. $this->assertRegExp('/(Emergency|Critical): ' . $testMessage . '/', $contents);
  472. $this->assertFileNotExists(LOGS . 'debug.log');
  473. $this->_deleteLogs();
  474. $testMessage = 'alert message';
  475. Log::alert($testMessage);
  476. $contents = file_get_contents(LOGS . 'error.log');
  477. $this->assertRegExp('/(Alert|Critical): ' . $testMessage . '/', $contents);
  478. $this->assertFileNotExists(LOGS . 'debug.log');
  479. $this->_deleteLogs();
  480. $testMessage = 'critical message';
  481. Log::critical($testMessage);
  482. $contents = file_get_contents(LOGS . 'error.log');
  483. $this->assertContains('Critical: ' . $testMessage, $contents);
  484. $this->assertFileNotExists(LOGS . 'debug.log');
  485. $this->_deleteLogs();
  486. $testMessage = 'error message';
  487. Log::error($testMessage);
  488. $contents = file_get_contents(LOGS . 'error.log');
  489. $this->assertContains('Error: ' . $testMessage, $contents);
  490. $this->assertFileNotExists(LOGS . 'debug.log');
  491. $this->_deleteLogs();
  492. $testMessage = 'warning message';
  493. Log::warning($testMessage);
  494. $contents = file_get_contents(LOGS . 'error.log');
  495. $this->assertContains('Warning: ' . $testMessage, $contents);
  496. $this->assertFileNotExists(LOGS . 'debug.log');
  497. $this->_deleteLogs();
  498. $testMessage = 'notice message';
  499. Log::notice($testMessage);
  500. $contents = file_get_contents(LOGS . 'debug.log');
  501. $this->assertRegExp('/(Notice|Debug): ' . $testMessage . '/', $contents);
  502. $this->assertFileNotExists(LOGS . 'error.log');
  503. $this->_deleteLogs();
  504. $testMessage = 'info message';
  505. Log::info($testMessage);
  506. $contents = file_get_contents(LOGS . 'debug.log');
  507. $this->assertRegExp('/(Info|Debug): ' . $testMessage . '/', $contents);
  508. $this->assertFileNotExists(LOGS . 'error.log');
  509. $this->_deleteLogs();
  510. $testMessage = 'debug message';
  511. Log::debug($testMessage);
  512. $contents = file_get_contents(LOGS . 'debug.log');
  513. $this->assertContains('Debug: ' . $testMessage, $contents);
  514. $this->assertFileNotExists(LOGS . 'error.log');
  515. $this->_deleteLogs();
  516. }
  517. /**
  518. * Test that write() returns false on an unhandled message.
  519. *
  520. * @return void
  521. */
  522. public function testWriteUnhandled()
  523. {
  524. Log::drop('error');
  525. Log::drop('debug');
  526. $result = Log::write('error', 'Bad stuff', 'unpossible');
  527. $this->assertFalse($result);
  528. }
  529. /**
  530. * Tests using a callable for creating a Log engine
  531. *
  532. * @return void
  533. */
  534. public function testCreateLoggerWithCallable()
  535. {
  536. $instance = new FileLog;
  537. Log::config('default', function ($alias) use ($instance) {
  538. $this->assertEquals('default', $alias);
  539. return $instance;
  540. });
  541. $this->assertSame($instance, Log::engine('default'));
  542. }
  543. }