FileLogTest.php 7.2 KB

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