From 575b6756b94723cbf5645b0adac31af55717b22b Mon Sep 17 00:00:00 2001 From: Moshe Weitzman Date: Mon, 28 Mar 2022 12:58:40 -0400 Subject: [PATCH 1/2] Handle watchdog logs --- Dockerfile | 2 +- README.md | 2 +- src/MassLogStreamManager.php | 31 +++++++++++++++++++++++++++---- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index 31185ad..4b79a96 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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" ] diff --git a/README.md b/README.md index 946f1d5..ff73509 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/MassLogStreamManager.php b/src/MassLogStreamManager.php index 24f1154..75ea44e 100644 --- a/src/MassLogStreamManager.php +++ b/src/MassLogStreamManager.php @@ -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; @@ -49,4 +54,22 @@ 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); + // Add extra items to top level. + $extra = $record['extra']; + $context = $record['context']; + unset($extra['user'], $extra['base_url']); + $record = array_merge($record, $extra); + unset($record['datetime'], $record['context'], $record['extra']); + $record['logtype'] = 'drupal.watchdog'; + $this->log->addRecord($this->log->toMonologLevel($record['level_name']), json_encode($record), $context); + } } \ No newline at end of file From 9832e1e65abb1cf1a3b100c8e26f0631248c2d2f Mon Sep 17 00:00:00 2001 From: Moshe Weitzman Date: Tue, 29 Mar 2022 09:26:17 -0400 Subject: [PATCH 2/2] No need to merge into top level. --- src/MassLogStreamManager.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/MassLogStreamManager.php b/src/MassLogStreamManager.php index 75ea44e..7b72d05 100644 --- a/src/MassLogStreamManager.php +++ b/src/MassLogStreamManager.php @@ -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; } @@ -63,13 +63,9 @@ protected function processWatchdog($line) } $json = substr($line, $pos, strlen($line) - $pos); $record = json_decode($json, JSON_OBJECT_AS_ARRAY); - // Add extra items to top level. - $extra = $record['extra']; - $context = $record['context']; - unset($extra['user'], $extra['base_url']); - $record = array_merge($record, $extra); - unset($record['datetime'], $record['context'], $record['extra']); + unset($record['datetime'], $record['extra']['user'], $record['extra']['base_url']); $record['logtype'] = 'drupal.watchdog'; - $this->log->addRecord($this->log->toMonologLevel($record['level_name']), json_encode($record), $context); + $record['error_type'] = 'keep-until-drop-filter-is-removed'; + $this->log->addRecord($this->log->toMonologLevel($record['level_name']), json_encode($record), $record['context']); } } \ No newline at end of file