LogTest.php 14 KB

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