Skip to content

Commit 92d6461

Browse files
Merge pull request nextcloud#26582 from nextcloud/enhancement/logger-function
Add a helper function that makes it easier to log from anywhere
2 parents 407ba4a + 450136b commit 92d6461

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed

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/public/Log/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(__DIR__);
6+
$baseDir = dirname(dirname($vendorDir));
7+
8+
return array(
9+
'03ae51fe9694f2f597f918142c49ff7a' => $baseDir . '/lib/public/Log/functions.php',
10+
);

lib/composer/composer/autoload_real.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,25 @@ public static function getLoader()
3131

3232
$loader->register(true);
3333

34+
$includeFiles = \Composer\Autoload\ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2::$files;
35+
foreach ($includeFiles as $fileIdentifier => $file) {
36+
composerRequire749170dad3f5e7f9ca158f5a9f04f6a2($fileIdentifier, $file);
37+
}
38+
3439
return $loader;
3540
}
3641
}
42+
43+
/**
44+
* @param string $fileIdentifier
45+
* @param string $file
46+
* @return void
47+
*/
48+
function composerRequire749170dad3f5e7f9ca158f5a9f04f6a2($fileIdentifier, $file)
49+
{
50+
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
51+
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
52+
53+
require $file;
54+
}
55+
}

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 ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
88
{
9+
public static $files = array (
10+
'03ae51fe9694f2f597f918142c49ff7a' => __DIR__ . '/../../..' . '/lib/public/Log/functions.php',
11+
);
12+
913
public static $prefixLengthsPsr4 = array (
1014
'O' =>
1115
array (

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 2022 Christoph Wurst <[email protected]>
7+
*
8+
* @author 2022 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 24.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)