Logging

Magento supports the messages logging mechanism via its PsrLogLoggerInterface class. The LoggerInterface class has a preference defined within app/etc/di.xml file for the MagentoFrameworkLoggerMonolog class type. The actual crux of implementation is actually in the Monolog parent class named MonologLogger, which comes from the Monolog vendor.

The LoggerInterface class uses the following eight methods to write logs to the eight RFC 5424 levels:

  • debug
  • info
  • notice
  • warning
  • error
  • critical
  • alert
  • emergency

To use a logger, we need to pass the LoggerInterface class to a constructor of a class from within we want to use it and then simply make one of the following method calls:

$this->logger->log(MonologLogger::DEBUG, 'debug msg');
$this->logger->log(MonologLogger::INFO, 'info msg');
$this->logger->log(MonologLogger::NOTICE, 'notice msg');
$this->logger->log(MonologLogger::WARNING, 'warning msg');
$this->logger->log(MonologLogger::ERROR, 'error msg');
$this->logger->log(MonologLogger::CRITICAL, 'critical msg');
$this->logger->log(MonologLogger::ALERT, 'alert msg');
$this->logger->log(MonologLogger::EMERGENCY, 'emergency msg');

Alternatively, the preferred shorter version through individual log level type methods is as follows:

$this->logger->debug('debug msg');
$this->logger->info('info msg');
$this->logger->notice('notice msg');
$this->logger->warning('warning msg');
$this->logger->error('error msg');
$this->logger->critical('critical msg');
$this->logger->alert('alert msg');
$this->logger->emergency('emergency msg');

Both approaches result in the same two log files being created in Magento, which are as follows:

  • var/log/debug.log
  • var/log/system.log

The debug.log file contains only the debug level type of the log, while the rest are saved under system.log.

Entries within these logs will then look like this:

[2015-11-21 09:42:18] main.DEBUG: debug msg {"is_exception":false} []
[2015-11-21 09:42:18] main.INFO: info msg [] []
[2015-11-21 09:42:18] main.NOTICE: notice msg [] []
[2015-11-21 09:42:18] main.WARNING: warning msg [] []
[2015-11-21 09:42:18] main.ERROR: error msg [] []
[2015-11-21 09:42:18] main.CRITICAL: critical msg [] []
[2015-11-21 09:42:18] main.ALERT: alert msg [] []
[2015-11-21 09:42:18] main.EMERGENCY: emergency msg [] []

Each of these logger methods can accept an entire array of arbitrary data called context, as follows:

$this->logger->info('User logged in.', ['user'=>'Branko', 'age'=>32]);

The preceding expression will produce the following entry in system.log:

[2015-11-21 09:42:18] main.INFO: User logged in. {"user":"Branko","age":32} []

Tip

We can manually delete any of the .log files from the var/log directory, and Magento will automatically create it again when needed.

Magento also has another logging mechanism in place, where it logs the following actions in the log_* tables in a database:

  • log_customer
  • log_quote
  • log_summary
  • log_summary_type
  • log_url
  • log_url_info
  • log_visitorz
  • log_visitor_info
  • log_visitor_online

It is worth noting that this database logging is not related in any way to Psr logger that was described previously. While Psr logger serves developers within the code to group and log certain messages according to the Psr standard, the database logging logs the live data that is a result of user/customer interaction in the browser.

By default, Magento keeps database logs for around 180 days. This is a configurable option that can be controlled in the Magento admin area under the Stores | Settings | Configuration | Advanced | System | Log Cleaning tab with other log related options, as shown in the following screenshot:

Logging

Configuration options that are shown in the preceding screenshot only bare meaning operating system cron is triggering Magento cron.

Tip

We can execute two commands on terminal: php bin/magento log:status to get the current state information about log tables and php bin/magento log:clean to force the clearing of tables.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.19.30.232