TimestampFormatter.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Console;
  8. use OCP\IConfig;
  9. use Symfony\Component\Console\Formatter\OutputFormatterInterface;
  10. use Symfony\Component\Console\Formatter\OutputFormatterStyleInterface;
  11. class TimestampFormatter implements OutputFormatterInterface {
  12. /** @var ?IConfig */
  13. protected $config;
  14. /** @var OutputFormatterInterface */
  15. protected $formatter;
  16. /**
  17. * @param ?IConfig $config
  18. * @param OutputFormatterInterface $formatter
  19. */
  20. public function __construct(?IConfig $config, OutputFormatterInterface $formatter) {
  21. $this->config = $config;
  22. $this->formatter = $formatter;
  23. }
  24. /**
  25. * Sets the decorated flag.
  26. *
  27. * @param bool $decorated Whether to decorate the messages or not
  28. */
  29. public function setDecorated(bool $decorated) {
  30. $this->formatter->setDecorated($decorated);
  31. }
  32. /**
  33. * Gets the decorated flag.
  34. *
  35. * @return bool true if the output will decorate messages, false otherwise
  36. */
  37. public function isDecorated(): bool {
  38. return $this->formatter->isDecorated();
  39. }
  40. /**
  41. * Sets a new style.
  42. *
  43. * @param string $name The style name
  44. * @param OutputFormatterStyleInterface $style The style instance
  45. */
  46. public function setStyle(string $name, OutputFormatterStyleInterface $style) {
  47. $this->formatter->setStyle($name, $style);
  48. }
  49. /**
  50. * Checks if output formatter has style with specified name.
  51. *
  52. * @param string $name
  53. * @return bool
  54. */
  55. public function hasStyle(string $name): bool {
  56. return $this->formatter->hasStyle($name);
  57. }
  58. /**
  59. * Gets style options from style with specified name.
  60. *
  61. * @param string $name
  62. * @return OutputFormatterStyleInterface
  63. * @throws \InvalidArgumentException When style isn't defined
  64. */
  65. public function getStyle(string $name): OutputFormatterStyleInterface {
  66. return $this->formatter->getStyle($name);
  67. }
  68. /**
  69. * Formats a message according to the given styles.
  70. *
  71. * @param string|null $message The message to style
  72. * @return string|null The styled message, prepended with a timestamp using the
  73. * log timezone and dateformat, e.g. "2015-06-23T17:24:37+02:00"
  74. */
  75. public function format(?string $message): ?string {
  76. if (!$this->formatter->isDecorated()) {
  77. // Don't add anything to the output when we shouldn't
  78. return $this->formatter->format($message);
  79. }
  80. if ($this->config instanceof IConfig) {
  81. $timeZone = $this->config->getSystemValue('logtimezone', 'UTC');
  82. $timeZone = $timeZone !== null ? new \DateTimeZone($timeZone) : null;
  83. $time = new \DateTime('now', $timeZone);
  84. $timestampInfo = $time->format($this->config->getSystemValue('logdateformat', \DateTimeInterface::ATOM));
  85. } else {
  86. $time = new \DateTime('now');
  87. $timestampInfo = $time->format(\DateTimeInterface::ATOM);
  88. }
  89. return $timestampInfo . ' ' . $this->formatter->format($message);
  90. }
  91. }