1
0

FileTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. *
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Log;
  8. use OC\Log\File;
  9. use OCP\IConfig;
  10. use OCP\ILogger;
  11. use Test\TestCase;
  12. /**
  13. * Class FileTest
  14. */
  15. class FileTest extends TestCase {
  16. private $restore_logfile;
  17. private $restore_logdateformat;
  18. /** @var File */
  19. protected $logFile;
  20. protected function setUp(): void {
  21. parent::setUp();
  22. $config = \OC::$server->getSystemConfig();
  23. $this->restore_logfile = $config->getValue('logfile');
  24. $this->restore_logdateformat = $config->getValue('logdateformat');
  25. $config->setValue('logfile', $config->getValue('datadirectory') . '/logtest.log');
  26. $this->logFile = new File($config->getValue('datadirectory') . '/logtest.log', '', $config);
  27. }
  28. protected function tearDown(): void {
  29. $config = \OC::$server->getSystemConfig();
  30. if (isset($this->restore_logfile)) {
  31. $config->getValue('logfile', $this->restore_logfile);
  32. } else {
  33. $config->deleteValue('logfile');
  34. }
  35. if (isset($this->restore_logdateformat)) {
  36. $config->getValue('logdateformat', $this->restore_logdateformat);
  37. } else {
  38. $config->deleteValue('logdateformat');
  39. }
  40. $this->logFile = new File($this->restore_logfile, '', $config);
  41. parent::tearDown();
  42. }
  43. public function testLogging(): void {
  44. $config = \OC::$server->get(IConfig::class);
  45. # delete old logfile
  46. unlink($config->getSystemValue('logfile'));
  47. # set format & write log line
  48. $config->setSystemValue('logdateformat', 'u');
  49. $this->logFile->write('code', ['something' => 'extra', 'message' => 'Testing logging'], ILogger::ERROR);
  50. # read log line
  51. $handle = @fopen($config->getSystemValue('logfile'), 'r');
  52. $line = fread($handle, 1000);
  53. fclose($handle);
  54. # check log has data content
  55. $values = (array)json_decode($line, true);
  56. $this->assertArrayNotHasKey('message', $values['data']);
  57. $this->assertEquals('extra', $values['data']['something']);
  58. $this->assertEquals('Testing logging', $values['message']);
  59. }
  60. public function testMicrosecondsLogTimestamp(): void {
  61. $config = \OC::$server->getConfig();
  62. # delete old logfile
  63. unlink($config->getSystemValue('logfile'));
  64. # set format & write log line
  65. $config->setSystemValue('logdateformat', 'u');
  66. $this->logFile->write('test', 'message', ILogger::ERROR);
  67. # read log line
  68. $handle = @fopen($config->getSystemValue('logfile'), 'r');
  69. $line = fread($handle, 1000);
  70. fclose($handle);
  71. # check timestamp has microseconds part
  72. $values = (array)json_decode($line);
  73. $microseconds = $values['time'];
  74. $this->assertNotEquals(0, $microseconds);
  75. }
  76. }