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