1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Faulancer\Log; |
4
|
|
|
|
5
|
|
|
use Faulancer\Exception\LogFileInvalidException; |
6
|
|
|
use Faulancer\Exception\LogTypeNotSupportedException; |
7
|
|
|
use Faulancer\Service\Config; |
8
|
|
|
use Faulancer\Log\Writer\DefaultWriter; |
9
|
|
|
use Faulancer\ServiceLocator\ServiceLocator; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Logger | Logger.php |
13
|
|
|
* |
14
|
|
|
* @package Faulancer\Log |
15
|
|
|
* @author Florian Knapp <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class LogManager |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
const LEVEL_INFO = 'info'; |
21
|
|
|
const LEVEL_DEBUG = 'debug'; |
22
|
|
|
const LEVEL_WARN = 'warn'; |
23
|
|
|
const LEVEL_ERROR = 'error'; |
24
|
|
|
|
25
|
|
|
/** @var static */ |
26
|
|
|
private static $instance; |
27
|
|
|
|
28
|
|
|
/** @var ServiceLocator */ |
29
|
|
|
private static $serviceLocator; |
30
|
|
|
|
31
|
|
|
/** @var array */ |
32
|
|
|
protected static $levels = [ |
33
|
|
|
self::LEVEL_INFO, |
34
|
|
|
self::LEVEL_DEBUG, |
35
|
|
|
self::LEVEL_WARN, |
36
|
|
|
self::LEVEL_ERROR |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** @var bool */ |
40
|
|
|
protected static $fileSystemChecked = false; |
41
|
|
|
|
42
|
|
|
/** @var array */ |
43
|
|
|
protected static $logFiles = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return static |
47
|
|
|
*/ |
48
|
|
|
public static function instance() |
49
|
|
|
{ |
50
|
|
|
if (!self::$instance) { |
51
|
|
|
self::$instance = new static(); |
52
|
|
|
self::$serviceLocator = ServiceLocator::instance(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return self::$instance; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Write to resource |
60
|
|
|
* |
61
|
|
|
* @param string $message |
62
|
|
|
* @param string $level |
63
|
|
|
* @param string $writer |
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
* |
67
|
|
|
* @throws LogTypeNotSupportedException |
68
|
|
|
* @throws LogTypeNotSupportedException |
69
|
|
|
* @throws LogFileInvalidException |
70
|
|
|
*/ |
71
|
|
|
public static function log(string $message = '', string $level = self::LEVEL_INFO, string $writer = '') |
72
|
|
|
{ |
73
|
|
|
if (!self::$fileSystemChecked) { |
74
|
|
|
self::_checkConsistency(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** @var Config $config */ |
78
|
|
|
$config = self::$serviceLocator->get(Config::class); |
79
|
|
|
|
80
|
|
|
if (!empty($writer) && class_exists($writer)) { |
81
|
|
|
$writer = new $writer($config, debug_backtrace()); |
82
|
|
|
} else { |
83
|
|
|
$writer = new DefaultWriter($config, debug_backtrace()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $writer->write(self::$logFiles[$level], $message, $level); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Check for filesystem consistency |
91
|
|
|
* |
92
|
|
|
* @return bool |
93
|
|
|
* |
94
|
|
|
* @throws LogTypeNotSupportedException |
95
|
|
|
* @throws LogFileInvalidException |
96
|
|
|
*/ |
97
|
|
|
private static function _checkConsistency() |
98
|
|
|
{ |
99
|
|
|
/** @var Config $config */ |
100
|
|
|
$config = self::$serviceLocator->get(Config::class); |
101
|
|
|
$appRoot = realpath($config->get('projectRoot')); |
102
|
|
|
$logDirs = $config->get('log'); |
103
|
|
|
|
104
|
|
|
if ($logDirs === null) { |
105
|
|
|
return true; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$hasInfoTypeInConf = false; |
109
|
|
|
|
110
|
|
|
if (in_array('info', array_keys($logDirs))) { |
111
|
|
|
$hasInfoTypeInConf = true; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
foreach ($logDirs as $level => $path) { |
115
|
|
|
|
116
|
|
|
if (!in_array($level, self::$levels, true)) { |
117
|
|
|
|
118
|
|
|
throw new LogTypeNotSupportedException( |
119
|
|
|
'Log type "' . $level . '" is currently not supported.' |
120
|
|
|
. 'Please use one of the following log types:' . PHP_EOL |
121
|
|
|
. implode(", ", self::$levels) |
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$lastDirectorySeparator = strrpos($path, '/'); |
127
|
|
|
|
128
|
|
|
if ($lastDirectorySeparator === false) { |
129
|
|
|
throw new LogFileInvalidException( |
130
|
|
|
'You have to define the logfile path as an absolute path beginning from your project root' |
131
|
|
|
. ' (i.e. if "/var/www/vhosts/app" is your project root set the logfile path to "/logs/{$level}.log")' |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$logDir = substr($path, 0, $lastDirectorySeparator); |
136
|
|
|
|
137
|
|
|
if (!is_dir($appRoot . $logDir)) { |
138
|
|
|
mkdir($appRoot . $logDir, 0774, true); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$file = substr($path, $lastDirectorySeparator, strlen($path)); |
142
|
|
|
$fullPath = $appRoot . $logDir . $file; |
143
|
|
|
$fileType = strrpos($fullPath, '.'); |
144
|
|
|
|
145
|
|
|
if ($fileType !== false) { |
146
|
|
|
$fileType = substr($fullPath, $fileType, strlen($fullPath)); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if (!file_exists($fullPath)) { |
150
|
|
|
file_put_contents($fullPath, ''); |
151
|
|
|
chmod($fullPath, 0744); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if (!$hasInfoTypeInConf && !file_exists($appRoot . $logDir . '/info' . $fileType)) { |
155
|
|
|
|
156
|
|
|
file_put_contents($appRoot . $logDir . '/info' . $fileType, ''); |
157
|
|
|
chmod($appRoot . $logDir . '/info' . $fileType, 0744); |
158
|
|
|
|
159
|
|
|
self::$logFiles[self::LEVEL_INFO] = $appRoot . $logDir . '/info' . $fileType; |
160
|
|
|
|
161
|
|
|
} else if (!$hasInfoTypeInConf) { |
162
|
|
|
|
163
|
|
|
self::$logFiles[self::LEVEL_INFO] = $appRoot . $logDir . '/info' . $fileType; |
164
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
self::$logFiles[$level] = $fullPath; |
168
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
self::$fileSystemChecked = true; |
172
|
|
|
|
173
|
|
|
return true; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
} |