1*0ecde6ceSAndreas Gohr<?php 2*0ecde6ceSAndreas Gohr 3*0ecde6ceSAndreas Gohrnamespace dokuwiki; 4*0ecde6ceSAndreas Gohr 5*0ecde6ceSAndreas Gohrclass Logger 6*0ecde6ceSAndreas Gohr{ 7*0ecde6ceSAndreas Gohr const LOG_ERROR = 'error'; 8*0ecde6ceSAndreas Gohr const LOG_DEPRECATED = 'deprecated'; 9*0ecde6ceSAndreas Gohr const LOG_DEBUG = 'debug'; 10*0ecde6ceSAndreas Gohr 11*0ecde6ceSAndreas Gohr /** @var Logger[] */ 12*0ecde6ceSAndreas Gohr static protected $instances; 13*0ecde6ceSAndreas Gohr 14*0ecde6ceSAndreas Gohr /** @var string what kind of log is this */ 15*0ecde6ceSAndreas Gohr protected $facility; 16*0ecde6ceSAndreas Gohr 17*0ecde6ceSAndreas Gohr /** 18*0ecde6ceSAndreas Gohr * Logger constructor. 19*0ecde6ceSAndreas Gohr * 20*0ecde6ceSAndreas Gohr * @param string $facility The type of log 21*0ecde6ceSAndreas Gohr */ 22*0ecde6ceSAndreas Gohr protected function __construct($facility) 23*0ecde6ceSAndreas Gohr { 24*0ecde6ceSAndreas Gohr $this->facility = $facility; 25*0ecde6ceSAndreas Gohr } 26*0ecde6ceSAndreas Gohr 27*0ecde6ceSAndreas Gohr /** 28*0ecde6ceSAndreas Gohr * Return a Logger instance for the given facility 29*0ecde6ceSAndreas Gohr * 30*0ecde6ceSAndreas Gohr * @param string $facility The type of log 31*0ecde6ceSAndreas Gohr * @return Logger 32*0ecde6ceSAndreas Gohr */ 33*0ecde6ceSAndreas Gohr static public function getInstance($facility = self::LOG_ERROR) 34*0ecde6ceSAndreas Gohr { 35*0ecde6ceSAndreas Gohr if (self::$instances[$facility] === null) { 36*0ecde6ceSAndreas Gohr self::$instances[$facility] = new Logger($facility); 37*0ecde6ceSAndreas Gohr } 38*0ecde6ceSAndreas Gohr return self::$instances[$facility]; 39*0ecde6ceSAndreas Gohr } 40*0ecde6ceSAndreas Gohr 41*0ecde6ceSAndreas Gohr /** 42*0ecde6ceSAndreas Gohr * Log a message to the facility log 43*0ecde6ceSAndreas Gohr * 44*0ecde6ceSAndreas Gohr * @param string $message The log message 45*0ecde6ceSAndreas Gohr * @param mixed $details Any details that should be added to the log entry 46*0ecde6ceSAndreas Gohr * @param string $file A source filename if this is related to a source position 47*0ecde6ceSAndreas Gohr * @param int $line A line number for the above file 48*0ecde6ceSAndreas Gohr * @return bool 49*0ecde6ceSAndreas Gohr */ 50*0ecde6ceSAndreas Gohr public function log($message, $details = null, $file = '', $line = 0) 51*0ecde6ceSAndreas Gohr { 52*0ecde6ceSAndreas Gohr // details are logged indented 53*0ecde6ceSAndreas Gohr if ($details && !is_string($details)) { 54*0ecde6ceSAndreas Gohr $details = json_encode($details, JSON_PRETTY_PRINT); 55*0ecde6ceSAndreas Gohr $details = explode("\n", $details); 56*0ecde6ceSAndreas Gohr $loglines = array_map(function ($line) { 57*0ecde6ceSAndreas Gohr return ' ' . $line; 58*0ecde6ceSAndreas Gohr }, $details); 59*0ecde6ceSAndreas Gohr } elseif ($details) { 60*0ecde6ceSAndreas Gohr $loglines = [$details]; 61*0ecde6ceSAndreas Gohr } else { 62*0ecde6ceSAndreas Gohr $loglines = []; 63*0ecde6ceSAndreas Gohr } 64*0ecde6ceSAndreas Gohr 65*0ecde6ceSAndreas Gohr $logline = gmdate('c') . "\t" . $message; 66*0ecde6ceSAndreas Gohr if ($file) { 67*0ecde6ceSAndreas Gohr $logline .= "\t$file"; 68*0ecde6ceSAndreas Gohr if ($line) $logline .= "($line)"; 69*0ecde6ceSAndreas Gohr } 70*0ecde6ceSAndreas Gohr 71*0ecde6ceSAndreas Gohr array_unshift($loglines, $logline); 72*0ecde6ceSAndreas Gohr return $this->writeLogLines($loglines); 73*0ecde6ceSAndreas Gohr } 74*0ecde6ceSAndreas Gohr 75*0ecde6ceSAndreas Gohr /** 76*0ecde6ceSAndreas Gohr * Write the given lines to today's facility log 77*0ecde6ceSAndreas Gohr * 78*0ecde6ceSAndreas Gohr * @param string[] $lines the raw lines to append to the log 79*0ecde6ceSAndreas Gohr * @return bool true if the log was written 80*0ecde6ceSAndreas Gohr */ 81*0ecde6ceSAndreas Gohr protected function writeLogLines($lines) 82*0ecde6ceSAndreas Gohr { 83*0ecde6ceSAndreas Gohr global $conf; 84*0ecde6ceSAndreas Gohr $logfile = $conf['logdir'] . '/' . $this->facility . '/' . gmdate('Y-m-d') . '.log'; 85*0ecde6ceSAndreas Gohr return io_saveFile($logfile, join("\n", $lines) . "\n", true); 86*0ecde6ceSAndreas Gohr } 87*0ecde6ceSAndreas Gohr} 88