FileTest.php 3.1 KB

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