1
0

RotationTrait.php 870 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCP\Log;
  7. /**
  8. * Trait RotationTrait
  9. *
  10. *
  11. * @since 14.0.0
  12. */
  13. trait RotationTrait {
  14. /**
  15. * @var string
  16. * @since 14.0.0
  17. */
  18. protected $filePath;
  19. /**
  20. * @var int
  21. * @since 14.0.0
  22. */
  23. protected $maxSize;
  24. /**
  25. * @return string the resulting new filepath
  26. * @since 14.0.0
  27. */
  28. protected function rotate():string {
  29. $rotatedFile = $this->filePath.'.1';
  30. rename($this->filePath, $rotatedFile);
  31. return $rotatedFile;
  32. }
  33. /**
  34. * @return bool
  35. * @since 14.0.0
  36. */
  37. protected function shouldRotateBySize():bool {
  38. if ((int)$this->maxSize > 0 && file_exists($this->filePath)) {
  39. $filesize = @filesize($this->filePath);
  40. if ($filesize >= (int)$this->maxSize) {
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46. }