Logger.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace dokuwiki;
  3. use dokuwiki\Extension\Event;
  4. /**
  5. * Log messages to a daily log file
  6. */
  7. class Logger
  8. {
  9. const LOG_ERROR = 'error';
  10. const LOG_DEPRECATED = 'deprecated';
  11. const LOG_DEBUG = 'debug';
  12. /** @var Logger[] */
  13. static protected $instances;
  14. /** @var string what kind of log is this */
  15. protected $facility;
  16. protected $isLogging = true;
  17. /**
  18. * Logger constructor.
  19. *
  20. * @param string $facility The type of log
  21. */
  22. protected function __construct($facility)
  23. {
  24. global $conf;
  25. $this->facility = $facility;
  26. // Should logging be disabled for this facility?
  27. $dontlog = explode(',', $conf['dontlog']);
  28. $dontlog = array_map('trim', $dontlog);
  29. if (in_array($facility, $dontlog)) $this->isLogging = false;
  30. }
  31. /**
  32. * Return a Logger instance for the given facility
  33. *
  34. * @param string $facility The type of log
  35. * @return Logger
  36. */
  37. static public function getInstance($facility = self::LOG_ERROR)
  38. {
  39. if (empty(self::$instances[$facility])) {
  40. self::$instances[$facility] = new Logger($facility);
  41. }
  42. return self::$instances[$facility];
  43. }
  44. /**
  45. * Convenience method to directly log to the error log
  46. *
  47. * @param string $message The log message
  48. * @param mixed $details Any details that should be added to the log entry
  49. * @param string $file A source filename if this is related to a source position
  50. * @param int $line A line number for the above file
  51. * @return bool has a log been written?
  52. */
  53. static public function error($message, $details = null, $file = '', $line = 0)
  54. {
  55. return self::getInstance(self::LOG_ERROR)->log(
  56. $message, $details, $file, $line
  57. );
  58. }
  59. /**
  60. * Convenience method to directly log to the debug log
  61. *
  62. * @param string $message The log message
  63. * @param mixed $details Any details that should be added to the log entry
  64. * @param string $file A source filename if this is related to a source position
  65. * @param int $line A line number for the above file
  66. * @return bool has a log been written?
  67. */
  68. static public function debug($message, $details = null, $file = '', $line = 0)
  69. {
  70. return self::getInstance(self::LOG_DEBUG)->log(
  71. $message, $details, $file, $line
  72. );
  73. }
  74. /**
  75. * Convenience method to directly log to the deprecation log
  76. *
  77. * @param string $message The log message
  78. * @param mixed $details Any details that should be added to the log entry
  79. * @param string $file A source filename if this is related to a source position
  80. * @param int $line A line number for the above file
  81. * @return bool has a log been written?
  82. */
  83. static public function deprecated($message, $details = null, $file = '', $line = 0)
  84. {
  85. return self::getInstance(self::LOG_DEPRECATED)->log(
  86. $message, $details, $file, $line
  87. );
  88. }
  89. /**
  90. * Log a message to the facility log
  91. *
  92. * @param string $message The log message
  93. * @param mixed $details Any details that should be added to the log entry
  94. * @param string $file A source filename if this is related to a source position
  95. * @param int $line A line number for the above file
  96. * @triggers LOGGER_DATA_FORMAT can be used to change the logged data or intercept it
  97. * @return bool has a log been written?
  98. */
  99. public function log($message, $details = null, $file = '', $line = 0)
  100. {
  101. global $EVENT_HANDLER;
  102. if (!$this->isLogging) return false;
  103. $datetime = time();
  104. $data = [
  105. 'facility' => $this->facility,
  106. 'datetime' => $datetime,
  107. 'message' => $message,
  108. 'details' => $details,
  109. 'file' => $file,
  110. 'line' => $line,
  111. 'loglines' => [],
  112. 'logfile' => $this->getLogfile($datetime),
  113. ];
  114. if ($EVENT_HANDLER !== null) {
  115. $event = new Event('LOGGER_DATA_FORMAT', $data);
  116. if ($event->advise_before()) {
  117. $data['loglines'] = $this->formatLogLines($data);
  118. }
  119. $event->advise_after();
  120. } else {
  121. // The event system is not yet available, to ensure the log isn't lost even on
  122. // fatal errors, the default action is executed
  123. $data['loglines'] = $this->formatLogLines($data);
  124. }
  125. // only log when any data available
  126. if (count($data['loglines'])) {
  127. return $this->writeLogLines($data['loglines'], $data['logfile']);
  128. } else {
  129. return false;
  130. }
  131. }
  132. /**
  133. * Is this logging instace actually logging?
  134. *
  135. * @return bool
  136. */
  137. public function isLogging()
  138. {
  139. return $this->isLogging;
  140. }
  141. /**
  142. * Formats the given data as loglines
  143. *
  144. * @param array $data Event data from LOGGER_DATA_FORMAT
  145. * @return string[] the lines to log
  146. */
  147. protected function formatLogLines($data)
  148. {
  149. extract($data);
  150. // details are logged indented
  151. if ($details) {
  152. if (!is_string($details)) {
  153. $details = json_encode($details, JSON_PRETTY_PRINT);
  154. }
  155. $details = explode("\n", $details);
  156. $loglines = array_map(function ($line) {
  157. return ' ' . $line;
  158. }, $details);
  159. } elseif ($details) {
  160. $loglines = [$details];
  161. } else {
  162. $loglines = [];
  163. }
  164. // datetime, fileline, message
  165. $logline = gmdate('Y-m-d H:i:s', $datetime) . "\t";
  166. if ($file) {
  167. $logline .= $file;
  168. if ($line) $logline .= "($line)";
  169. }
  170. $logline .= "\t" . $message;
  171. array_unshift($loglines, $logline);
  172. return $loglines;
  173. }
  174. /**
  175. * Construct the log file for the given day
  176. *
  177. * @param false|string|int $date Date to access, false for today
  178. * @return string
  179. */
  180. public function getLogfile($date = false)
  181. {
  182. global $conf;
  183. if ($date !== null && !is_numeric($date)) {
  184. $date = strtotime($date);
  185. }
  186. if (!$date) $date = time();
  187. return $conf['logdir'] . '/' . $this->facility . '/' . date('Y-m-d', $date) . '.log';
  188. }
  189. /**
  190. * Write the given lines to today's facility log
  191. *
  192. * @param string[] $lines the raw lines to append to the log
  193. * @param string $logfile where to write to
  194. * @return bool true if the log was written
  195. */
  196. protected function writeLogLines($lines, $logfile)
  197. {
  198. if (defined('DOKU_UNITTEST')) {
  199. fwrite(STDERR, "\n[" . $this->facility . '] ' . join("\n", $lines) . "\n");
  200. }
  201. return io_saveFile($logfile, join("\n", $lines) . "\n", true);
  202. }
  203. }