[TASK] Streamline PHPDoc comment matches function/method signature
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Log / Writer / FileWriter.php
1 <?php
2
3 namespace TYPO3\CMS\Core\Log\Writer;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\Core\Environment;
19 use TYPO3\CMS\Core\Log\Exception\InvalidLogWriterConfigurationException;
20 use TYPO3\CMS\Core\Log\LogLevel;
21 use TYPO3\CMS\Core\Log\LogRecord;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Core\Utility\PathUtility;
24
25 /**
26 * Log writer that writes the log records into a file.
27 */
28 class FileWriter extends AbstractWriter
29 {
30 /**
31 * Log file path, relative to TYPO3's base project folder
32 *
33 * @var string
34 */
35 protected $logFile = '';
36
37 /**
38 * @var string
39 */
40 protected $logFileInfix = '';
41
42 /**
43 * Default log file path
44 *
45 * @var string
46 */
47 protected $defaultLogFileTemplate = '/log/typo3_%s.log';
48
49 /**
50 * Log file handle storage
51 *
52 * To avoid concurrent file handles on a the same file when using several FileWriter instances,
53 * we share the file handles in a static class variable
54 *
55 * @static
56 * @var array
57 */
58 protected static $logFileHandles = [];
59
60 /**
61 * Keep track of used file handles by different fileWriter instances
62 * As the logger gets instantiated by class name but the resources
63 * are shared via the static $logFileHandles we need to track usage
64 * of file handles to avoid closing handles that are still needed
65 * by different instances. Only if the count is zero may the file
66 * handle be closed.
67 *
68 * @var array
69 */
70 protected static $logFileHandlesCount = [];
71
72 /**
73 * Constructor, opens the log file handle
74 *
75 * @param array $options
76 */
77 public function __construct(array $options = [])
78 {
79 // the parent constructor reads $options and sets them
80 parent::__construct($options);
81 if (empty($options['logFile'])) {
82 $this->setLogFile($this->getDefaultLogFileName());
83 }
84 }
85
86 /**
87 * Destructor, closes the log file handle
88 */
89 public function __destruct()
90 {
91 self::$logFileHandlesCount[$this->logFile]--;
92 if (self::$logFileHandlesCount[$this->logFile] <= 0) {
93 $this->closeLogFile();
94 }
95 }
96
97 public function setLogFileInfix(string $infix)
98 {
99 $this->logFileInfix = $infix;
100 }
101
102 /**
103 * Sets the path to the log file.
104 *
105 * @param string $relativeLogFile path to the log file, relative to public web dir
106 * @return WriterInterface
107 * @throws InvalidLogWriterConfigurationException
108 */
109 public function setLogFile($relativeLogFile)
110 {
111 $logFile = $relativeLogFile;
112 // Skip handling if logFile is a stream resource. This is used by unit tests with vfs:// directories
113 if (false === strpos($logFile, '://') && !PathUtility::isAbsolutePath($logFile)) {
114 $logFile = GeneralUtility::getFileAbsFileName($logFile);
115 if (empty($logFile)) {
116 throw new InvalidLogWriterConfigurationException(
117 'Log file path "' . $relativeLogFile . '" is not valid!',
118 1444374805
119 );
120 }
121 }
122 $this->logFile = $logFile;
123 $this->openLogFile();
124
125 return $this;
126 }
127
128 /**
129 * Gets the path to the log file.
130 *
131 * @return string Path to the log file.
132 */
133 public function getLogFile()
134 {
135 return $this->logFile;
136 }
137
138 /**
139 * Writes the log record
140 *
141 * @param LogRecord $record Log record
142 * @return WriterInterface $this
143 * @throws \RuntimeException
144 */
145 public function writeLog(LogRecord $record)
146 {
147 $timestamp = date('r', (int)$record->getCreated());
148 $levelName = LogLevel::getName($record->getLevel());
149 $data = '';
150 $recordData = $record->getData();
151 if (!empty($recordData)) {
152 // According to PSR3 the exception-key may hold an \Exception
153 // Since json_encode() does not encode an exception, we run the _toString() here
154 if (isset($recordData['exception']) && $recordData['exception'] instanceof \Exception) {
155 $recordData['exception'] = (string)$recordData['exception'];
156 }
157 $data = '- ' . json_encode($recordData);
158 }
159
160 $message = sprintf(
161 '%s [%s] request="%s" component="%s": %s %s',
162 $timestamp,
163 $levelName,
164 $record->getRequestId(),
165 $record->getComponent(),
166 $record->getMessage(),
167 $data
168 );
169
170 if (false === fwrite(self::$logFileHandles[$this->logFile], $message . LF)) {
171 throw new \RuntimeException('Could not write log record to log file', 1345036335);
172 }
173
174 return $this;
175 }
176
177 /**
178 * Opens the log file handle
179 *
180 * @throws \RuntimeException if the log file can't be opened.
181 */
182 protected function openLogFile()
183 {
184 if (isset(self::$logFileHandlesCount[$this->logFile])) {
185 self::$logFileHandlesCount[$this->logFile]++;
186 } else {
187 self::$logFileHandlesCount[$this->logFile] = 1;
188 }
189 if (isset(self::$logFileHandles[$this->logFile]) && is_resource(self::$logFileHandles[$this->logFile] ?? false)) {
190 return;
191 }
192
193 $this->createLogFile();
194 self::$logFileHandles[$this->logFile] = fopen($this->logFile, 'a');
195 if (!is_resource(self::$logFileHandles[$this->logFile])) {
196 throw new \RuntimeException('Could not open log file "' . $this->logFile . '"', 1321804422);
197 }
198 }
199
200 /**
201 * Closes the log file handle.
202 */
203 protected function closeLogFile()
204 {
205 if (!empty(self::$logFileHandles[$this->logFile]) && is_resource(self::$logFileHandles[$this->logFile])) {
206 fclose(self::$logFileHandles[$this->logFile]);
207 unset(self::$logFileHandles[$this->logFile]);
208 }
209 }
210
211 /**
212 * Creates the log file with correct permissions
213 * and parent directories, if needed
214 */
215 protected function createLogFile()
216 {
217 if (file_exists($this->logFile)) {
218 return;
219 }
220 $logFileDirectory = PathUtility::dirname($this->logFile);
221 if (!@is_dir($logFileDirectory)) {
222 GeneralUtility::mkdir_deep($logFileDirectory);
223 // create .htaccess file if log file is within the site path
224 if (PathUtility::getCommonPrefix([Environment::getPublicPath() . '/', $logFileDirectory]) === (Environment::getPublicPath() . '/')) {
225 // only create .htaccess, if we created the directory on our own
226 $this->createHtaccessFile($logFileDirectory . '/.htaccess');
227 }
228 }
229 // create the log file
230 GeneralUtility::writeFile($this->logFile, '');
231 }
232
233 /**
234 * Creates .htaccess file inside a new directory to access protect it
235 *
236 * @param string $htaccessFile Path of .htaccess file
237 */
238 protected function createHtaccessFile($htaccessFile)
239 {
240 // write .htaccess file to protect the log file
241 if (!empty($GLOBALS['TYPO3_CONF_VARS']['SYS']['generateApacheHtaccess']) && !file_exists($htaccessFile)) {
242 $htaccessContent = '
243 # Apache < 2.3
244 <IfModule !mod_authz_core.c>
245 Order allow,deny
246 Deny from all
247 Satisfy All
248 </IfModule>
249
250 # Apache ≥ 2.3
251 <IfModule mod_authz_core.c>
252 Require all denied
253 </IfModule>
254 ';
255 GeneralUtility::writeFile($htaccessFile, $htaccessContent);
256 }
257 }
258
259 /**
260 * Returns the path to the default log file.
261 * Uses the defaultLogFileTemplate and replaces the %s placeholder with a short MD5 hash
262 * based on a static string and the current encryption key.
263 *
264 * @return string
265 */
266 protected function getDefaultLogFileName()
267 {
268 $namePart = substr(GeneralUtility::hmac($this->defaultLogFileTemplate, 'defaultLogFile'), 0, 10);
269 if ($this->logFileInfix !== '') {
270 $namePart = $this->logFileInfix . '_' . $namePart;
271 }
272 return Environment::getVarPath() . sprintf($this->defaultLogFileTemplate, $namePart);
273 }
274
275 /**
276 * Allow serialization of logger - reinitialize log file on unserializing
277 */
278 public function __wakeup()
279 {
280 self::$logFileHandlesCount[$this->logFile]++;
281 $this->setLogFile($this->logFile ?: $this->getDefaultLogFileName());
282 }
283
284 /**
285 * Property 'logFile' should be kept
286 *
287 * @return array
288 */
289 public function __sleep(): array
290 {
291 return ['logFile'];
292 }
293 }