Skip to content

Commit

Permalink
Merge pull request #3 from massgov/feature/DP-24384_handle-watchdog-logs
Browse files Browse the repository at this point in the history
Handle watchdog logs
  • Loading branch information
tom-fleming authored Mar 29, 2022
2 parents 1260f3c + 9832e1e commit b366ed7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ WORKDIR /usr/src/myapp
RUN apt-get update && apt-get install -y libzip-dev zip && docker-php-ext-install zip
COPY --from=composer /usr/bin/composer /usr/bin/composer
RUN composer install
CMD [ "php", "./index.php", "mass:logstream", "--logtypes=varnish-request" ]
CMD [ "php", "./index.php", "mass:logstream", "--logtypes=varnish-request", "--logtypes=drupal-watchdog" ]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Fetch logs from Acquia Logstream, massage, and POST to New Relic Logs.

#### Usage

./index.php mass:logstream --logtypes=varnish-request
./index.php mass:logstream --logtypes=varnish-request --logtypes=drupal-watchdog

- Other logtypes can be fetched but their records are not parsed correctly as they dont get delivered in JSON but rather in unparsed log lines.
- Redirect stdOut if you dont want to see log lines in the console.
Expand Down
29 changes: 24 additions & 5 deletions src/MassLogStreamManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(InputInterface $input, OutputInterface $output)
$log->pushProcessor(new Processor);
$handler = new Handler;
$handler->setLicenseKey(getenv('NR_LICENSE_KEY'));
$records_until_http_send = 50;
$records_until_http_send = getenv('NUM_BUFFER') ?: 50;
$log->pushHandler(new BufferHandler($handler, $records_until_http_send, Logger::DEBUG, true, true));
$this->log = $log;
}
Expand All @@ -33,14 +33,19 @@ protected function processMessage($msg)
{
$message = json_decode($msg);
if ($message->cmd === 'line') {
$json = $this->enrichJson($message->text);
$verb = $message->http_status >= 400 ? 'error' : 'info';
$this->log->$verb($json);
if ($message->log_type == 'varnish-request') {
$json = $this->processVarnish($message->text);
$verb = $message->http_status >= 400 ? 'error' : 'info';
$this->log->$verb($json);
}
elseif ($message->log_type == 'drupal-watchdog') {
$this->processWatchdog($message->text);
}
}
return parent::processMessage($msg);
}

protected function enrichJson($json)
protected function processVarnish($json)
{
$record = json_decode($json);
$time = $record->time;
Expand All @@ -49,4 +54,18 @@ protected function enrichJson($json)
$record->logtype = 'varnish.request';
return json_encode($record);
}

protected function processWatchdog($line)
{
$pos = strpos($line, '{');
if (!$pos) {
return;
}
$json = substr($line, $pos, strlen($line) - $pos);
$record = json_decode($json, JSON_OBJECT_AS_ARRAY);
unset($record['datetime'], $record['extra']['user'], $record['extra']['base_url']);
$record['logtype'] = 'drupal.watchdog';
$record['error_type'] = 'keep-until-drop-filter-is-removed';
$this->log->addRecord($this->log->toMonologLevel($record['level_name']), json_encode($record), $record['context']);
}
}

0 comments on commit b366ed7

Please sign in to comment.