From a7d05294592bfb8237b06078870ff3cd68ef9ddb Mon Sep 17 00:00:00 2001 From: "Yusuf A. Hasan Miyan" Date: Fri, 18 Feb 2022 14:14:05 +0400 Subject: [PATCH] [DDS-1053] Added the Tide Logs module. When enabled and a Collector Code provided, it forwards logs to the SumoLogic HTTP collector. --- config/schema/tide_logs.schema.yml | 18 ++++++ src/Form/TideLogsSettingsForm.php | 65 ++++++++++++++++++++++ src/Logger/TideLogsLoggerFactory.php | 70 ++++++++++++++++++++++++ src/Monolog/Handler/SumoLogicHandler.php | 69 +++++++++++++++++++++++ tide_logs.info.yml | 9 +++ tide_logs.links.menu.yml | 5 ++ tide_logs.routing.yml | 7 +++ tide_logs.services.yml | 7 +++ 8 files changed, 250 insertions(+) create mode 100644 config/schema/tide_logs.schema.yml create mode 100644 src/Form/TideLogsSettingsForm.php create mode 100644 src/Logger/TideLogsLoggerFactory.php create mode 100644 src/Monolog/Handler/SumoLogicHandler.php create mode 100644 tide_logs.info.yml create mode 100644 tide_logs.links.menu.yml create mode 100644 tide_logs.routing.yml create mode 100644 tide_logs.services.yml diff --git a/config/schema/tide_logs.schema.yml b/config/schema/tide_logs.schema.yml new file mode 100644 index 0000000..eeb2d30 --- /dev/null +++ b/config/schema/tide_logs.schema.yml @@ -0,0 +1,18 @@ +# Schema for the configuration files of the tide_logs module. + +tide_logs.settings: + type: config_object + label: 'Tide logs settings' + mapping: + enable: + type: boolean + label: 'Enabled' + sumologic_collector_code: + type: string + label: 'Sumo Logic collector code' + sumologic_category: + type: string + label: 'Sumo Logic category' + debug: + type: boolean + label: 'Show debug messages' diff --git a/src/Form/TideLogsSettingsForm.php b/src/Form/TideLogsSettingsForm.php new file mode 100644 index 0000000..1a63964 --- /dev/null +++ b/src/Form/TideLogsSettingsForm.php @@ -0,0 +1,65 @@ +config('tide_logs.settings'); + + $form['enable'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Enable module'), + '#description' => $this->t('Send logs to SumoLogic.'), + '#default_value' => $config->get('enable'), + ]; + + $form['description'] = [ + '#prefix' => '
', + '#suffix' => '
', + '#markup' => $this->t( + '

Current settings for the Tide Logs module. The defaults are set in configuration, this page is meant primarily for troubleshooting.

