Skip to content

Commit c802e50

Browse files
emit an event when a message is logged
Signed-off-by: Robin Appelman <[email protected]>
1 parent 6fb44c7 commit c802e50

File tree

6 files changed

+114
-1
lines changed

6 files changed

+114
-1
lines changed

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@
470470
'OCP\\Lock\\ManuallyLockedException' => $baseDir . '/lib/public/Lock/ManuallyLockedException.php',
471471
'OCP\\Lockdown\\ILockdownManager' => $baseDir . '/lib/public/Lockdown/ILockdownManager.php',
472472
'OCP\\Log\\Audit\\CriticalActionPerformedEvent' => $baseDir . '/lib/public/Log/Audit/CriticalActionPerformedEvent.php',
473+
'OCP\\Log\\BeforeMessageLoggedEvent' => $baseDir . '/lib/public/Log/BeforeMessageLoggedEvent.php',
473474
'OCP\\Log\\IDataLogger' => $baseDir . '/lib/public/Log/IDataLogger.php',
474475
'OCP\\Log\\IFileBased' => $baseDir . '/lib/public/Log/IFileBased.php',
475476
'OCP\\Log\\ILogFactory' => $baseDir . '/lib/public/Log/ILogFactory.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
503503
'OCP\\Lock\\ManuallyLockedException' => __DIR__ . '/../../..' . '/lib/public/Lock/ManuallyLockedException.php',
504504
'OCP\\Lockdown\\ILockdownManager' => __DIR__ . '/../../..' . '/lib/public/Lockdown/ILockdownManager.php',
505505
'OCP\\Log\\Audit\\CriticalActionPerformedEvent' => __DIR__ . '/../../..' . '/lib/public/Log/Audit/CriticalActionPerformedEvent.php',
506+
'OCP\\Log\\BeforeMessageLoggedEvent' => __DIR__ . '/../../..' . '/lib/public/Log/BeforeMessageLoggedEvent.php',
506507
'OCP\\Log\\IDataLogger' => __DIR__ . '/../../..' . '/lib/public/Log/IDataLogger.php',
507508
'OCP\\Log\\IFileBased' => __DIR__ . '/../../..' . '/lib/public/Log/IFileBased.php',
508509
'OCP\\Log\\ILogFactory' => __DIR__ . '/../../..' . '/lib/public/Log/ILogFactory.php',

lib/private/EventDispatcher/EventDispatcher.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*/
2828
namespace OC\EventDispatcher;
2929

30+
use OC\Log;
3031
use Psr\Log\LoggerInterface;
3132
use function get_class;
3233
use OC\Broadcast\Events\BroadcastEvent;
@@ -54,6 +55,12 @@ public function __construct(SymfonyDispatcher $dispatcher,
5455
$this->dispatcher = $dispatcher;
5556
$this->container = $container;
5657
$this->logger = $logger;
58+
59+
// inject the event dispatcher into the logger
60+
// this is done here because there is a cyclic dependency between the event dispatcher and logger
61+
if ($this->logger instanceof Log or $this->logger instanceof Log\PsrLoggerAdapter) {
62+
$this->logger->setEventDispatcher($this);
63+
}
5764
}
5865

