Skip to content

Commit d068d47

Browse files
Add a helper function that makes it easier to log from anywhere
Our DI is able to inject a logger implementation to any server and app class if they want one. However, sometimes DI isn't applicable or hard to add. In those cases we typically fell back to the *service locator* pattern where we acquired a logger from the server via a global variable. There were some issues with that * `\OC` is a private class, apps are not supposed to use it * `\OC::$server` is a global variable, a well known anti-pattern * `\OC::$server->get(...)` uses the service locator anti-pattern * `\OC::$server->get(...)` may throw * `\OC::$server->get(LoggerInterface::class)` is not scoped to an app With this patch I'm proposing a new helper function ``\OCP\Log\logger`` that can be used to acquire a logger more easily. This function is meant to be public API and therefore apps may use it and there is an optional parameter to specifiy the app ID. The function hides all the ugly details about the global variable and the potentially thrown exceptions from the user. Therefore it's guaranteed that you always get a logger instance. In the worst case you get a noop, though those occasions should be rare. Signed-off-by: Christoph Wurst <[email protected]>
1 parent 98fd66b commit d068d47

6 files changed

Lines changed: 115 additions & 2 deletions

File tree

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
}
1212
},
1313
"autoload": {
14+
"files": [
15+
"lib/functions.php"
16+
],
1417
"psr-4": {
1518
"": "lib/private/legacy",
1619
"OC\\": "lib/private",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
// autoload_files.php @generated by Composer
4+
5+
$vendorDir = dirname(dirname(__FILE__));
6+
$baseDir = dirname(dirname($vendorDir));
7+
8+
return array(
9+
'a13625b0b50fbf6c65490231df38b04b' => $baseDir . '/lib/functions.php',
10+
);

lib/composer/composer/autoload_real.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,29 @@ public static function getLoader()
5050

5151
$loader->register(true);
5252

53+
if ($useStaticLoader) {
54+
$includeFiles = Composer\Autoload\ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c::$files;
55+
} else {
56+
$includeFiles = require __DIR__ . '/autoload_files.php';
57+
}
58+
foreach ($includeFiles as $fileIdentifier => $file) {
59+
composerRequire53792487c5a8370acc0b06b1a864ff4c($fileIdentifier, $file);
60+
}
61+
5362
return $loader;
5463
}
5564
}
65+
66+
/**
67+
* @param string $fileIdentifier
68+
* @param string $file
69+
* @return void
70+
*/
71+
function composerRequire53792487c5a8370acc0b06b1a864ff4c($fileIdentifier, $file)
72+
{
73+
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
74+
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
75+
76+
require $file;
77+
}
78+
}

lib/composer/composer/autoload_static.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
88
{
9+
public static $files = array (
10+
'a13625b0b50fbf6c65490231df38b04b' => __DIR__ . '/../../..' . '/lib/functions.php',
11+
);
12+
913
public static $prefixLengthsPsr4 = array (
1014
'O' =>
1115
array (

lib/composer/composer/installed.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
'type' => 'library',
66
'install_path' => __DIR__ . '/../../../',
77
'aliases' => array(),
8-
'reference' => '1225189f74d06606aafc4150d07584b90cea50dd',
8+
'reference' => 'acdfa9eb7e43337958e4211e33b16932aff8e238',
99
'name' => '__root__',
1010
'dev' => false,
1111
),
@@ -16,7 +16,7 @@
1616
'type' => 'library',
1717
'install_path' => __DIR__ . '/../../../',
1818
'aliases' => array(),
19-
'reference' => '1225189f74d06606aafc4150d07584b90cea50dd',
19+
'reference' => 'acdfa9eb7e43337958e4211e33b16932aff8e238',
2020
'dev_requirement' => false,
2121
),
2222
),

lib/public/Log/functions.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* @copyright 2021 Christoph Wurst <[email protected]>
7+
*
8+
* @author 2021 Christoph Wurst <[email protected]>
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 OCP\Log;
27+
28+
use OC;
29+
use OCP\AppFramework\QueryException;
30+
use Psr\Log\LoggerInterface;
31+
use Psr\Log\NullLogger;
32+
use function class_exists;
33+
34+
/**
35+
* Get a PSR logger
36+
*
37+
* Whenever possible, inject a logger into your classes instead of relying on
38+
* this helper function.
39+
*
40+
* @warning the returned logger implementation is not guaranteed to be the same
41+
* between two function calls. During early stages of the process you
42+
* might in fact get a noop implementation when Nextcloud isn't ready
43+
* to log. Therefore you MUST NOT cache the result of this function but
44+
* fetch a new logger for every log line you want to write.
45+
*
46+
* @param string|null $appId optional parameter to acquire the app-specific logger
47+
*
48+
* @return LoggerInterface
49+
* @since 22.0.0
50+
*/
51+
function logger(string $appId = null): LoggerInterface {
52+
if (!class_exists(OC::class) || OC::$server === null) {
53+
// If someone calls this log before Nextcloud is initialized, there is
54+
// no logging available. In that case we return a noop implementation
55+
// TODO: evaluate whether logging to error_log could be an alternative
56+
return new NullLogger();
57+
}
58+
59+
if ($appId !== null) {
60+
try {
61+
$appContainer = OC::$server->getRegisteredAppContainer($appId);
62+
return $appContainer->get(LoggerInterface::class);
63+
} catch (QueryException $e) {
64+
// Ignore and return the server logger below
65+
}
66+
}
67+
68+
try {
69+
return OC::$server->get(LoggerInterface::class);
70+
} catch (QueryException $e) {
71+
return new NullLogger();
72+
}
73+
}

0 commit comments

Comments
 (0)