Rotate.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Log;
  26. use OCP\IConfig;
  27. use OCP\Log\RotationTrait;
  28. use Psr\Log\LoggerInterface;
  29. /**
  30. * This rotates the current logfile to a new name, this way the total log usage
  31. * will stay limited and older entries are available for a while longer.
  32. * For more professional log management set the 'logfile' config to a different
  33. * location and manage that with your own tools.
  34. */
  35. class Rotate extends \OCP\BackgroundJob\Job {
  36. use RotationTrait;
  37. public function run($argument): void {
  38. $config = \OCP\Server::get(IConfig::class);
  39. $this->filePath = $config->getSystemValueString('logfile', $config->getSystemValueString('datadirectory', \OC::$SERVERROOT . '/data') . '/nextcloud.log');
  40. $this->maxSize = $config->getSystemValueInt('log_rotate_size', 100 * 1024 * 1024);
  41. if ($this->shouldRotateBySize()) {
  42. $rotatedFile = $this->rotate();
  43. $msg = 'Log file "'.$this->filePath.'" was over '.$this->maxSize.' bytes, moved to "'.$rotatedFile.'"';
  44. \OCP\Server::get(LoggerInterface::class)->info($msg, ['app' => Rotate::class]);
  45. }
  46. }
  47. }