FileLogTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. /**
  22. * FileLogTest class
  23. */
  24. class FileLogTest extends TestCase
  25. {
  26. /**
  27. * testLogFileWriting method
  28. *
  29. * @return void
  30. */
  31. public function testLogFileWriting()
  32. {
  33. $this->_deleteLogs(LOGS);
  34. $log = new FileLog(['path' => LOGS]);
  35. $log->log('warning', 'Test warning');
  36. $this->assertFileExists(LOGS . 'error.log');
  37. $result = file_get_contents(LOGS . 'error.log');
  38. $this->assertMatchesRegularExpression('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ warning: Test warning/', $result);
  39. $log->log('debug', 'Test warning');
  40. $this->assertFileExists(LOGS . 'debug.log');
  41. $result = file_get_contents(LOGS . 'debug.log');
  42. $this->assertMatchesRegularExpression('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ debug: Test warning/', $result);
  43. $log->log('random', 'Test warning');
  44. $this->assertFileExists(LOGS . 'random.log');
  45. $result = file_get_contents(LOGS . 'random.log');
  46. $this->assertMatchesRegularExpression('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ random: Test warning/', $result);
  47. }
  48. /**
  49. * test using the path setting to log logs in other places.
  50. *
  51. * @return void
  52. */
  53. public function testPathSetting()
  54. {
  55. $path = TMP . 'tests' . DS;
  56. $this->_deleteLogs($path);
  57. $log = new FileLog(compact('path'));
  58. $log->log('warning', 'Test warning');
  59. $this->assertFileExists($path . 'error.log');
  60. }
  61. /**
  62. * test log rotation
  63. *
  64. * @return void
  65. */
  66. public function testRotation()
  67. {
  68. $path = TMP . 'tests' . DS;
  69. $this->_deleteLogs($path);
  70. file_put_contents($path . 'error.log', "this text is under 35 bytes\n");
  71. $log = new FileLog([
  72. 'path' => $path,
  73. 'size' => 35,
  74. 'rotate' => 2,
  75. ]);
  76. $log->log('warning', 'Test warning one');
  77. $this->assertFileExists($path . 'error.log');
  78. $result = file_get_contents($path . 'error.log');
  79. $this->assertMatchesRegularExpression('/warning: Test warning one/', $result);
  80. $this->assertCount(0, glob($path . 'error.log.*'));
  81. clearstatcache();
  82. $log->log('warning', 'Test warning second');
  83. $files = glob($path . 'error.log.*');
  84. $this->assertCount(1, $files);
  85. $result = file_get_contents($files[0]);
  86. $this->assertMatchesRegularExpression('/this text is under 35 bytes/', $result);
  87. $this->assertMatchesRegularExpression('/warning: Test warning one/', $result);
  88. sleep(1);
  89. clearstatcache();
  90. $log->log('warning', 'Test warning third');
  91. $result = file_get_contents($path . 'error.log');
  92. $this->assertMatchesRegularExpression('/warning: Test warning third/', $result);
  93. $files = glob($path . 'error.log.*');
  94. $this->assertCount(2, $files);
  95. $result = file_get_contents($files[0]);
  96. $this->assertMatchesRegularExpression('/this text is under 35 bytes/', $result);
  97. $result = file_get_contents($files[1]);
  98. $this->assertMatchesRegularExpression('/warning: Test warning second/', $result);
  99. file_put_contents($path . 'error.log.0000000000', "The oldest log file with over 35 bytes.\n");
  100. sleep(1);
  101. clearstatcache();
  102. $log->log('warning', 'Test warning fourth');
  103. // rotate count reached so file count should not increase
  104. $files = glob($path . 'error.log.*');
  105. $this->assertCount(2, $files);
  106. $result = file_get_contents($path . 'error.log');
  107. $this->assertMatchesRegularExpression('/warning: Test warning fourth/', $result);
  108. $result = file_get_contents(array_pop($files));
  109. $this->assertMatchesRegularExpression('/warning: Test warning third/', $result);
  110. $result = file_get_contents(array_pop($files));
  111. $this->assertMatchesRegularExpression('/warning: Test warning second/', $result);
  112. file_put_contents($path . 'debug.log', "this text is just greater than 35 bytes\n");
  113. $log = new FileLog([
  114. 'path' => $path,
  115. 'size' => 35,
  116. 'rotate' => 0,
  117. ]);
  118. file_put_contents($path . 'debug.log.0000000000', "The oldest log file with over 35 bytes.\n");
  119. $log->log('debug', 'Test debug');
  120. $this->assertFileExists($path . 'debug.log');
  121. $result = file_get_contents($path . 'debug.log');
  122. $this->assertMatchesRegularExpression('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ debug: Test debug/', $result);
  123. $this->assertFalse(strstr($result, 'greater than 5 bytes'));
  124. $this->assertCount(0, glob($path . 'debug.log.*'));
  125. }
  126. public function testMaskSetting()
  127. {
  128. if (DS === '\\') {
  129. $this->markTestSkipped('File permission testing does not work on Windows.');
  130. }
  131. $path = TMP . 'tests' . DS;
  132. $this->_deleteLogs($path);
  133. $log = new FileLog(['path' => $path, 'mask' => 0666]);
  134. $log->log('warning', 'Test warning one');
  135. $result = substr(sprintf('%o', fileperms($path . 'error.log')), -4);
  136. $expected = '0666';
  137. $this->assertSame($expected, $result);
  138. unlink($path . 'error.log');
  139. $log = new FileLog(['path' => $path, 'mask' => 0644]);
  140. $log->log('warning', 'Test warning two');
  141. $result = substr(sprintf('%o', fileperms($path . 'error.log')), -4);
  142. $expected = '0644';
  143. $this->assertSame($expected, $result);
  144. unlink($path . 'error.log');
  145. $log = new FileLog(['path' => $path, 'mask' => 0640]);
  146. $log->log('warning', 'Test warning three');
  147. $result = substr(sprintf('%o', fileperms($path . 'error.log')), -4);
  148. $expected = '0640';
  149. $this->assertSame($expected, $result);
  150. unlink($path . 'error.log');
  151. }
  152. /**
  153. * helper function to clears all log files in specified directory
  154. *
  155. * @param string $dir
  156. * @return void
  157. */
  158. protected function _deleteLogs($dir)
  159. {
  160. $files = array_merge(glob($dir . '*.log'), glob($dir . '*.log.*'));
  161. foreach ($files as $file) {
  162. unlink($file);
  163. }
  164. }
  165. /**
  166. * test dateFormat option
  167. *
  168. * @return void
  169. */
  170. public function testDateFormat()
  171. {
  172. $this->_deleteLogs(LOGS);
  173. // 'c': ISO 8601 date (added in PHP 5)
  174. $log = new FileLog(['path' => LOGS, 'formatter.dateFormat' => 'c']);
  175. $log->log('warning', 'Test warning');
  176. $result = file_get_contents(LOGS . 'error.log');
  177. $this->assertMatchesRegularExpression('/^2[0-9]{3}-[0-9]+-[0-9]+T[0-9]+:[0-9]+:[0-9]+\+\d{2}:\d{2} warning: Test warning/', $result);
  178. }
  179. }