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

minor syntax update #43534

Merged
merged 2 commits into from
Mar 16, 2024
Merged
Changes from all 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
69 changes: 20 additions & 49 deletions lib/private/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,38 +62,22 @@
* MonoLog is an example implementing this interface.
*/
class Log implements ILogger, IDataLogger {
private ?SystemConfig $config;
private ?bool $logConditionSatisfied = null;
private ?Normalizer $normalizer;
private ?IEventDispatcher $eventDispatcher;
private ?IEventDispatcher $eventDispatcher = null;

/**
* @param IWriter $logger The logger that should be used
* @param SystemConfig|null $config the system config object
* @param Normalizer|null $normalizer
* @param IRegistry|null $crashReporters
*/
public function __construct(
private IWriter $logger,
SystemConfig $config = null,
Normalizer $normalizer = null,
private ?IRegistry $crashReporters = null
private SystemConfig $config,
private ?Normalizer $normalizer = null,
private ?IRegistry $crashReporters = null
) {
// FIXME: Add this for backwards compatibility, should be fixed at some point probably
if ($config === null) {
$config = \OC::$server->getSystemConfig();
}

$this->config = $config;
// FIXME: php8.1 allows "private Normalizer $normalizer = new Normalizer()," in initializer
if ($normalizer === null) {
$this->normalizer = new Normalizer();
} else {
$this->normalizer = $normalizer;
}
$this->eventDispatcher = null;
}

public function setEventDispatcher(IEventDispatcher $eventDispatcher) {
public function setEventDispatcher(IEventDispatcher $eventDispatcher): void {
$this->eventDispatcher = $eventDispatcher;
}

Expand All @@ -102,9 +86,8 @@ public function setEventDispatcher(IEventDispatcher $eventDispatcher) {
*
* @param string $message
* @param array $context
* @return void
*/
public function emergency(string $message, array $context = []) {
public function emergency(string $message, array $context = []): void {
$this->log(ILogger::FATAL, $message, $context);
}

Expand All @@ -116,9 +99,8 @@ public function emergency(string $message, array $context = []) {
*
* @param string $message
* @param array $context
* @return void
*/
public function alert(string $message, array $context = []) {
public function alert(string $message, array $context = []): void {
$this->log(ILogger::ERROR, $message, $context);
}

Expand All @@ -129,9 +111,8 @@ public function alert(string $message, array $context = []) {
*
* @param string $message
* @param array $context
* @return void
*/
public function critical(string $message, array $context = []) {
public function critical(string $message, array $context = []): void {
$this->log(ILogger::ERROR, $message, $context);
}

Expand All @@ -141,9 +122,8 @@ public function critical(string $message, array $context = []) {
*
* @param string $message
* @param array $context
* @return void
*/
public function error(string $message, array $context = []) {
public function error(string $message, array $context = []): void {
$this->log(ILogger::ERROR, $message, $context);
}

Expand All @@ -155,9 +135,8 @@ public function error(string $message, array $context = []) {
*
* @param string $message
* @param array $context
* @return void
*/
public function warning(string $message, array $context = []) {
public function warning(string $message, array $context = []): void {
$this->log(ILogger::WARN, $message, $context);
}

Expand All @@ -166,9 +145,8 @@ public function warning(string $message, array $context = []) {
*
* @param string $message
* @param array $context
* @return void
*/
public function notice(string $message, array $context = []) {
public function notice(string $message, array $context = []): void {
$this->log(ILogger::INFO, $message, $context);
}

Expand All @@ -179,9 +157,8 @@ public function notice(string $message, array $context = []) {
*
* @param string $message
* @param array $context
* @return void
*/
public function info(string $message, array $context = []) {
public function info(string $message, array $context = []): void {
$this->log(ILogger::INFO, $message, $context);
}

Expand All @@ -190,9 +167,8 @@ public function info(string $message, array $context = []) {
*
* @param string $message
* @param array $context
* @return void
*/
public function debug(string $message, array $context = []) {
public function debug(string $message, array $context = []): void {
$this->log(ILogger::DEBUG, $message, $context);
}

Expand All @@ -203,9 +179,8 @@ public function debug(string $message, array $context = []) {
* @param int $level
* @param string $message
* @param array $context
* @return void
*/
public function log(int $level, string $message, array $context = []) {
public function log(int $level, string $message, array $context = []): void {
$minLevel = $this->getLogLevel($context);
if ($level < $minLevel
&& (($this->crashReporters?->hasReporters() ?? false) === false)
Expand Down Expand Up @@ -247,7 +222,7 @@ public function log(int $level, string $message, array $context = []) {
}
}

public function getLogLevel($context) {
public function getLogLevel($context): int {
$logCondition = $this->config->getValue('log.condition', []);

/**
Expand Down Expand Up @@ -297,20 +272,16 @@ public function getLogLevel($context) {
}

if (isset($context['app'])) {
$app = $context['app'];

/**
* check log condition based on the context of each log message
* once this is met -> change the required log level to debug
*/
if (!empty($logCondition)
&& isset($logCondition['apps'])
&& in_array($app, $logCondition['apps'], true)) {
if (in_array($context['app'], $logCondition['apps'] ?? [], true)) {
return ILogger::DEBUG;
}
}

return min($this->config->getValue('loglevel', ILogger::WARN), ILogger::FATAL);
return min($this->config->getValue('loglevel', ILogger::WARN) ?? ILogger::WARN, ILogger::FATAL);
}

/**
Expand All @@ -321,7 +292,7 @@ public function getLogLevel($context) {
* @return void
* @since 8.2.0
*/
public function logException(Throwable $exception, array $context = []) {
public function logException(Throwable $exception, array $context = []): void {
$app = $context['app'] ?? 'no app in context';
$level = $context['level'] ?? ILogger::ERROR;

Expand Down Expand Up @@ -395,7 +366,7 @@ public function logData(string $message, array $data, array $context = []): void
* @param string|array $entry
* @param int $level
*/
protected function writeLog(string $app, $entry, int $level) {
protected function writeLog(string $app, $entry, int $level): void {
$this->logger->write($app, $entry, $level);
}

Expand Down
Loading