PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Api/v1/log4php/appenders/LoggerAppenderDailyFile.php

https://gitlab.com/tomiwa/bcode
PHP | 130 lines | 40 code | 14 blank | 76 comment | 5 complexity | f97b57f6b0b9dba2f650ae5e17d9f28b MD5 | raw file
  1. <?php
  2. /**
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /**
  19. * An Appender that automatically creates a new logfile each day.
  20. *
  21. * The file is rolled over once a day. That means, for each day a new file
  22. * is created. A formatted version of the date pattern is used as to create
  23. * the file name using the {@link PHP_MANUAL#sprintf} function.
  24. *
  25. * This appender uses a layout.
  26. *
  27. * ##Configurable parameters:##
  28. *
  29. * - **datePattern** - Format for the date in the file path, follows formatting
  30. * rules used by the PHP date() function. Default value: "Ymd".
  31. * - **file** - Path to the target file. Should contain a %s which gets
  32. * substituted by the date.
  33. * - **append** - If set to true, the appender will append to the file,
  34. * otherwise the file contents will be overwritten. Defaults to true.
  35. *
  36. * @version $Revision: 1382274 $
  37. * @package log4php
  38. * @subpackage appenders
  39. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  40. * @link http://logging.apache.org/log4php/docs/appenders/daily-file.html Appender documentation
  41. */
  42. class LoggerAppenderDailyFile extends LoggerAppenderFile {
  43. /**
  44. * The 'datePattern' parameter.
  45. * Determines how date will be formatted in file name.
  46. * @var string
  47. */
  48. protected $datePattern = "Ymd";
  49. /**
  50. * Current date which was used when opening a file.
  51. * Used to determine if a rollover is needed when the date changes.
  52. * @var string
  53. */
  54. protected $currentDate;
  55. /** Additional validation for the date pattern. */
  56. public function activateOptions() {
  57. parent::activateOptions();
  58. if (empty($this->datePattern)) {
  59. $this->warn("Required parameter 'datePattern' not set. Closing appender.");
  60. $this->closed = true;
  61. return;
  62. }
  63. }
  64. /**
  65. * Appends a logging event.
  66. *
  67. * If the target file changes because of passage of time (e.g. at midnight)
  68. * the current file is closed. A new file, with the new date, will be
  69. * opened by the write() method.
  70. */
  71. public function append(LoggerLoggingEvent $event) {
  72. $eventDate = $this->getDate($event->getTimestamp());
  73. // Initial setting of current date
  74. if (!isset($this->currentDate)) {
  75. $this->currentDate = $eventDate;
  76. }
  77. // Check if rollover is needed
  78. else if ($this->currentDate !== $eventDate) {
  79. $this->currentDate = $eventDate;
  80. // Close the file if it's open.
  81. // Note: $this->close() is not called here because it would set
  82. // $this->closed to true and the appender would not recieve
  83. // any more logging requests
  84. if (is_resource($this->fp)) {
  85. $this->write($this->layout->getFooter());
  86. fclose($this->fp);
  87. }
  88. $this->fp = null;
  89. }
  90. parent::append($event);
  91. }
  92. /** Renders the date using the configured <var>datePattern<var>. */
  93. protected function getDate($timestamp = null) {
  94. return date($this->datePattern, $timestamp);
  95. }
  96. /**
  97. * Determines target file. Replaces %s in file path with a date.
  98. */
  99. protected function getTargetFile() {
  100. return str_replace('%s', $this->currentDate, $this->file);
  101. }
  102. /**
  103. * Sets the 'datePattern' parameter.
  104. * @param string $datePattern
  105. */
  106. public function setDatePattern($datePattern) {
  107. $this->setString('datePattern', $datePattern);
  108. }
  109. /**
  110. * Returns the 'datePattern' parameter.
  111. * @return string
  112. */
  113. public function getDatePattern() {
  114. return $this->datePattern;
  115. }
  116. }