Quickstart¶
Instantiate a logger for the current class¶
New in version 11.4: Feature: #95044 - Support autowired LoggerInterface injection
Constructor injection can be used to automatically instantiate the logger:
use Psr\Log\LoggerInterface;
class MyClass {
private LoggerInterface $logger;
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
}
Log¶
Log a simple message:
$this->logger->warning('Something went awry, check your configuration!');
Provide additional context information with the log message:
$this->logger->error('Passing {value} was unwise.', [
'value' => $value,
'other_data' => $foo,
]);
Values in the message string that should vary based on the error (such as
specifying what an invalid value was) should use placeholders, denoted by
{ }
. Provide the value for that placeholder in the context array.
$this->logger->warning()
etc. are only shorthands - you can also call
$this->logger->log()
directly and pass the severity level:
$this->logger->log(
\TYPO3\CMS\Core\Log\LogLevel::CRITICAL,
'This is an utter failure!'
);
Set logging output¶
TYPO3 has the FileWriter enabled by default
for warnings (LogLevel::WARNING
) and lower, so all matching log entries
are written to a file.
If the filename is not set, then the file will contain a hash like
var/log/typo3_<hash>.log
, for example
var/log/typo3_7ac500bce5.log
.
typo3temp/var/log/typo3_<hash>.log
, for example
typo3temp/var/log/typo3_7ac500bce5.log
.
A sample output looks like this:
Fri, 08 Mar 2013 09:45:00 +0100 [WARNING] request="5139a50bee3a1" component="TYPO3.Examples.Controller.DefaultController": Something went awry, check your configuration!
Fri, 08 Mar 2013 09:45:00 +0100 [ERROR] request="5139a50bee3a1" component="TYPO3.Examples.Controller.DefaultController": This was not a good idea - {"foo":"bar","bar":{}}
Fri, 08 Mar 2013 09:45:00 +0100 [CRITICAL] request="5139a50bee3a1" component="TYPO3.Examples.Controller.DefaultController": This is an utter failure!