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..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; } @@ -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,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']); + } } \ No newline at end of file