2020-08-13 13:25:46 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace dokuwiki;
|
|
|
|
|
|
|
|
class Logger
|
|
|
|
{
|
|
|
|
const LOG_ERROR = 'error';
|
|
|
|
const LOG_DEPRECATED = 'deprecated';
|
|
|
|
const LOG_DEBUG = 'debug';
|
|
|
|
|
|
|
|
/** @var Logger[] */
|
|
|
|
static protected $instances;
|
|
|
|
|
|
|
|
/** @var string what kind of log is this */
|
|
|
|
protected $facility;
|
|
|
|
|
2020-08-13 20:28:52 +02:00
|
|
|
protected $isLogging = true;
|
|
|
|
|
2020-08-13 13:25:46 +02:00
|
|
|
/**
|
|
|
|
* Logger constructor.
|
|
|
|
*
|
|
|
|
* @param string $facility The type of log
|
|
|
|
*/
|
|
|
|
protected function __construct($facility)
|
|
|
|
{
|
2020-08-13 20:28:52 +02:00
|
|
|
global $conf;
|
2020-08-13 13:25:46 +02:00
|
|
|
$this->facility = $facility;
|
2020-08-13 20:28:52 +02:00
|
|
|
|
|
|
|
// Should logging be disabled for this facility?
|
|
|
|
$dontlog = explode(',', $conf['dontlog']);
|
|
|
|
$dontlog = array_map('trim', $dontlog);
|
|
|
|
if (in_array($facility, $dontlog)) $this->isLogging = false;
|
2020-08-13 13:25:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a Logger instance for the given facility
|
|
|
|
*
|
|
|
|
* @param string $facility The type of log
|
|
|
|
* @return Logger
|
|
|
|
*/
|
|
|
|
static public function getInstance($facility = self::LOG_ERROR)
|
|
|
|
{
|
|
|
|
if (self::$instances[$facility] === null) {
|
|
|
|
self::$instances[$facility] = new Logger($facility);
|
|
|
|
}
|
|
|
|
return self::$instances[$facility];
|
|
|
|
}
|
|
|
|
|
2020-08-13 20:03:41 +02:00
|
|
|
/**
|
|
|
|
* Convenience method to directly log to the error log
|
|
|
|
*
|
|
|
|
* @param string $message The log message
|
|
|
|
* @param mixed $details Any details that should be added to the log entry
|
|
|
|
* @param string $file A source filename if this is related to a source position
|
|
|
|
* @param int $line A line number for the above file
|
2020-08-13 20:28:52 +02:00
|
|
|
* @return bool has a log been written?
|
2020-08-13 20:03:41 +02:00
|
|
|
*/
|
|
|
|
static public function error($message, $details = null, $file = '', $line = 0)
|
|
|
|
{
|
|
|
|
return self::getInstance(self::LOG_ERROR)->log(
|
|
|
|
$message, $details, $file, $line
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience method to directly log to the debug log
|
|
|
|
*
|
|
|
|
* @param string $message The log message
|
|
|
|
* @param mixed $details Any details that should be added to the log entry
|
|
|
|
* @param string $file A source filename if this is related to a source position
|
|
|
|
* @param int $line A line number for the above file
|
2020-08-13 20:28:52 +02:00
|
|
|
* @return bool has a log been written?
|
2020-08-13 20:03:41 +02:00
|
|
|
*/
|
|
|
|
static public function debug($message, $details = null, $file = '', $line = 0)
|
|
|
|
{
|
|
|
|
return self::getInstance(self::LOG_DEBUG)->log(
|
|
|
|
$message, $details, $file, $line
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience method to directly log to the deprecation log
|
|
|
|
*
|
|
|
|
* @param string $message The log message
|
|
|
|
* @param mixed $details Any details that should be added to the log entry
|
|
|
|
* @param string $file A source filename if this is related to a source position
|
|
|
|
* @param int $line A line number for the above file
|
2020-08-13 20:28:52 +02:00
|
|
|
* @return bool has a log been written?
|
2020-08-13 20:03:41 +02:00
|
|
|
*/
|
|
|
|
static public function deprecated($message, $details = null, $file = '', $line = 0)
|
|
|
|
{
|
|
|
|
return self::getInstance(self::LOG_DEPRECATED)->log(
|
|
|
|
$message, $details, $file, $line
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-13 13:25:46 +02:00
|
|
|
/**
|
|
|
|
* Log a message to the facility log
|
|
|
|
*
|
|
|
|
* @param string $message The log message
|
|
|
|
* @param mixed $details Any details that should be added to the log entry
|
|
|
|
* @param string $file A source filename if this is related to a source position
|
|
|
|
* @param int $line A line number for the above file
|
2020-08-13 20:28:52 +02:00
|
|
|
* @return bool has a log been written?
|
2020-08-13 13:25:46 +02:00
|
|
|
*/
|
|
|
|
public function log($message, $details = null, $file = '', $line = 0)
|
|
|
|
{
|
2020-08-13 20:28:52 +02:00
|
|
|
if(!$this->isLogging) return false;
|
|
|
|
|
2020-08-13 13:25:46 +02:00
|
|
|
// details are logged indented
|
2020-08-13 19:48:45 +02:00
|
|
|
if ($details) {
|
|
|
|
if (!is_string($details)) {
|
|
|
|
$details = json_encode($details, JSON_PRETTY_PRINT);
|
|
|
|
}
|
2020-08-13 13:25:46 +02:00
|
|
|
$details = explode("\n", $details);
|
|
|
|
$loglines = array_map(function ($line) {
|
|
|
|
return ' ' . $line;
|
|
|
|
}, $details);
|
|
|
|
} elseif ($details) {
|
|
|
|
$loglines = [$details];
|
|
|
|
} else {
|
|
|
|
$loglines = [];
|
|
|
|
}
|
|
|
|
|
2020-08-13 19:48:45 +02:00
|
|
|
// datetime, fileline, message
|
|
|
|
$logline = gmdate('Y-m-d H:i:s') . "\t";
|
2020-08-13 13:25:46 +02:00
|
|
|
if ($file) {
|
2020-08-13 19:48:45 +02:00
|
|
|
$logline .= $file;
|
2020-08-13 13:25:46 +02:00
|
|
|
if ($line) $logline .= "($line)";
|
|
|
|
}
|
2020-08-13 19:48:45 +02:00
|
|
|
$logline .= "\t" . $message;
|
2020-08-13 13:25:46 +02:00
|
|
|
|
|
|
|
array_unshift($loglines, $logline);
|
|
|
|
return $this->writeLogLines($loglines);
|
|
|
|
}
|
|
|
|
|
2020-08-13 19:48:45 +02:00
|
|
|
/**
|
|
|
|
* Construct the log file for the given day
|
|
|
|
*
|
|
|
|
* @param false|string|int $date Date to access, false for today
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getLogfile($date = false)
|
|
|
|
{
|
|
|
|
global $conf;
|
|
|
|
|
|
|
|
if ($date !== null) $date = strtotime($date);
|
|
|
|
if (!$date) $date = time();
|
|
|
|
|
|
|
|
return $conf['logdir'] . '/' . $this->facility . '/' . date('Y-m-d', $date) . '.log';
|
|
|
|
}
|
|
|
|
|
2020-08-13 13:25:46 +02:00
|
|
|
/**
|
|
|
|
* Write the given lines to today's facility log
|
|
|
|
*
|
|
|
|
* @param string[] $lines the raw lines to append to the log
|
|
|
|
* @return bool true if the log was written
|
|
|
|
*/
|
|
|
|
protected function writeLogLines($lines)
|
|
|
|
{
|
2020-08-13 19:48:45 +02:00
|
|
|
$logfile = $this->getLogfile();
|
2020-08-13 13:25:46 +02:00
|
|
|
return io_saveFile($logfile, join("\n", $lines) . "\n", true);
|
|
|
|
}
|
|
|
|
}
|