-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #180 from City-of-Helsinki/UHF-10751-log-levels
UHF-10751: Add log level to monolog
- Loading branch information
Showing
3 changed files
with
167 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\helfi_api_base\Logger; | ||
|
||
use Drupal\Core\Session\AccountProxyInterface; | ||
use Monolog\LogRecord; | ||
use Monolog\Processor\ProcessorInterface; | ||
|
||
/** | ||
* Processor that adds user information to the log records. | ||
* | ||
* Compared to monolog implementation, this does not log usernames. | ||
* | ||
* @see \Drupal\monolog\Logger\Processor\CurrentUserProcessor | ||
*/ | ||
class CurrentUserProcessor implements ProcessorInterface { | ||
|
||
/** | ||
* The current user. | ||
* | ||
* @var \Drupal\Core\Session\AccountProxyInterface | ||
*/ | ||
protected AccountProxyInterface $accountProxy; | ||
|
||
/** | ||
* Constructs a Default object. | ||
* | ||
* @param \Drupal\Core\Session\AccountProxyInterface $account_proxy | ||
* The current user. | ||
*/ | ||
public function __construct(AccountProxyInterface $account_proxy) { | ||
$this->accountProxy = $account_proxy; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __invoke(LogRecord $record): LogRecord { | ||
$record->extra = \array_merge( | ||
$record->extra, | ||
[ | ||
'uid' => $this->accountProxy->id(), | ||
], | ||
); | ||
|
||
return $record; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\Tests\helfi_api_base\Kernel; | ||
|
||
use Drupal\KernelTests\KernelTestBase; | ||
use Drupal\monolog\Logger\ConditionResolver\ConditionResolverInterface; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
|
||
/** | ||
* Tests monolog configuration. | ||
*/ | ||
final class MonologTest extends KernelTestBase { | ||
|
||
use ProphecyTrait; | ||
|
||
/** | ||
* Log level that is configured for the duration of this test. | ||
*/ | ||
private const TEST_LOG_LEVEL = 'warning'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected static $modules = [ | ||
'monolog', | ||
'helfi_api_base', | ||
]; | ||
|
||
/** | ||
* Temporary file where logs are stored. | ||
* | ||
* @var string | ||
*/ | ||
private string $logFile; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function bootKernel() : void { | ||
// Set test log level before kernel is booted. | ||
$this->setSetting('helfi_api_base.log_level', self::TEST_LOG_LEVEL); | ||
|
||
parent::bootKernel(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function setUp() : void { | ||
parent::setUp(); | ||
|
||
$this->logFile = 'temporary://' . $this->randomMachineName(); | ||
|
||
$this->container | ||
->getDefinition('monolog.handler.website') | ||
->replaceArgument(0, $this->logFile); | ||
|
||
// Pretend that we are running in web process so Drush log is not used. | ||
$this->container->set('monolog.condition_resolver.cli', new class implements ConditionResolverInterface { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resolve(): bool { | ||
return FALSE; | ||
} | ||
|
||
}); | ||
} | ||
|
||
/** | ||
* Tests logger message. | ||
*/ | ||
public function testLogging() : void { | ||
/** @var \Psr\Log\LoggerInterface $logger */ | ||
$logger = $this->container->get('logger.channel.default'); | ||
$logger->warning('Test warning message'); | ||
$logger->debug('Test debug message'); | ||
|
||
$log = file_get_contents($this->logFile); | ||
$this->assertNotFalse($log); | ||
|
||
// Debug messages were not logged due to the log level. | ||
$this->assertStringContainsString('Test warning message', $log); | ||
$this->assertStringNotContainsString('Test debug message', $log); | ||
|
||
foreach (explode('\n', $log) as $logLine) { | ||
// Message is valid JSON. | ||
$message = json_decode($logLine, flags: JSON_THROW_ON_ERROR); | ||
|
||
// Tests \Drupal\helfi_api_base\Logger\CurrentUserProcessor. | ||
$this->assertObjectNotHasProperty('user', $message->extra); | ||
} | ||
} | ||
|
||
} |