-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2ba0819
feat(occ): Add a --debug option to output all log levels to the output
come-nc 9baf8fe
fix(occ): Move debug log listener setup to a static method, add optio…
come-nc 612088b
feat: Add reserved options in a new OCP class so that applications kn…
come-nc 5b9966f
fix(occ): Use the const instead of hardcoded string for --debug-log o…
come-nc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') { | ||
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(...)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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"?