You are here

public function Stdout::log in log_stdout 8

Same name and namespace in other branches
  1. 8.2 src/Logger/Stdout.php \Drupal\log_stdout\Logger\Stdout::log()

Overrides RfcLoggerTrait::log

File

src/Logger/Stdout.php, line 47

Class

Stdout
This class allows logging to stdout and stderr.

Namespace

Drupal\log_stdout\Logger

Code

public function log($level, $message, array $context = []) {
  global $base_url;
  if ($this->config
    ->get('use_stderr') == '1' && $level <= RfcLogLevel::WARNING) {
    $output = fopen('php://stderr', 'w');
  }
  else {
    $output = fopen('php://stdout', 'w');
  }
  $severity = strtoupper(RfcLogLevel::getLevels()[$level]);

  // Populate the message placeholders and then replace them in the message.
  $variables = $this->parser
    ->parseMessagePlaceholders($message, $context);
  $message = empty($variables) ? $message : strtr($message, $variables);
  $fmt = $this->config
    ->get('format');
  if (empty($fmt)) {
    $fmt = '[@severity] [@type] [@date] @message | uid: @uid | request-uri: @request_uri | refer: @referer | ip:  @ip | link: @link';
  }
  $entry = strtr($fmt, [
    '@base_url' => $base_url,
    '@timestamp' => $context['timestamp'],
    '@severity' => $severity,
    '@type' => $context['channel'],
    '@message' => strip_tags($message),
    '@uid' => $context['uid'],
    '@request_uri' => $context['request_uri'],
    '@referer' => $context['referer'],
    '@ip' => $context['ip'],
    '@link' => strip_tags($context['link']),
    '@date' => date('Y-m-d\\TH:i:s', $context['timestamp']),
  ]);
  fwrite($output, $entry . "\r\n");
  fclose($output);
}