FileTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  6. * License as published by the Free Software Foundation; either
  7. * version 3 of the License, or any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public
  15. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. namespace Test\Log;
  18. use OC\Log\File;
  19. use OCP\ILogger;
  20. use Test\TestCase;
  21. /**
  22. * Class FileTest
  23. */
  24. class FileTest extends TestCase {
  25. private $restore_logfile;
  26. private $restore_logdateformat;
  27. /** @var File */
  28. protected $logFile;
  29. protected function setUp(): void {
  30. parent::setUp();
  31. $config = \OC::$server->getSystemConfig();
  32. $this->restore_logfile = $config->getValue("logfile");
  33. $this->restore_logdateformat = $config->getValue('logdateformat');
  34. $config->setValue("logfile", $config->getValue('datadirectory') . "/logtest.log");
  35. $this->logFile = new File($config->getValue('datadirectory') . '/logtest.log', '', $config);
  36. }
  37. protected function tearDown(): void {
  38. $config = \OC::$server->getSystemConfig();
  39. if (isset($this->restore_logfile)) {
  40. $config->getValue("logfile", $this->restore_logfile);
  41. } else {
  42. $config->deleteValue("logfile");
  43. }
  44. if (isset($this->restore_logdateformat)) {
  45. $config->getValue("logdateformat", $this->restore_logdateformat);
  46. } else {
  47. $config->deleteValue("logdateformat");
  48. }
  49. $this->logFile = new File($this->restore_logfile, '', $config);
  50. parent::tearDown();
  51. }
  52. public function testMicrosecondsLogTimestamp() {
  53. $config = \OC::$server->getConfig();
  54. # delete old logfile
  55. unlink($config->getSystemValue('logfile'));
  56. # set format & write log line
  57. $config->setSystemValue('logdateformat', 'u');
  58. $this->logFile->write('test', 'message', ILogger::ERROR);
  59. # read log line
  60. $handle = @fopen($config->getSystemValue('logfile'), 'r');
  61. $line = fread($handle, 1000);
  62. fclose($handle);
  63. # check timestamp has microseconds part
  64. $values = (array) json_decode($line);
  65. $microseconds = $values['time'];
  66. $this->assertNotEquals(0, $microseconds);
  67. }
  68. }