3939use Exception ;
4040use Nextcloud \LogNormalizer \Normalizer ;
4141use OC \AppFramework \Bootstrap \Coordinator ;
42+ use OCP \EventDispatcher \IEventDispatcher ;
43+ use OCP \Log \BeforeMessageLoggedEvent ;
4244use OCP \Log \IDataLogger ;
4345use Throwable ;
4446use 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) {
0 commit comments