' . + '' + ), + ]; + + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + parent::submitForm($form, $form_state); + $this->config('tide_logs.settings') + ->set('enable', $form_state->getValue('enable')) + ->save(); + } + +} diff --git a/src/Logger/TideLogsLoggerFactory.php b/src/Logger/TideLogsLoggerFactory.php new file mode 100644 index 0000000..e189a88 --- /dev/null +++ b/src/Logger/TideLogsLoggerFactory.php @@ -0,0 +1,70 @@ +get('tide_logs.settings')->get('enable'); + return $enabled ? + implode('-', [ + getenv('LAGOON_PROJECT') ?: LagoonLogsLoggerFactory::LAGOON_LOGS_DEFAULT_LAGOON_PROJECT, + getenv('LAGOON_GIT_SAFE_BRANCH') ?: LagoonLogsLoggerFactory::LAGOON_LOGS_DEFAULT_SAFE_BRANCH, + ]) : + FALSE; + } + + public static function getSumoLogicCollectorCode(ConfigFactoryInterface $config) { + $enabled = $config->get('tide_logs.settings')->get('enable'); + if (!$enabled) { + return FALSE; + } + // Allow collector code to be specified via environment. + $config_code = $config->get('tide_logs.settings')->get('sumologic_collector_code'); + return $config_code ?: getenv('SUMOLOGIC_COLLECTOR_CODE'); + } + + public static function getSumoLogicCategory(ConfigFactoryInterface $config) { + $enabled = $config->get('tide_logs.settings')->get('enable'); + if (!$enabled) { + return FALSE; + } + // Allow category to be specified via environment, otherwise default. + $category = $config->get('tide_logs.settings')->get('sumologic_category'); + if (!$category) { + $category = getenv('SUMOLOGIC_CATEGORY'); + } + return $category ?: static::TIDE_LOGS_DEFAULT_SUMOLOGIC_CATEGORY; + } + + public static function getDebug(ConfigFactoryInterface $config) + { + $enabled = $config->get('tide_logs.settings')->get('enable'); + if (!$enabled) { + return FALSE; + } + return $config->get('tide_logs.settings')->get('debug'); + } + +} diff --git a/src/Monolog/Handler/SumoLogicHandler.php b/src/Monolog/Handler/SumoLogicHandler.php new file mode 100644 index 0000000..ef242f8 --- /dev/null +++ b/src/Monolog/Handler/SumoLogicHandler.php @@ -0,0 +1,69 @@ +collectorCode = $collector_code; + $this->host = $host; + $this->category = $category; + parent::__construct($level, $bubble); + } + + /** + * Sets the GuzzleHttp Client. + */ + public function setClient(Client $client) + { + $this->client = $client; + } + + protected function write(array $record): void + { + $url = sprintf("https://%s/%s/%s/", static::HOST, static::ENDPOINT, $this->collectorCode); + + $headers = ['X-Sumo-Category' => $this->category]; + if ($this->host) { + $headers['X-Sumo-Host'] = $this->host; + } + + $this->client->post($url, [ + 'headers' => $headers, + 'json' => $record, + ]); + } + + protected function getDefaultFormatter(): FormatterInterface + { + return new JsonFormatter(); + } +} diff --git a/tide_logs.info.yml b/tide_logs.info.yml new file mode 100644 index 0000000..3670f13 --- /dev/null +++ b/tide_logs.info.yml @@ -0,0 +1,9 @@ +name: Tide Logs +description: Simple monolog wrapper for Sumo Logic. +package: Tide +configure: tide_logs.settings +type: module +core_version_requirement: ^8 || ^9 + +dependencies: + - lagoon_logs:lagoon_logs diff --git a/tide_logs.links.menu.yml b/tide_logs.links.menu.yml new file mode 100644 index 0000000..4fd29d1 --- /dev/null +++ b/tide_logs.links.menu.yml @@ -0,0 +1,5 @@ +system.tide_logs_settings: + title: 'Tide Logs settings' + parent: system.admin_config_development + description: 'Configure Tide Logs.' + route_name: tide_logs.settings diff --git a/tide_logs.routing.yml b/tide_logs.routing.yml new file mode 100644 index 0000000..e802ccb --- /dev/null +++ b/tide_logs.routing.yml @@ -0,0 +1,7 @@ +tide_logs.settings: + path: '/admin/config/development/tide_logs' + defaults: + _form: '\Drupal\tide_logs\Form\TideLogsSettingsForm' + _title: 'Tide Logs configuration' + requirements: + _permission: 'administer site configuration' diff --git a/tide_logs.services.yml b/tide_logs.services.yml new file mode 100644 index 0000000..2734a8e --- /dev/null +++ b/tide_logs.services.yml @@ -0,0 +1,7 @@ +services: + logger.tide_logs: + class: Drupal\tide_logs\Logger\TideLogsLogger + factory: Drupal\tide_logs\Logger\TideLogsLoggerFactory::create + tags: + - { name: logger } + arguments: ['@config.factory', '@logger.log_message_parser', '@http_client']