5966
public function addListener(string $eventName,

lib/private/Log.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
use Exception;
4040
use Nextcloud\LogNormalizer\Normalizer;
4141
use OC\AppFramework\Bootstrap\Coordinator;
42+
use OCP\EventDispatcher\IEventDispatcher;
43+
use OCP\Log\BeforeMessageLoggedEvent;
4244
use OCP\Log\IDataLogger;
4345
use Throwable;
4446
use function array_merge;
@@ -64,14 +66,20 @@ class Log implements ILogger, IDataLogger {
6466
private ?bool $logConditionSatisfied = null;
6567
private ?Normalizer $normalizer;
6668
private ?IRegistry $crashReporters;
69+
private ?IEventDispatcher $eventDispatcher;
6770

6871
/**
6972
* @param IWriter $logger The logger that should be used
7073
* @param SystemConfig $config the system config object
7174
* @param Normalizer|null $normalizer
7275
* @param IRegistry|null $registry
7376
*/
74-
public function __construct(IWriter $logger, SystemConfig $config = null, Normalizer $normalizer = null, IRegistry $registry = null) {
77+
public function __construct(
78+
IWriter $logger,
79+
SystemConfig $config = null,
80+
Normalizer $normalizer = null,
81+
IRegistry $registry = null
82+
) {
7583
// FIXME: Add this for backwards compatibility, should be fixed at some point probably
7684
if ($config === null) {
7785
$config = \OC::$server->getSystemConfig();
@@ -85,6 +93,11 @@ public function __construct(IWriter $logger, SystemConfig $config = null, Normal
8593
$this->normalizer = $normalizer;
8694
}
8795
$this->crashReporters = $registry;
96+
$this->eventDispatcher = null;
97+
}
98+
99+
public function setEventDispatcher(IEventDispatcher $eventDispatcher) {
100+
$this->eventDispatcher = $eventDispatcher;
88101
}
89102

90103
/**
@@ -203,6 +216,10 @@ public function log(int $level, string $message, array $context = []) {
203216
$app = $context['app'] ?? 'no app in context';
204217
$entry = $this->interpolateMessage($context, $message);
205218

219+
if ($this->eventDispatcher) {
220+
$this->eventDispatcher->dispatchTyped(new BeforeMessageLoggedEvent($app, $level, $entry));
221+
}
222+
206223
try {
207224
if ($level >= $minLevel) {
208225
$this->writeLog($app, $entry, $level);
@@ -322,6 +339,10 @@ public function logException(Throwable $exception, array $context = []) {
322339

323340
array_walk($context, [$this->normalizer, 'format']);
324341

342+
if ($this->eventDispatcher) {
343+
$this->eventDispatcher->dispatchTyped(new BeforeMessageLoggedEvent($app, $level, $data));
344+
}
345+
325346
try {
326347
if ($level >= $minLevel) {
327348
if (!$this->logger instanceof IFileBased) {

lib/private/Log/PsrLoggerAdapter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
namespace OC\Log;
2727

2828
use OC\Log;
29+
use OCP\EventDispatcher\IEventDispatcher;
2930
use OCP\ILogger;
3031
use OCP\Log\IDataLogger;
3132
use Psr\Log\InvalidArgumentException;
@@ -42,6 +43,10 @@ public function __construct(Log $logger) {
4243
$this->logger = $logger;
4344
}
4445

46+
public function setEventDispatcher(IEventDispatcher $eventDispatcher) {
47+
$this->logger->setEventDispatcher($eventDispatcher);
48+
}
49+
4550
private function containsThrowable(array $context): bool {
4651
return array_key_exists('exception', $context) && $context['exception'] instanceof Throwable;
4752
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* @copyright Copyright (c) 2023 Robin Appelman <[email protected]>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OCP\Log;
25+
26+
use OCP\EventDispatcher\Event;
27+
28+
/**
29+
* Even for when a log item is being logged
30+
*
31+
* @since 28.0.0
32+
*/
33+
class BeforeMessageLoggedEvent extends Event {
34+
private int $level;
35+
private string $app;
36+
private $message;
37+
38+
/**
39+
* @since 28.0.0
40+
*/
41+
public function __construct(string $app, int $level, $message) {
42+
$this->level = $level;
43+
$this->app = $app;
44+
$this->message = $message;
45+
}
46+
47+
/**
48+
* Get the level of the log item
49+
*
50+
* @return int
51+
* @since 28.0.0
52+
*/
53+
public function getLevel(): int {
54+
return $this->level;
55+
}
56+
57+
58+
/**
59+
* Get the app context of the log item
60+
*
61+
* @return string
62+
* @since 28.0.0
63+
*/
64+
public function getApp(): string {
65+
return $this->app;
66+
}
67+
68+
69+
/**
70+
* Get the message of the log item
71+
*
72+
* @return string
73+
* @since 28.0.0
74+
*/
75+
public function getMessage(): string {
76+
return $this->message;
77+
}
78+
}

0 commit comments

Comments
 (0)