Attention

TYPO3 v10 has reached end-of-life as of April 30th 2023 and is no longer being maintained. Use the version switcher on the top left of this page to select documentation for a supported version of TYPO3.

Need more time before upgrading? You can purchase Extended Long Term Support (ELTS) for TYPO3 v10 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: Feature: #82441 - Inject logger when creating objects. 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.

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!