FileLogTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * FileLogTest file
  5. *
  6. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  7. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  8. *
  9. * Licensed under The MIT License
  10. * For full copyright and license information, please see the LICENSE.txt
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  14. * @link https://cakephp.org CakePHP(tm) Project
  15. * @since 1.3.0
  16. * @license https://opensource.org/licenses/mit-license.php MIT License
  17. */
  18. namespace Cake\Test\TestCase\Log\Engine;
  19. use Cake\Log\Engine\FileLog;
  20. use Cake\TestSuite\TestCase;
  21. use TestApp\Log\Object\JsonObject;
  22. use TestApp\Log\Object\StringObject;
  23. /**
  24. * FileLogTest class
  25. */
  26. class FileLogTest extends TestCase
  27. {
  28. /**
  29. * testLogFileWriting method
  30. *
  31. * @return void
  32. */
  33. public function testLogFileWriting()
  34. {
  35. $this->_deleteLogs(LOGS);
  36. $log = new FileLog(['path' => LOGS]);
  37. $log->log('warning', 'Test warning');
  38. $this->assertFileExists(LOGS . 'error.log');
  39. $result = file_get_contents(LOGS . 'error.log');
  40. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning/', $result);
  41. $log->log('debug', 'Test warning');
  42. $this->assertFileExists(LOGS . 'debug.log');
  43. $result = file_get_contents(LOGS . 'debug.log');
  44. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: Test warning/', $result);
  45. $log->log('random', 'Test warning');
  46. $this->assertFileExists(LOGS . 'random.log');
  47. $result = file_get_contents(LOGS . 'random.log');
  48. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Random: Test warning/', $result);
  49. $object = new StringObject();
  50. $log->log('debug', $object);
  51. $this->assertFileExists(LOGS . 'debug.log');
  52. $result = file_get_contents(LOGS . 'debug.log');
  53. $this->assertContains('Debug: Hey!', $result);
  54. $object = new JsonObject();
  55. $log->log('debug', $object);
  56. $this->assertFileExists(LOGS . 'debug.log');
  57. $result = file_get_contents(LOGS . 'debug.log');
  58. $this->assertContains('Debug: ' . json_encode(['hello' => 'world']), $result);
  59. $log->log('debug', [1, 2]);
  60. $this->assertFileExists(LOGS . 'debug.log');
  61. $result = file_get_contents(LOGS . 'debug.log');
  62. $this->assertContains('Debug: ' . print_r([1, 2], true), $result);
  63. }
  64. /**
  65. * test using the path setting to log logs in other places.
  66. *
  67. * @return void
  68. */
  69. public function testPathSetting()
  70. {
  71. $path = TMP . 'tests' . DS;
  72. $this->_deleteLogs($path);
  73. $log = new FileLog(compact('path'));
  74. $log->log('warning', 'Test warning');
  75. $this->assertFileExists($path . 'error.log');
  76. }
  77. /**
  78. * test log rotation
  79. *
  80. * @return void
  81. */
  82. public function testRotation()
  83. {
  84. $path = TMP . 'tests' . DS;
  85. $this->_deleteLogs($path);
  86. file_put_contents($path . 'error.log', "this text is under 35 bytes\n");
  87. $log = new FileLog([
  88. 'path' => $path,
  89. 'size' => 35,
  90. 'rotate' => 2,
  91. ]);
  92. $log->log('warning', 'Test warning one');
  93. $this->assertFileExists($path . 'error.log');
  94. $result = file_get_contents($path . 'error.log');
  95. $this->assertRegExp('/Warning: Test warning one/', $result);
  96. $this->assertCount(0, glob($path . 'error.log.*'));
  97. clearstatcache();
  98. $log->log('warning', 'Test warning second');
  99. $files = glob($path . 'error.log.*');
  100. $this->assertCount(1, $files);
  101. $result = file_get_contents($files[0]);
  102. $this->assertRegExp('/this text is under 35 bytes/', $result);
  103. $this->assertRegExp('/Warning: Test warning one/', $result);
  104. sleep(1);
  105. clearstatcache();
  106. $log->log('warning', 'Test warning third');
  107. $result = file_get_contents($path . 'error.log');
  108. $this->assertRegExp('/Warning: Test warning third/', $result);
  109. $files = glob($path . 'error.log.*');
  110. $this->assertCount(2, $files);
  111. $result = file_get_contents($files[0]);
  112. $this->assertRegExp('/this text is under 35 bytes/', $result);
  113. $result = file_get_contents($files[1]);
  114. $this->assertRegExp('/Warning: Test warning second/', $result);
  115. file_put_contents($path . 'error.log.0000000000', "The oldest log file with over 35 bytes.\n");
  116. sleep(1);
  117. clearstatcache();
  118. $log->log('warning', 'Test warning fourth');
  119. // rotate count reached so file count should not increase
  120. $files = glob($path . 'error.log.*');
  121. $this->assertCount(2, $files);
  122. $result = file_get_contents($path . 'error.log');
  123. $this->assertRegExp('/Warning: Test warning fourth/', $result);
  124. $result = file_get_contents(array_pop($files));
  125. $this->assertRegExp('/Warning: Test warning third/', $result);
  126. $result = file_get_contents(array_pop($files));
  127. $this->assertRegExp('/Warning: Test warning second/', $result);
  128. file_put_contents($path . 'debug.log', "this text is just greater than 35 bytes\n");
  129. $log = new FileLog([
  130. 'path' => $path,
  131. 'size' => 35,
  132. 'rotate' => 0,
  133. ]);
  134. file_put_contents($path . 'debug.log.0000000000', "The oldest log file with over 35 bytes.\n");
  135. $log->log('debug', 'Test debug');
  136. $this->assertFileExists($path . 'debug.log');
  137. $result = file_get_contents($path . 'debug.log');
  138. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: Test debug/', $result);
  139. $this->assertFalse(strstr($result, 'greater than 5 bytes'));
  140. $this->assertCount(0, glob($path . 'debug.log.*'));
  141. }
  142. public function testMaskSetting()
  143. {
  144. if (DS === '\\') {
  145. $this->markTestSkipped('File permission testing does not work on Windows.');
  146. }
  147. $path = TMP . 'tests' . DS;
  148. $this->_deleteLogs($path);
  149. $log = new FileLog(['path' => $path, 'mask' => 0666]);
  150. $log->log('warning', 'Test warning one');
  151. $result = substr(sprintf('%o', fileperms($path . 'error.log')), -4);
  152. $expected = '0666';
  153. $this->assertEquals($expected, $result);
  154. unlink($path . 'error.log');
  155. $log = new FileLog(['path' => $path, 'mask' => 0644]);
  156. $log->log('warning', 'Test warning two');
  157. $result = substr(sprintf('%o', fileperms($path . 'error.log')), -4);
  158. $expected = '0644';
  159. $this->assertEquals($expected, $result);
  160. unlink($path . 'error.log');
  161. $log = new FileLog(['path' => $path, 'mask' => 0640]);
  162. $log->log('warning', 'Test warning three');
  163. $result = substr(sprintf('%o', fileperms($path . 'error.log')), -4);
  164. $expected = '0640';
  165. $this->assertEquals($expected, $result);
  166. unlink($path . 'error.log');
  167. }
  168. /**
  169. * helper function to clears all log files in specified directory
  170. *
  171. * @param string $dir
  172. * @return void
  173. */
  174. protected function _deleteLogs($dir)
  175. {
  176. $files = array_merge(glob($dir . '*.log'), glob($dir . '*.log.*'));
  177. foreach ($files as $file) {
  178. unlink($file);
  179. }
  180. }
  181. }