Skip to content

Commit

Permalink
fix(core): minor syntax update
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Mar 12, 2024
1 parent c423973 commit 1341233
Showing 1 changed file with 20 additions and 45 deletions.
65 changes: 20 additions & 45 deletions lib/private/Log.php
Expand Up @@ -62,38 +62,26 @@
* 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 = null,
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 = \OCP\Server::get(SystemConfig::class);
}

$this->config = $config;
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 +90,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 +103,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 +115,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 +126,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 +139,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 +149,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 +161,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 +171,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 +183,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 +226,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 +276,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 +296,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 +370,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

0 comments on commit 1341233

Please sign in to comment.