Merge pull request #3439 from dokuwiki-translate/lang_update_275_1616098692
[dokuwiki.git] / inc / Logger.php
bloba3999a6ea4322194e3a43a2ea4a1ae5614911338
1 <?php
3 namespace dokuwiki;
5 class Logger
7 const LOG_ERROR = 'error';
8 const LOG_DEPRECATED = 'deprecated';
9 const LOG_DEBUG = 'debug';
11 /** @var Logger[] */
12 static protected $instances;
14 /** @var string what kind of log is this */
15 protected $facility;
17 protected $isLogging = true;
19 /**
20 * Logger constructor.
22 * @param string $facility The type of log
24 protected function __construct($facility)
26 global $conf;
27 $this->facility = $facility;
29 // Should logging be disabled for this facility?
30 $dontlog = explode(',', $conf['dontlog']);
31 $dontlog = array_map('trim', $dontlog);
32 if (in_array($facility, $dontlog)) $this->isLogging = false;
35 /**
36 * Return a Logger instance for the given facility
38 * @param string $facility The type of log
39 * @return Logger
41 static public function getInstance($facility = self::LOG_ERROR)
43 if (empty(self::$instances[$facility])) {
44 self::$instances[$facility] = new Logger($facility);
46 return self::$instances[$facility];
49 /**
50 * Convenience method to directly log to the error log
52 * @param string $message The log message
53 * @param mixed $details Any details that should be added to the log entry
54 * @param string $file A source filename if this is related to a source position
55 * @param int $line A line number for the above file
56 * @return bool has a log been written?
58 static public function error($message, $details = null, $file = '', $line = 0)
60 return self::getInstance(self::LOG_ERROR)->log(
61 $message, $details, $file, $line
65 /**
66 * Convenience method to directly log to the debug log
68 * @param string $message The log message
69 * @param mixed $details Any details that should be added to the log entry
70 * @param string $file A source filename if this is related to a source position
71 * @param int $line A line number for the above file
72 * @return bool has a log been written?
74 static public function debug($message, $details = null, $file = '', $line = 0)
76 return self::getInstance(self::LOG_DEBUG)->log(
77 $message, $details, $file, $line
81 /**
82 * Convenience method to directly log to the deprecation log
84 * @param string $message The log message
85 * @param mixed $details Any details that should be added to the log entry
86 * @param string $file A source filename if this is related to a source position
87 * @param int $line A line number for the above file
88 * @return bool has a log been written?
90 static public function deprecated($message, $details = null, $file = '', $line = 0)
92 return self::getInstance(self::LOG_DEPRECATED)->log(
93 $message, $details, $file, $line
97 /**
98 * Log a message to the facility log
100 * @param string $message The log message
101 * @param mixed $details Any details that should be added to the log entry
102 * @param string $file A source filename if this is related to a source position
103 * @param int $line A line number for the above file
104 * @return bool has a log been written?
106 public function log($message, $details = null, $file = '', $line = 0)
108 if(!$this->isLogging) return false;
110 // details are logged indented
111 if ($details) {
112 if (!is_string($details)) {
113 $details = json_encode($details, JSON_PRETTY_PRINT);
115 $details = explode("\n", $details);
116 $loglines = array_map(function ($line) {
117 return ' ' . $line;
118 }, $details);
119 } elseif ($details) {
120 $loglines = [$details];
121 } else {
122 $loglines = [];
125 // datetime, fileline, message
126 $logline = gmdate('Y-m-d H:i:s') . "\t";
127 if ($file) {
128 $logline .= $file;
129 if ($line) $logline .= "($line)";
131 $logline .= "\t" . $message;
133 array_unshift($loglines, $logline);
134 return $this->writeLogLines($loglines);
138 * Construct the log file for the given day
140 * @param false|string|int $date Date to access, false for today
141 * @return string
143 public function getLogfile($date = false)
145 global $conf;
147 if ($date !== null) $date = strtotime($date);
148 if (!$date) $date = time();
150 return $conf['logdir'] . '/' . $this->facility . '/' . date('Y-m-d', $date) . '.log';
154 * Write the given lines to today's facility log
156 * @param string[] $lines the raw lines to append to the log
157 * @return bool true if the log was written
159 protected function writeLogLines($lines)
161 $logfile = $this->getLogfile();
162 return io_saveFile($logfile, join("\n", $lines) . "\n", true);