Attention
TYPO3 v9 has reached its end-of-life September 30th, 2021 and is not maintained by the community anymore. Looking for a stable version? Use the version switch on the top left.
You can order Extended Long Term Support (ELTS) here: TYPO3 ELTS.
Quickstart¶
Instantiate a Logger for the Current Class¶
New in version 9.0: You no longer need to use makeInstance to create an
instance of the logger yourself. You can use LoggerAwareTrait:
Changelog/9.0/Feature-82441-InjectLoggerWhenCreatingObjects.
You must implement the \Psr\Log\LoggerAwareInterface
interface with your class to have the Trait taking effect.
Use LoggerAwareTrait in your class to automatically instantiate $this->logger
:
use Psr\Log\LoggerAwareTrait;
class Example implements \Psr\Log\LoggerAwareInterface
{
use LoggerAwareTrait;
protected function myFunction() {
$this->logger->info('entered function myFunction');
}
}
Or instantiate the logger in the classic way with makeInstance:
$this->logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Log\LogManager::class)->getLogger(__CLASS__);
Log¶
Log a simple message:
$this->logger->info('Everything went fine.');
$this->logger->warning('Something went awry, check your configuration!');
Provide additional information with the log message:
$this->logger->error(
'This was not a good idea',
array(
'foo' => $bar,
'bar' => $foo,
)
);
$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,
so all log entries are written to a file. If the filename is not set,
then the file will contain a hash like typo3temp/var/logs/typo3_<hash>.log
,
for example typo3temp/var/logs/typo3_7ac500bce5.log
.
In composer-based installations the file will be located in <project-root>/var/log
A sample output looks like this:
Fri, 08 Mar 2013 09:45:00 +0100 [INFO] request="5139a50bee3a1" component="TYPO3.Examples.Controller.DefaultController": Everything went fine.
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!