You are here

class Stdout in log_stdout 8.2

Same name and namespace in other branches
  1. 8 src/Logger/Stdout.php \Drupal\log_stdout\Logger\Stdout

Hierarchy

Expanded class hierarchy of Stdout

1 string reference to 'Stdout'
log_stdout.services.yml in ./log_stdout.services.yml
log_stdout.services.yml
1 service uses Stdout
logger.stdout in ./log_stdout.services.yml
Drupal\log_stdout\Logger\Stdout

File

src/Logger/Stdout.php, line 13

Namespace

Drupal\log_stdout\Logger
View source
class Stdout implements LoggerInterface {
  use RfcLoggerTrait;

  /**
   * A configuration object containing settings.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $config;

  /**
   * The message's placeholders parser.
   *
   * @var \Drupal\Core\Logger\LogMessageParserInterface
   */
  protected $parser;

  /**
   * Constructs a Stdout object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory object.
   * @param \Drupal\Core\Logger\LogMessageParserInterface $parser
   *   The parser to use when extracting message variables.
   */
  public function __construct(ConfigFactoryInterface $config_factory, LogMessageParserInterface $parser) {
    $this->config = $config_factory
      ->get('log_stdout.settings');
    $this->parser = $parser;
  }

  /**
   * {@inheritdoc}
   */
  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]);
    $username = $this
      ->getUserName($context);

    // 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 = '@date' . '|@timestamp' . '|@severity' . '|@type' . '|@message' . '|@uid' . '|@request_uri' . '|@referer' . '|@ip' . '|@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);
  }
  private function getUserName(array $context) {
    if (isset($context['user']) && !empty($context['user'])) {
      return $context['user']
        ->getAccountName();
    }
    return 'anonymous';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RfcLoggerTrait::alert public function
RfcLoggerTrait::critical public function
RfcLoggerTrait::debug public function
RfcLoggerTrait::emergency public function
RfcLoggerTrait::error public function
RfcLoggerTrait::info public function
RfcLoggerTrait::notice public function
RfcLoggerTrait::warning public function
Stdout::$config protected property A configuration object containing settings.
Stdout::$parser protected property The message's placeholders parser.
Stdout::getUserName private function
Stdout::log public function Overrides RfcLoggerTrait::log
Stdout::__construct public function Constructs a Stdout object.