Skip to content

Commit

Permalink
Merge pull request #164 from City-of-Helsinki/UHF-9986
Browse files Browse the repository at this point in the history
UHF-9986: Monolog dependency
  • Loading branch information
tuutti authored Apr 23, 2024
2 parents 45489df + 179923f commit 0c5927f
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 213 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"require": {
"drupal/entity": "^1.0",
"drupal/health_check": "^3.0",
"drupal/monolog": "^3.0",
"drupal/raven": "^4.0 || ^5.0",
"firebase/php-jwt": "^6.5",
"php": "^8.1",
Expand Down
1 change: 0 additions & 1 deletion config/install/helfi_api_base.features.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
logger: true
disable_user_password: true
10 changes: 5 additions & 5 deletions documentation/feature-toggle.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ Provides a service to conditionally check if the given feature is enabled.
```php
$service = \Drupal::service(\Drupal\helfi_api_base\Features\FeatureManager::class);

$service->isEnabled(\Drupal\helfi_api_base\Features\FeatureManager::LOGGER); // Returns true if the logger feature is enabled.
// Disables the logger feature.
$service->disableFeature(\Drupal\helfi_api_base\Features\FeatureManager::LOGGER)
// Enables the logger feature.
$service->enableFeature(\Drupal\helfi_api_base\Features\FeatureManager::LOGGER);
$service->isEnabled(\Drupal\helfi_api_base\Features\FeatureManager::DISABLE_USER_PASSWORD);
// Disables the feature.
$service->disableFeature(\Drupal\helfi_api_base\Features\FeatureManager::DISABLE_USER_PASSWORD)
// Enables the feature.
$service->enableFeature(\Drupal\helfi_api_base\Features\FeatureManager::DISABLE_USER_PASSWORD);
```

## Development
Expand Down
12 changes: 3 additions & 9 deletions documentation/logging.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
# Logging

Errors caught by Drupal will be logged to `temporary://drupal.log` file as JSON, which will be piped to container stdout by [15-syslog.sh](https://github.com/City-of-Helsinki/drupal-docker-images/blob/main/openshift/drupal/files/entrypoints/15-syslog.sh) docker entrypoint.
Errors caught by Drupal will be logged to `php://stdout` by `drupal/monolog` module.

JSON parsing needs to be enabled manually in order for this to work. See https://helsinkisolutionoffice.atlassian.net/wiki/spaces/HELFI/pages/7854817294/Logging+Structured+logs#Configuration-using-openshift-console.

## How to disable logging
## Usage

Call:
```php
$service = \Drupal::service(\Drupal\helfi_api_base\Features\FeatureManager::class);
$service->disableFeature(\Drupal\helfi_api_base\Features\FeatureManager::LOGGER);
```

and export the changed configuration.
Enable `monolog` module: `drush en monolog`.
1 change: 1 addition & 0 deletions helfi_api_base.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ core_version_requirement: ^9 || ^10 || ^11
dependencies:
- drupal:migrate
- entity:entity
- monolog:monolog
- health_check:health_check
- raven:raven
11 changes: 9 additions & 2 deletions helfi_api_base.install
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,6 @@ function helfi_api_base_update_9014() : void {
function helfi_api_base_update_9015() : void {
/** @var \Drupal\helfi_api_base\Features\FeatureManager $service */
$service = \Drupal::service(FeatureManager::class);
$service->enableFeature(FeatureManager::LOGGER);

/** @var \Drupal\helfi_api_base\Environment\EnvironmentResolverInterface $environmentResolver */

$environmentResolver = \Drupal::service('helfi_api_base.environment_resolver');
Expand All @@ -244,3 +242,12 @@ function helfi_api_base_update_9016() : void {
->set('twig_tracing', TRUE)
->save();
}

/**
* Enable 'monolog' module.
*/
function helfi_api_base_update_9017(): void {
Drupal::service('module_installer')->install([
'monolog',
]);
}
12 changes: 0 additions & 12 deletions helfi_api_base.services.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
parameters:
helfi_api_base.json_logger_path: '/tmp/drupal.log'
helfi_api_base.environment_file: ''
helfi_api_base.default_composer_lock: '%app.root%/../composer.lock'
helfi_api_base.internal_domains:
Expand Down Expand Up @@ -98,17 +97,6 @@ services:
tags:
- { name: event_subscriber }

helfi_api_base.logger_filesystem:
class: Symfony\Component\Filesystem\Filesystem
public: false

Symfony\Component\Filesystem\Filesystem:
class: Symfony\Component\Filesystem\Filesystem
public: false
Drupal\helfi_api_base\Logger\JsonLog:
tags:
- { name: logger }

Drupal\helfi_api_base\Vault\VaultManager: '@helfi_api_base.vault_manager'
helfi_api_base.vault_manager:
class: Drupal\helfi_api_base\Vault\VaultManager
Expand Down
1 change: 0 additions & 1 deletion src/Features/FeatureManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
final class FeatureManager {

public const LOGGER = 'logger';
public const DISABLE_USER_PASSWORD = 'disable_user_password';

/**
Expand Down
65 changes: 65 additions & 0 deletions src/HelfiApiBaseServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace Drupal\helfi_api_base;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderBase;
use Drupal\monolog\Logger\Formatter\ConditionalFormatter;
use Drupal\monolog\Logger\Handler\ConditionalHandler;
use Drupal\monolog\Logger\Handler\DrupalHandler;
use Drush\Log\DrushLog;
use Monolog\Handler\StreamHandler;
use Symfony\Component\DependencyInjection\Reference;

/**
* A service provider for 'helfi_api_base' module.
*/
final class HelfiApiBaseServiceProvider extends ServiceProviderBase {

/**
* {@inheritdoc}
*/
public function register(ContainerBuilder $container) : void {
// We cannot use the module handler as the container is not yet compiled.
// @see \Drupal\Core\DrupalKernel::compileContainer()
$modules = $container->getParameter('container.modules');

if (isset($modules['monolog'])) {
$container->setParameter('monolog.channel_handlers', [
'default' => [
'handlers' => [
[
'name' => 'default_conditional_handler',
'formatter' => 'drush_or_json',
],
],
],
]);

if (!$container->has('logger.drupaltodrush')) {
$container->register('logger.drupaltodrush', DrushLog::class)
->addArgument(new Reference('logger.log_message_parser'))
->addTag('logger');
}
if (!$container->has('monolog.handler.drupal.drupaltodrush')) {
$container->register('monolog.handler.drupal.drupaltodrush', DrupalHandler::class)
->addArgument(new Reference('logger.drupaltodrush'))
->setShared(FALSE);
}
$container->register('monolog.handler.default_conditional_handler', ConditionalHandler::class)
->addArgument(new Reference('monolog.handler.drupal.drupaltodrush'))
->addArgument(new Reference('monolog.handler.website'))
->addArgument(new Reference('monolog.condition_resolver.cli'));
$container->register('monolog.handler.website', StreamHandler::class)
->addArgument('php://stdout');
$container->register('monolog.formatter.drush_or_json', ConditionalFormatter::class)
->addArgument(new Reference('monolog.formatter.drush'))
->addArgument(new Reference('monolog.formatter.json'))
->addArgument(new Reference('monolog.condition_resolver.cli'))
->setShared(FALSE);
}
}

}
103 changes: 0 additions & 103 deletions src/Logger/JsonLog.php

This file was deleted.

1 change: 0 additions & 1 deletion tests/src/Kernel/Features/FeatureToggleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ public function testDefaults() : void {

$this->assertEquals([
FeatureManager::DISABLE_USER_PASSWORD => TRUE,
FeatureManager::LOGGER => TRUE,
], $features);
}

Expand Down
79 changes: 0 additions & 79 deletions tests/src/Kernel/Logger/JsonLoggerTest.php

This file was deleted.

0 comments on commit 0c5927f

Please sign in to comment.