8.5.x Drupal.php Drupal::logger($channel)
8.0.x Drupal.php Drupal::logger($channel)
8.1.x Drupal.php Drupal::logger($channel)
8.2.x Drupal.php Drupal::logger($channel)
8.3.x Drupal.php Drupal::logger($channel)
8.4.x Drupal.php Drupal::logger($channel)
8.6.x Drupal.php Drupal::logger($channel)

Returns a channel logger object.

Parameters

string $channel: The name of the channel. Can be any string, but the general practice is to use the name of the subsystem calling this.

Return value

\Psr\Log\LoggerInterface The logger for this channel.

File

core/lib/Drupal.php, line 670
Contains \Drupal.

Class

Drupal
Static Service Container wrapper.

Code

public static function logger($channel) {
  return static::getContainer()
    ->get('logger.factory')
    ->get($channel);
}

Comments

sagar.w’s picture

<?php
// Logs a notice
\Drupal::logger('my_module')->notice($message);
// Logs an error
\Drupal::logger('my_module')->error($message);
?>

jdgp77’s picture

We have many logs:

Action must be taken immediately:
\Drupal::logger('my_module')->alert($message);

Critical conditions:
\Drupal::logger('my_module')->critical($message);

Detailed debug information:
\Drupal::logger('my_module')->debug($message);

System is unusable:
\Drupal::logger('my_module')->emergency($message);

Runtime errors that do not require immediate action but should typically be logged and monitored:
\Drupal::logger('my_module')->error($message);
Tipical errors, as when the page doesn't load

Interesting events:
\Drupal::logger('my_module')->info($message);

Logs with an arbitrary level:
\Drupal::logger('my_module')->log($message);
(Ex: When a module was installed)

Normal but significant events:
\Drupal::logger('my_module')->notice($message);
(Normals events, as cron execution)

Exceptional occurrences that are not errors:
\Drupal::logger('my_module')->warning($message);

You can see this logs in: /admin/reports/dblog

This is very importan, because you can report to others developers or admins, to many importants thinks in yours modules.

japo32’s picture

Also don't use t() for logger messages.

$logger->alert('Message from @module: @message.', [
  '@module' => $module,
  '@message' => $message,
]);

The messages are stored untranslated and will be translated on runtime.