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