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.

Logger

Instantiation

New in version 9.0: You no longer need to call makeInstance to create an instance of the logger. You can use the LoggerAwareTrait: Changelog/9.0/Feature-82441-InjectLoggerWhenCreatingObjects

Use the LoggerAwareTrait in your class to automatically instantiate $this->logger:

use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;

class Example implements LoggerAwareInterface
{
   use LoggerAwareTrait;
}

Or, you can instantiate the Logger with makeInstance.

The LogManager enables an auto-configured usage of loggers in your PHP code by reading the logging configuration and setting the minimum severity level of the Logger accordingly.

/** @var $logger \TYPO3\CMS\Core\Log\Logger */
$this->logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Log\LogManager::class)->getLogger(__CLASS__);

Using __CLASS__ as name for the logger is recommended to enable logging configuration based on the class hierarchy.

Log() Method

\TYPO3\CMS\Core\Log\Logger provides a central point for submitting log messages, the log() method:

$this->logger->log($level, $message, $data);

which takes three parameters:

Parameter

Type

Description

$level

Type integer

One of either:

  • \TYPO3\CMS\Core\Log\LogLevel::EMERGENCY

  • \TYPO3\CMS\Core\Log\LogLevel::ALERT

  • \TYPO3\CMS\Core\Log\LogLevel::CRITICAL

  • \TYPO3\CMS\Core\Log\LogLevel::ERROR

  • \TYPO3\CMS\Core\Log\LogLevel::WARNING

  • \TYPO3\CMS\Core\Log\LogLevel::NOTICE

  • \TYPO3\CMS\Core\Log\LogLevel::INFO

  • \TYPO3\CMS\Core\Log\LogLevel::DEBUG

$message

Type string

The log message itself.

$data

Type array

Optional parameter, can contain additional data, which is added to the log record in the form of an array.

An early return in the log() method prevents unneeded computation work to be done. So you are safe to call $this->logger->debug() frequently without slowing down your code too much. The Logger will know by its configuration, what the most explicit severity level is.

As next step, all registered Processors are notified. They can modify the log records or add extra information.

The Logger then forwards the log records to all of its configured Writers, which will then persist the log record.

Shorthand Methods

For each of the severity levels mentioned above, a shorthand method exists in \TYPO3\CMS\Core\Log\Logger, like

  • $this->logger->debug($message, array $data = array());

  • $this->logger->info($message, array $data = array());

  • $this->logger->notice($message, array $data = array());

  • etc.