You are here

public function JsonLog::log in JSONlog 8

Same name and namespace in other branches
  1. 8.2 src/Logger/JsonLog.php \Drupal\jsonlog\Logger\JsonLog::log()
  2. 3.x src/Logger/JsonLog.php \Drupal\jsonlog\Logger\JsonLog::log()

Logs any log statement - at or above a certain severity threshold - to a custom log file as JSON.

Overrides RfcLoggerTrait::log

File

src/Logger/JsonLog.php, line 114

Class

JsonLog
Redirects logging messages to jsonlog.

Namespace

Drupal\jsonlog\Logger

Code

public function log($level, $message, array $context = []) {
  if (!($log_entry = $this
    ->prepareLog($level, $message, $context))) {
    return;
  }

  // File append, using lock (write, doesn't prevent reading).
  // If failure: log filing error to web server's default log.
  if ($this->config
    ->get('jsonlog_newline_prepend') ?? JsonLog::JSONLOG_NEWLINE_PREPEND) {
    $prepend = "\n";
    $append = '';
  }
  else {
    $prepend = '';
    $append = "\n";
  }
  if (!file_put_contents($this->file, $prepend . $log_entry
    ->getJson() . $append, FILE_APPEND | LOCK_EX)) {
    error_log('Drupal jsonlog, site ID[' . $this->site_id . '], failed to write to file[' . $this->file . '].');
  }
  unset($log_entry);
}