Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(occ): Add a --debug option to output all log levels to the output #46115

Merged
merged 4 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions console.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,16 @@ function exceptionHandler($exception) {
$eventLogger->start('console:build_application', 'Build Application instance and load commands');

$application = \OCP\Server::get(Application::class);
$application->loadCommands(new ArgvInput(), new ConsoleOutput());
/* base.php will have removed eventual debug options from argv in $_SERVER */
$argv = $_SERVER['argv'];
$input = new ArgvInput($argv);
$application->loadCommands($input, new ConsoleOutput());

$eventLogger->end('console:build_application');
$eventLogger->start('console:run', 'Run the command');

$application->setAutoExit(false);
$exitCode = $application->run();
$exitCode = $application->run($input);

$eventLogger->end('console:run');

Expand Down
68 changes: 68 additions & 0 deletions core/Listener/BeforeMessageLoggedEventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OC\Core\Listener;

use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\EventDispatcher\IEventListener;
use OCP\Log\BeforeMessageLoggedEvent;
use OCP\Server;

/**
* Listen to log calls and output them to STDOUT for debug purposes
* @template-implements IEventListener<BeforeMessageLoggedEvent>
*/
class BeforeMessageLoggedEventListener implements IEventListener {
public function __construct(
private int $level,
) {
}

public function handle(Event $event): void {
if (!$event instanceof BeforeMessageLoggedEvent) {
return;
}
if ($event->getLevel() < $this->level) {
return;
}
echo
match($event->getLevel()) {
0 => '[debug]',
1 => '[info]',
2 => '[warning]',
3 => '[error]',
4 => '[fatal]',
default => '['.$event->getLevel().']',
}
.' ['.$event->getApp().'] '
.$event->getMessage()['message']
."\n";
}

/**
* Register listener to log messages and remove debug options from $_SERVER['argv']
*/
public static function setup(): void {
$eventDispatcher = Server::get(IEventDispatcher::class);
$argv = $_SERVER['argv'];
$level = 0;
foreach ($argv as $key => $arg) {
if ($arg === '--debug-log') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we put them into OCP somewhere so app devs have a list/track of "reserved arguments"?

unset($argv[$key]);
} elseif (str_starts_with($arg, '--debug-log-level=')) {
$level = (int)substr($arg, strlen('--debug-log-level='));
unset($argv[$key]);
}
}
$_SERVER['argv'] = array_values($argv);
$debugLoggerEventListener = new self($level);
$eventDispatcher->addListener(BeforeMessageLoggedEvent::class, $debugLoggerEventListener->handle(...));
}
}
5 changes: 5 additions & 0 deletions lib/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
// slash which is required by URL generation.
if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] === \OC::$WEBROOT &&
substr($_SERVER['REQUEST_URI'], -1) !== '/') {
header('Location: '.\OC::$WEBROOT.'/');

Check failure on line 144 in lib/base.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-security

TaintedHeader

lib/base.php:144:12: TaintedHeader: Detected tainted header (see https://psalm.dev/256)
exit();
}
}
Expand Down Expand Up @@ -223,7 +223,7 @@
throw new Exception('Not installed');
} else {
$url = OC::$WEBROOT . '/index.php';
header('Location: ' . $url);

Check failure on line 226 in lib/base.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-security

TaintedHeader

lib/base.php:226:12: TaintedHeader: Detected tainted header (see https://psalm.dev/256)
}
exit();
}
Expand Down Expand Up @@ -556,6 +556,7 @@
self::$composerAutoloader = require_once OC::$SERVERROOT . '/lib/composer/autoload.php';
self::$composerAutoloader->setApcuPrefix(null);


try {
self::initPaths();
// setup 3rdparty autoloader
Expand All @@ -578,6 +579,10 @@
self::$server = new \OC\Server(\OC::$WEBROOT, self::$config);
self::$server->boot();

if (self::$CLI && in_array('--debug-log', $_SERVER['argv'])) {
artonge marked this conversation as resolved.
Show resolved Hide resolved
\OC\Core\Listener\BeforeMessageLoggedEventListener::setup();
}

$eventLogger = Server::get(\OCP\Diagnostics\IEventLogger::class);
$eventLogger->log('autoloader', 'Autoloader', $loaderStart, $loaderEnd);
$eventLogger->start('boot', 'Initialize');
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,7 @@
'OC\\Core\\Events\\PasswordResetEvent' => $baseDir . '/core/Events/PasswordResetEvent.php',
'OC\\Core\\Exception\\LoginFlowV2NotFoundException' => $baseDir . '/core/Exception/LoginFlowV2NotFoundException.php',
'OC\\Core\\Exception\\ResetPasswordException' => $baseDir . '/core/Exception/ResetPasswordException.php',
'OC\\Core\\Listener\\BeforeMessageLoggedEventListener' => $baseDir . '/core/Listener/BeforeMessageLoggedEventListener.php',
'OC\\Core\\Listener\\BeforeTemplateRenderedListener' => $baseDir . '/core/Listener/BeforeTemplateRenderedListener.php',
'OC\\Core\\Middleware\\TwoFactorMiddleware' => $baseDir . '/core/Middleware/TwoFactorMiddleware.php',
'OC\\Core\\Migrations\\Version13000Date20170705121758' => $baseDir . '/core/Migrations/Version13000Date20170705121758.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Core\\Events\\PasswordResetEvent' => __DIR__ . '/../../..' . '/core/Events/PasswordResetEvent.php',
'OC\\Core\\Exception\\LoginFlowV2NotFoundException' => __DIR__ . '/../../..' . '/core/Exception/LoginFlowV2NotFoundException.php',
'OC\\Core\\Exception\\ResetPasswordException' => __DIR__ . '/../../..' . '/core/Exception/ResetPasswordException.php',
'OC\\Core\\Listener\\BeforeMessageLoggedEventListener' => __DIR__ . '/../../..' . '/core/Listener/BeforeMessageLoggedEventListener.php',
'OC\\Core\\Listener\\BeforeTemplateRenderedListener' => __DIR__ . '/../../..' . '/core/Listener/BeforeTemplateRenderedListener.php',
'OC\\Core\\Middleware\\TwoFactorMiddleware' => __DIR__ . '/../../..' . '/core/Middleware/TwoFactorMiddleware.php',
'OC\\Core\\Migrations\\Version13000Date20170705121758' => __DIR__ . '/../../..' . '/core/Migrations/Version13000Date20170705121758.php',
Expand Down
Loading