Skip to content

Commit c80d1bd

Browse files
Add a PSR-3 logger adapter and make it injectable
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
1 parent fceb7ae commit c80d1bd

4 files changed

Lines changed: 192 additions & 0 deletions

File tree

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,7 @@
10651065
'OC\\Log\\File' => $baseDir . '/lib/private/Log/File.php',
10661066
'OC\\Log\\LogDetails' => $baseDir . '/lib/private/Log/LogDetails.php',
10671067
'OC\\Log\\LogFactory' => $baseDir . '/lib/private/Log/LogFactory.php',
1068+
'OC\\Log\\PsrLoggerAdapter' => $baseDir . '/lib/private/Log/PsrLoggerAdapter.php',
10681069
'OC\\Log\\Rotate' => $baseDir . '/lib/private/Log/Rotate.php',
10691070
'OC\\Log\\Syslog' => $baseDir . '/lib/private/Log/Syslog.php',
10701071
'OC\\Log\\Systemdlog' => $baseDir . '/lib/private/Log/Systemdlog.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
10941094
'OC\\Log\\File' => __DIR__ . '/../../..' . '/lib/private/Log/File.php',
10951095
'OC\\Log\\LogDetails' => __DIR__ . '/../../..' . '/lib/private/Log/LogDetails.php',
10961096
'OC\\Log\\LogFactory' => __DIR__ . '/../../..' . '/lib/private/Log/LogFactory.php',
1097+
'OC\\Log\\PsrLoggerAdapter' => __DIR__ . '/../../..' . '/lib/private/Log/PsrLoggerAdapter.php',
10971098
'OC\\Log\\Rotate' => __DIR__ . '/../../..' . '/lib/private/Log/Rotate.php',
10981099
'OC\\Log\\Syslog' => __DIR__ . '/../../..' . '/lib/private/Log/Syslog.php',
10991100
'OC\\Log\\Systemdlog' => __DIR__ . '/../../..' . '/lib/private/Log/Systemdlog.php',
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
<?php
2+
/**
3+
* @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
4+
*
5+
* @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
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+
declare(strict_types=1);
24+
25+
/**
26+
* @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
27+
*
28+
* @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
29+
*
30+
* @license GNU AGPL version 3 or any later version
31+
*
32+
* This program is free software: you can redistribute it and/or modify
33+
* it under the terms of the GNU Affero General Public License as
34+
* published by the Free Software Foundation, either version 3 of the
35+
* License, or (at your option) any later version.
36+
*
37+
* This program is distributed in the hope that it will be useful,
38+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
39+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40+
* GNU Affero General Public License for more details.
41+
*
42+
* You should have received a copy of the GNU Affero General Public License
43+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
44+
*/
45+
46+
namespace OC\Log;
47+
48+
use OCP\ILogger;
49+
use Psr\Log\InvalidArgumentException;
50+
use Psr\Log\LoggerInterface;
51+
52+
final class PsrLoggerAdapter implements LoggerInterface {
53+
54+
/** @var ILogger */
55+
private $logger;
56+
57+
public function __construct(ILogger $logger) {
58+
$this->logger = $logger;
59+
}
60+
61+
/**
62+
* System is unusable.
63+
*
64+
* @param string $message
65+
* @param array $context
66+
*
67+
* @return void
68+
*/
69+
public function emergency($message, array $context = []): void {
70+
$this->logger->emergency($message, $context);
71+
}
72+
73+
/**
74+
* Action must be taken immediately.
75+
*
76+
* Example: Entire website down, database unavailable, etc. This should
77+
* trigger the SMS alerts and wake you up.
78+
*
79+
* @param string $message
80+
* @param array $context
81+
*
82+
* @return void
83+
*/
84+
public function alert($message, array $context = []) {
85+
$this->logger->alert($message, $context);
86+
}
87+
88+
/**
89+
* Critical conditions.
90+
*
91+
* Example: Application component unavailable, unexpected exception.
92+
*
93+
* @param string $message
94+
* @param array $context
95+
*
96+
* @return void
97+
*/
98+
public function critical($message, array $context = []) {
99+
$this->logger->critical($message, $context);
100+
}
101+
102+
/**
103+
* Runtime errors that do not require immediate action but should typically
104+
* be logged and monitored.
105+
*
106+
* @param string $message
107+
* @param array $context
108+
*
109+
* @return void
110+
*/
111+
public function error($message, array $context = []) {
112+
$this->logger->error($message, $context);
113+
}
114+
115+
/**
116+
* Exceptional occurrences that are not errors.
117+
*
118+
* Example: Use of deprecated APIs, poor use of an API, undesirable things
119+
* that are not necessarily wrong.
120+
*
121+
* @param string $message
122+
* @param array $context
123+
*
124+
* @return void
125+
*/
126+
public function warning($message, array $context = []) {
127+
$this->logger->warning($message, $context);
128+
}
129+
130+
/**
131+
* Normal but significant events.
132+
*
133+
* @param string $message
134+
* @param array $context
135+
*
136+
* @return void
137+
*/
138+
public function notice($message, array $context = []) {
139+
$this->logger->notice($message, $context);
140+
}
141+
142+
/**
143+
* Interesting events.
144+
*
145+
* Example: User logs in, SQL logs.
146+
*
147+
* @param string $message
148+
* @param array $context
149+
*
150+
* @return void
151+
*/
152+
public function info($message, array $context = []) {
153+
$this->logger->info($message, $context);
154+
}
155+
156+
/**
157+
* Detailed debug information.
158+
*
159+
* @param string $message
160+
* @param array $context
161+
*
162+
* @return void
163+
*/
164+
public function debug($message, array $context = []) {
165+
$this->logger->debug($message, $context);
166+
}
167+
168+
/**
169+
* Logs with an arbitrary level.
170+
*
171+
* @param mixed $level
172+
* @param string $message
173+
* @param array $context
174+
*
175+
* @return void
176+
*
177+
* @throws InvalidArgumentException
178+
*/
179+
public function log($level, $message, array $context = []) {
180+
if (!is_int($level) || $level < ILogger::DEBUG || $level > ILogger::FATAL) {
181+
throw new InvalidArgumentException('Nextcloud allows only integer log levels');
182+
}
183+
$this->logger->log($level, $message, $context);
184+
}
185+
186+
}

lib/private/Server.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
use OC\Lock\NoopLockingProvider;
102102
use OC\Lockdown\LockdownManager;
103103
use OC\Log\LogFactory;
104+
use OC\Log\PsrLoggerAdapter;
104105
use OC\Mail\Mailer;
105106
use OC\Memcache\ArrayCache;
106107
use OC\Memcache\Factory;
@@ -184,6 +185,7 @@
184185
use OCP\User\Events\UserLoggedInEvent;
185186
use OCP\User\Events\UserLoggedInWithCookieEvent;
186187
use OCP\User\Events\UserLoggedOutEvent;
188+
use Psr\Log\LoggerInterface;
187189
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
188190
use Symfony\Component\EventDispatcher\GenericEvent;
189191

@@ -678,6 +680,8 @@ public function __construct($webRoot, \OC\Config $config) {
678680
});
679681
$this->registerAlias(\OCP\ILogger::class, \OC\Log::class);
680682
$this->registerAlias('Logger', \OC\Log::class);
683+
// PSR-3 logger
684+
$this->registerAlias(LoggerInterface::class, PsrLoggerAdapter::class);
681685

682686
$this->registerService(ILogFactory::class, function (Server $c) {
683687
return new LogFactory($c, $this->getSystemConfig());

0 commit comments

Comments
 (0)