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.

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: Feature: #82441 - Inject logger when creating objects

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 level

Log levels according to RFC 3164, starting from the lowest level.

Debug

| debug information | \TYPO3\CMS\Core\Log\LogLevel::DEBUG |

Detailed status information during the development of new PHP code.

Informational

| informational messages | \TYPO3\CMS\Core\Log\LogLevel::INFO |

User logs in, SQL logs.

Notice

| significant condition | \TYPO3\CMS\Core\Log\LogLevel::NOTICE |

Things you should have a look at, nothing to worry about though.

Warning

| warning condition | \TYPO3\CMS\Core\Log\LogLevel::WARNING |

Use of deprecated APIs. Undesirable events that are not necessarily wrong.

Error

| error condition | \TYPO3\CMS\Core\Log\LogLevel::ERROR |

Runtime error. Some PHP coding error has happened. A white screen is shown.

Critical

| critical condition | \TYPO3\CMS\Core\Log\LogLevel::CRITICAL |

Unexpected exception. An important file has not been found, data is corrupt or outdated.

Alert

| blocking condition | \TYPO3\CMS\Core\Log\LogLevel::ALERT |

Action must be taken immediately. Entire website down, database unavailable.

Emergency

| nothing works | \TYPO3\CMS\Core\Log\LogLevel::EMERGENCY |

The system is unusable. You will likely not be able to reach the system. You better have a system admin reachable when this happens.

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

See the chapter above.

$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.

Best Practices

There are no strict rules or guidelines about logging. Still it can be considered to be best practice to follow these rules:

Meaningful Message

The message itself has to be meaningful, for example exception messages.

Bad example:

"Something went wrong"

Good example:

"Could not connect to database"

Searchable Message

Most of the times log entries will be stored. They are most important if something goes wrong within the system. In such situations people might search for specific issues or situations, considering this while writing log entries will reduce debugging time in future.

Messages should therefore contain keywords that might be used in searches.

Good example:

"Connection to mysql database could not be established"

This includes "connection", "mysql" and "database" as possible keywords.

Distinguishable and Grouped

Log entries might be collected and people might scroll through them. Therefore it is helpful to write log entries that are distinguishable, but are also grouped.

Bad examples:

"Connection to mysql database could not be established."
"Could not establish connection to memcache."

Good examples:

"Connection to mysql database could not be established."
"Connection to memcache could not be established."

This way the same issue is grouped by the same structure, and one can scan the same position for either "mysql" or "memcache".

Provide Useful Information

TYPO3 already uses the component of the logger to give some context. Still further individual context might be available that should be added. In case of an exception, the code, stacktrace, file and line number would be helpful.

Keep in mind that it is hard to add information afterwards. Logging is there to get information if something got wrong. All necessary information should be available to get the state of the system and why something happened.