LogTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. <?php
  2. /**
  3. * CakePHP(tm) <http://book.cakephp.org/2.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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  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 with convenience methods
  331. */
  332. public function testConvenienceScopedLogging()
  333. {
  334. if (file_exists(LOGS . 'shops.log')) {
  335. unlink(LOGS . 'shops.log');
  336. }
  337. if (file_exists(LOGS . 'error.log')) {
  338. unlink(LOGS . 'error.log');
  339. }
  340. if (file_exists(LOGS . 'debug.log')) {
  341. unlink(LOGS . 'debug.log');
  342. }
  343. $this->_resetLogConfig();
  344. Log::config('shops', [
  345. 'engine' => 'File',
  346. 'path' => LOGS,
  347. 'types' => ['info', 'debug', 'notice', 'warning'],
  348. 'scopes' => ['transactions', 'orders'],
  349. 'file' => 'shops',
  350. ]);
  351. Log::info('info message', 'transactions');
  352. $this->assertFileNotExists(LOGS . 'error.log');
  353. $this->assertFileExists(LOGS . 'shops.log');
  354. $this->assertFileExists(LOGS . 'debug.log');
  355. $this->_deleteLogs();
  356. Log::error('error message', 'orders');
  357. $this->assertFileExists(LOGS . 'error.log');
  358. $this->assertFileNotExists(LOGS . 'debug.log');
  359. $this->assertFileNotExists(LOGS . 'shops.log');
  360. $this->_deleteLogs();
  361. Log::warning('warning message', 'orders');
  362. $this->assertFileExists(LOGS . 'error.log');
  363. $this->assertFileExists(LOGS . 'shops.log');
  364. $this->assertFileNotExists(LOGS . 'debug.log');
  365. $this->_deleteLogs();
  366. Log::drop('shops');
  367. }
  368. /**
  369. * Test that scopes are exclusive and don't bleed.
  370. *
  371. * @return void
  372. */
  373. public function testScopedLoggingExclusive()
  374. {
  375. $this->_deleteLogs();
  376. Log::config('shops', [
  377. 'engine' => 'File',
  378. 'path' => LOGS,
  379. 'types' => ['debug', 'notice', 'warning'],
  380. 'scopes' => ['transactions', 'orders'],
  381. 'file' => 'shops.log',
  382. ]);
  383. Log::config('eggs', [
  384. 'engine' => 'File',
  385. 'path' => LOGS,
  386. 'types' => ['debug', 'notice', 'warning'],
  387. 'scopes' => ['eggs'],
  388. 'file' => 'eggs.log',
  389. ]);
  390. Log::write('debug', 'transactions message', 'transactions');
  391. $this->assertFileNotExists(LOGS . 'eggs.log');
  392. $this->assertFileExists(LOGS . 'shops.log');
  393. $this->_deleteLogs();
  394. Log::write('debug', 'eggs message', 'eggs');
  395. $this->assertFileExists(LOGS . 'eggs.log');
  396. $this->assertFileNotExists(LOGS . 'shops.log');
  397. }
  398. /**
  399. * testPassingScopeToEngine method
  400. */
  401. public function testPassingScopeToEngine()
  402. {
  403. Configure::write('App.namespace', 'TestApp');
  404. Log::reset();
  405. Log::config('scope_test', [
  406. 'engine' => 'TestApp',
  407. 'path' => LOGS,
  408. 'types' => ['notice', 'info', 'debug'],
  409. 'scopes' => ['foo', 'bar'],
  410. ]);
  411. $engine = Log::engine('scope_test');
  412. $this->assertNull($engine->passedScope);
  413. Log::write('debug', 'test message', 'foo');
  414. $this->assertEquals(['scope' => ['foo']], $engine->passedScope);
  415. Log::write('debug', 'test message', ['foo', 'bar']);
  416. $this->assertEquals(['scope' => ['foo', 'bar']], $engine->passedScope);
  417. $result = Log::write('debug', 'test message');
  418. $this->assertFalse($result);
  419. }
  420. /**
  421. * test convenience methods
  422. */
  423. public function testConvenienceMethods()
  424. {
  425. $this->_deleteLogs();
  426. Log::config('debug', [
  427. 'engine' => 'File',
  428. 'path' => LOGS,
  429. 'types' => ['notice', 'info', 'debug'],
  430. 'file' => 'debug',
  431. ]);
  432. Log::config('error', [
  433. 'engine' => 'File',
  434. 'path' => LOGS,
  435. 'types' => ['emergency', 'alert', 'critical', 'error', 'warning'],
  436. 'file' => 'error',
  437. ]);
  438. $testMessage = 'emergency message';
  439. Log::emergency($testMessage);
  440. $contents = file_get_contents(LOGS . 'error.log');
  441. $this->assertRegExp('/(Emergency|Critical): ' . $testMessage . '/', $contents);
  442. $this->assertFileNotExists(LOGS . 'debug.log');
  443. $this->_deleteLogs();
  444. $testMessage = 'alert message';
  445. Log::alert($testMessage);
  446. $contents = file_get_contents(LOGS . 'error.log');
  447. $this->assertRegExp('/(Alert|Critical): ' . $testMessage . '/', $contents);
  448. $this->assertFileNotExists(LOGS . 'debug.log');
  449. $this->_deleteLogs();
  450. $testMessage = 'critical message';
  451. Log::critical($testMessage);
  452. $contents = file_get_contents(LOGS . 'error.log');
  453. $this->assertContains('Critical: ' . $testMessage, $contents);
  454. $this->assertFileNotExists(LOGS . 'debug.log');
  455. $this->_deleteLogs();
  456. $testMessage = 'error message';
  457. Log::error($testMessage);
  458. $contents = file_get_contents(LOGS . 'error.log');
  459. $this->assertContains('Error: ' . $testMessage, $contents);
  460. $this->assertFileNotExists(LOGS . 'debug.log');
  461. $this->_deleteLogs();
  462. $testMessage = 'warning message';
  463. Log::warning($testMessage);
  464. $contents = file_get_contents(LOGS . 'error.log');
  465. $this->assertContains('Warning: ' . $testMessage, $contents);
  466. $this->assertFileNotExists(LOGS . 'debug.log');
  467. $this->_deleteLogs();
  468. $testMessage = 'notice message';
  469. Log::notice($testMessage);
  470. $contents = file_get_contents(LOGS . 'debug.log');
  471. $this->assertRegExp('/(Notice|Debug): ' . $testMessage . '/', $contents);
  472. $this->assertFileNotExists(LOGS . 'error.log');
  473. $this->_deleteLogs();
  474. $testMessage = 'info message';
  475. Log::info($testMessage);
  476. $contents = file_get_contents(LOGS . 'debug.log');
  477. $this->assertRegExp('/(Info|Debug): ' . $testMessage . '/', $contents);
  478. $this->assertFileNotExists(LOGS . 'error.log');
  479. $this->_deleteLogs();
  480. $testMessage = 'debug message';
  481. Log::debug($testMessage);
  482. $contents = file_get_contents(LOGS . 'debug.log');
  483. $this->assertContains('Debug: ' . $testMessage, $contents);
  484. $this->assertFileNotExists(LOGS . 'error.log');
  485. $this->_deleteLogs();
  486. }
  487. /**
  488. * Test that write() returns false on an unhandled message.
  489. *
  490. * @return void
  491. */
  492. public function testWriteUnhandled()
  493. {
  494. Log::drop('error');
  495. Log::drop('debug');
  496. $result = Log::write('error', 'Bad stuff', 'unpossible');
  497. $this->assertFalse($result);
  498. }
  499. /**
  500. * Tests using a callable for creating a Log engine
  501. *
  502. * @return void
  503. */
  504. public function testCreateLoggerWithCallable()
  505. {
  506. $instance = new FileLog;
  507. Log::config('default', function () use ($instance) {
  508. return $instance;
  509. });
  510. $this->assertSame($instance, Log::engine('default'));
  511. }
  512. }