Skip to content

Commit 1b4380a

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

4 files changed

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

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;
@@ -222,6 +223,7 @@
222223
use OCP\User\Events\UserLoggedInEvent;
223224
use OCP\User\Events\UserLoggedInWithCookieEvent;
224225
use OCP\User\Events\UserLoggedOutEvent;
226+
use Psr\Log\LoggerInterface;
225227
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
226228
use Symfony\Component\EventDispatcher\GenericEvent;
227229
use OCA\Files_External\Service\UserStoragesService;
@@ -720,6 +722,8 @@ public function __construct($webRoot, \OC\Config $config) {
720722
});
721723
$this->registerAlias(ILogger::class, \OC\Log::class);
722724
$this->registerDeprecatedAlias('Logger', \OC\Log::class);
725+
// PSR-3 logger
726+
$this->registerAlias(LoggerInterface::class, PsrLoggerAdapter::class);
723727

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

0 commit comments

Comments
 (0)