LogTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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 \Cake\Core\Exception\Exception
  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 \Cake\Core\Exception\Exception
  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 \Cake\Core\Exception\Exception
  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()],
  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 \Cake\Core\Exception\Exception
  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. 'types' => 'debug',
  218. 'file' => 'spam',
  219. ));
  220. Log::config('eggs', array(
  221. 'engine' => 'File',
  222. 'types' => array('eggs', 'debug', 'error', 'warning'),
  223. 'file' => 'eggs',
  224. ));
  225. $testMessage = 'selective logging';
  226. Log::write(LOG_WARNING, $testMessage);
  227. $this->assertFileExists(LOGS . 'eggs.log');
  228. $this->assertFileNotExists(LOGS . 'spam.log');
  229. Log::write(LOG_DEBUG, $testMessage);
  230. $this->assertFileExists(LOGS . 'spam.log');
  231. $contents = file_get_contents(LOGS . 'spam.log');
  232. $this->assertContains('Debug: ' . $testMessage, $contents);
  233. $contents = file_get_contents(LOGS . 'eggs.log');
  234. $this->assertContains('Debug: ' . $testMessage, $contents);
  235. if (file_exists(LOGS . 'spam.log')) {
  236. unlink(LOGS . 'spam.log');
  237. }
  238. if (file_exists(LOGS . 'eggs.log')) {
  239. unlink(LOGS . 'eggs.log');
  240. }
  241. }
  242. protected function _resetLogConfig() {
  243. Log::config('debug', array(
  244. 'engine' => 'File',
  245. 'types' => array('notice', 'info', 'debug'),
  246. 'file' => 'debug',
  247. ));
  248. Log::config('error', array(
  249. 'engine' => 'File',
  250. 'types' => array('warning', 'error', 'critical', 'alert', 'emergency'),
  251. 'file' => 'error',
  252. ));
  253. }
  254. protected function _deleteLogs() {
  255. if (file_exists(LOGS . 'shops.log')) {
  256. unlink(LOGS . 'shops.log');
  257. }
  258. if (file_exists(LOGS . 'error.log')) {
  259. unlink(LOGS . 'error.log');
  260. }
  261. if (file_exists(LOGS . 'debug.log')) {
  262. unlink(LOGS . 'debug.log');
  263. }
  264. if (file_exists(LOGS . 'bogus.log')) {
  265. unlink(LOGS . 'bogus.log');
  266. }
  267. if (file_exists(LOGS . 'spam.log')) {
  268. unlink(LOGS . 'spam.log');
  269. }
  270. if (file_exists(LOGS . 'eggs.log')) {
  271. unlink(LOGS . 'eggs.log');
  272. }
  273. }
  274. /**
  275. * test scoped logging
  276. *
  277. * @return void
  278. */
  279. public function testScopedLogging() {
  280. $this->_deleteLogs();
  281. $this->_resetLogConfig();
  282. Log::config('shops', array(
  283. 'engine' => 'File',
  284. 'types' => array('info', 'debug', 'warning'),
  285. 'scopes' => array('transactions', 'orders'),
  286. 'file' => 'shops',
  287. ));
  288. Log::write('debug', 'debug message', 'transactions');
  289. $this->assertFileNotExists(LOGS . 'error.log');
  290. $this->assertFileExists(LOGS . 'shops.log');
  291. $this->assertFileExists(LOGS . 'debug.log');
  292. $this->_deleteLogs();
  293. Log::write('warning', 'warning message', 'orders');
  294. $this->assertFileExists(LOGS . 'error.log');
  295. $this->assertFileExists(LOGS . 'shops.log');
  296. $this->assertFileNotExists(LOGS . 'debug.log');
  297. $this->_deleteLogs();
  298. Log::write('error', 'error message', 'orders');
  299. $this->assertFileExists(LOGS . 'error.log');
  300. $this->assertFileNotExists(LOGS . 'debug.log');
  301. $this->assertFileNotExists(LOGS . 'shops.log');
  302. $this->_deleteLogs();
  303. Log::drop('shops');
  304. }
  305. /**
  306. * test scoped logging with convenience methods
  307. */
  308. public function testConvenienceScopedLogging() {
  309. if (file_exists(LOGS . 'shops.log')) {
  310. unlink(LOGS . 'shops.log');
  311. }
  312. if (file_exists(LOGS . 'error.log')) {
  313. unlink(LOGS . 'error.log');
  314. }
  315. if (file_exists(LOGS . 'debug.log')) {
  316. unlink(LOGS . 'debug.log');
  317. }
  318. $this->_resetLogConfig();
  319. Log::config('shops', array(
  320. 'engine' => 'File',
  321. 'types' => array('info', 'debug', 'notice', 'warning'),
  322. 'scopes' => array('transactions', 'orders'),
  323. 'file' => 'shops',
  324. ));
  325. Log::info('info message', 'transactions');
  326. $this->assertFileNotExists(LOGS . 'error.log');
  327. $this->assertFileExists(LOGS . 'shops.log');
  328. $this->assertFileExists(LOGS . 'debug.log');
  329. $this->_deleteLogs();
  330. Log::error('error message', 'orders');
  331. $this->assertFileExists(LOGS . 'error.log');
  332. $this->assertFileNotExists(LOGS . 'debug.log');
  333. $this->assertFileNotExists(LOGS . 'shops.log');
  334. $this->_deleteLogs();
  335. Log::warning('warning message', 'orders');
  336. $this->assertFileExists(LOGS . 'error.log');
  337. $this->assertFileExists(LOGS . 'shops.log');
  338. $this->assertFileNotExists(LOGS . 'debug.log');
  339. $this->_deleteLogs();
  340. Log::drop('shops');
  341. }
  342. /**
  343. * Test that scopes are exclusive and don't bleed.
  344. *
  345. * @return void
  346. */
  347. public function testScopedLoggingExclusive() {
  348. $this->_deleteLogs();
  349. Log::config('shops', array(
  350. 'engine' => 'File',
  351. 'types' => array('debug', 'notice', 'warning'),
  352. 'scopes' => array('transactions', 'orders'),
  353. 'file' => 'shops.log',
  354. ));
  355. Log::config('eggs', array(
  356. 'engine' => 'File',
  357. 'types' => array('debug', 'notice', 'warning'),
  358. 'scopes' => array('eggs'),
  359. 'file' => 'eggs.log',
  360. ));
  361. Log::write('debug', 'transactions message', 'transactions');
  362. $this->assertFileNotExists(LOGS . 'eggs.log');
  363. $this->assertFileExists(LOGS . 'shops.log');
  364. $this->_deleteLogs();
  365. Log::write('debug', 'eggs message', 'eggs');
  366. $this->assertFileExists(LOGS . 'eggs.log');
  367. $this->assertFileNotExists(LOGS . 'shops.log');
  368. }
  369. /**
  370. * testPassingScopeToEngine method
  371. */
  372. public function testPassingScopeToEngine() {
  373. Configure::write('App.namespace', 'TestApp');
  374. Log::reset();
  375. Log::config('scope_test', [
  376. 'engine' => 'TestApp',
  377. 'types' => array('notice', 'info', 'debug'),
  378. 'scopes' => array('foo', 'bar'),
  379. ]);
  380. $engine = Log::engine('scope_test');
  381. $this->assertNull($engine->passedScope);
  382. Log::write('debug', 'test message', 'foo');
  383. $this->assertEquals('foo', $engine->passedScope);
  384. Log::write('debug', 'test message', ['foo', 'bar']);
  385. $this->assertEquals(['foo', 'bar'], $engine->passedScope);
  386. $result = Log::write('debug', 'test message');
  387. $this->assertFalse($result);
  388. }
  389. /**
  390. * test convenience methods
  391. */
  392. public function testConvenienceMethods() {
  393. $this->_deleteLogs();
  394. Log::config('debug', array(
  395. 'engine' => 'File',
  396. 'types' => array('notice', 'info', 'debug'),
  397. 'file' => 'debug',
  398. ));
  399. Log::config('error', array(
  400. 'engine' => 'File',
  401. 'types' => array('emergency', 'alert', 'critical', 'error', 'warning'),
  402. 'file' => 'error',
  403. ));
  404. $testMessage = 'emergency message';
  405. Log::emergency($testMessage);
  406. $contents = file_get_contents(LOGS . 'error.log');
  407. $this->assertRegExp('/(Emergency|Critical): ' . $testMessage . '/', $contents);
  408. $this->assertFileNotExists(LOGS . 'debug.log');
  409. $this->_deleteLogs();
  410. $testMessage = 'alert message';
  411. Log::alert($testMessage);
  412. $contents = file_get_contents(LOGS . 'error.log');
  413. $this->assertRegExp('/(Alert|Critical): ' . $testMessage . '/', $contents);
  414. $this->assertFileNotExists(LOGS . 'debug.log');
  415. $this->_deleteLogs();
  416. $testMessage = 'critical message';
  417. Log::critical($testMessage);
  418. $contents = file_get_contents(LOGS . 'error.log');
  419. $this->assertContains('Critical: ' . $testMessage, $contents);
  420. $this->assertFileNotExists(LOGS . 'debug.log');
  421. $this->_deleteLogs();
  422. $testMessage = 'error message';
  423. Log::error($testMessage);
  424. $contents = file_get_contents(LOGS . 'error.log');
  425. $this->assertContains('Error: ' . $testMessage, $contents);
  426. $this->assertFileNotExists(LOGS . 'debug.log');
  427. $this->_deleteLogs();
  428. $testMessage = 'warning message';
  429. Log::warning($testMessage);
  430. $contents = file_get_contents(LOGS . 'error.log');
  431. $this->assertContains('Warning: ' . $testMessage, $contents);
  432. $this->assertFileNotExists(LOGS . 'debug.log');
  433. $this->_deleteLogs();
  434. $testMessage = 'notice message';
  435. Log::notice($testMessage);
  436. $contents = file_get_contents(LOGS . 'debug.log');
  437. $this->assertRegExp('/(Notice|Debug): ' . $testMessage . '/', $contents);
  438. $this->assertFileNotExists(LOGS . 'error.log');
  439. $this->_deleteLogs();
  440. $testMessage = 'info message';
  441. Log::info($testMessage);
  442. $contents = file_get_contents(LOGS . 'debug.log');
  443. $this->assertRegExp('/(Info|Debug): ' . $testMessage . '/', $contents);
  444. $this->assertFileNotExists(LOGS . 'error.log');
  445. $this->_deleteLogs();
  446. $testMessage = 'debug message';
  447. Log::debug($testMessage);
  448. $contents = file_get_contents(LOGS . 'debug.log');
  449. $this->assertContains('Debug: ' . $testMessage, $contents);
  450. $this->assertFileNotExists(LOGS . 'error.log');
  451. $this->_deleteLogs();
  452. }
  453. /**
  454. * Test that write() returns false on an unhandled message.
  455. *
  456. * @return false
  457. */
  458. public function testWriteUnhandled() {
  459. Log::drop('error');
  460. Log::drop('debug');
  461. $result = Log::write('error', 'Bad stuff', 'unpossible');
  462. $this->assertFalse($result);
  463. }
  464. }