Skip to content

Commit

Permalink
WIP: Move away from deprecated ILogger
Browse files Browse the repository at this point in the history
The ILogger is marked as deprecated with a decision
 to now use the more stadard `Psr\Log\LoggerInterface`.

This commit move all logging dependent on our custom
 ILogger to `Psr\Log\LoggerInterface`

See : https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#3-psrlogloggerinterface
Signed-off-by: fenn-cs <fenn25.fn@gmail.com>
  • Loading branch information
nfebe committed Mar 28, 2023
1 parent 8ee52d3 commit 26be809
Show file tree
Hide file tree
Showing 22 changed files with 95 additions and 215 deletions.
12 changes: 6 additions & 6 deletions apps/dav/lib/BackgroundJob/RefreshWebcalJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\Job;
use OCP\IConfig;
use OCP\ILogger;
use Psr\Log\LoggerInterface;
use Sabre\VObject\DateTimeParser;
use Sabre\VObject\InvalidDataException;

Expand All @@ -50,7 +50,7 @@ class RefreshWebcalJob extends Job {
*/
private $config;

/** @var ILogger */
/** @var LoggerInterface */
private $logger;

/** @var ITimeFactory */
Expand All @@ -61,10 +61,10 @@ class RefreshWebcalJob extends Job {
*
* @param RefreshWebcalService $refreshWebcalService
* @param IConfig $config
* @param ILogger $logger
* @param LoggerInterface $logger
* @param ITimeFactory $timeFactory
*/
public function __construct(RefreshWebcalService $refreshWebcalService, IConfig $config, ILogger $logger, ITimeFactory $timeFactory) {
public function __construct(RefreshWebcalService $refreshWebcalService, IConfig $config, LoggerInterface $logger, ITimeFactory $timeFactory) {
parent::__construct($timeFactory);
$this->refreshWebcalService = $refreshWebcalService;
$this->config = $config;
Expand All @@ -77,7 +77,7 @@ public function __construct(RefreshWebcalService $refreshWebcalService, IConfig
*
* @inheritdoc
*/
public function execute(IJobList $jobList, ILogger $logger = null) {
public function execute(IJobList $jobList, LoggerInterface $logger = null) {
$subscription = $this->refreshWebcalService->getSubscription($this->argument['principaluri'], $this->argument['uri']);
if (!$subscription) {
return;
Expand All @@ -95,7 +95,7 @@ public function execute(IJobList $jobList, ILogger $logger = null) {
/** @var DateInterval $dateInterval */
$dateInterval = DateTimeParser::parseDuration($refreshRate);
} catch (InvalidDataException $ex) {
$this->logger->logException($ex);
$this->logger->log('error', $ex);
$this->logger->warning("Subscription $subscriptionId could not be refreshed, refreshrate in database is invalid");
return;
}
Expand Down
12 changes: 6 additions & 6 deletions config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -923,21 +923,21 @@
'logfilemode' => 0640,

/**
* Loglevel to start logging at. Valid values are: 0 = Debug, 1 = Info, 2 =
* Warning, 3 = Error, and 4 = Fatal. The default value is Warning.
* Loglevel to start logging at. Valid values are: 'debug', 'info',
* 'warning' and 'error'. The default value is Warning.
*
* Defaults to ``2``
* Defaults to ``warning``
*/
'loglevel' => 2,
'loglevel' => 'warning',

/**
* Loglevel used by the frontend to start logging at. The same values as
* for ``loglevel`` can be used. If not set it defaults to the value
* configured for ``loglevel`` or Warning if that is not set either.
*
* Defaults to ``2``
* Defaults to ``warning``
*/
'loglevel_frontend' => 2,
'loglevel_frontend' => 'warning',

/**
* If you maintain different instances and aggregate the logs, you may want
Expand Down
61 changes: 4 additions & 57 deletions core/Command/Log/Manage.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

class Manage extends Command implements CompletionAwareInterface {
public const DEFAULT_BACKEND = 'file';
public const DEFAULT_LOG_LEVEL = 2;
public const DEFAULT_LOG_LEVEL = 'warning';
public const DEFAULT_TIMEZONE = 'UTC';

protected IConfig $config;
Expand Down Expand Up @@ -82,14 +82,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$level = $input->getOption('level');
if ($level !== null) {
if (is_numeric($level)) {
$levelNum = $level;
// sanity check
$this->convertLevelNumber($levelNum);
} else {
$levelNum = $this->convertLevelString($level);
}
$toBeSet['loglevel'] = $levelNum;
$toBeSet['loglevel'] = $level;
}

if ($timezone = $input->getOption('timezone')) {
Expand All @@ -106,9 +99,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$backend = $this->config->getSystemValue('log_type', self::DEFAULT_BACKEND);
$output->writeln('Enabled logging backend: '.$backend);

$levelNum = $this->config->getSystemValue('loglevel', self::DEFAULT_LOG_LEVEL);
$level = $this->convertLevelNumber($levelNum);
$output->writeln('Log level: '.$level.' ('.$levelNum.')');
$level = $this->config->getSystemValue('loglevel', self::DEFAULT_LOG_LEVEL);
$output->writeln('Log level: '.$level.' ('.$level.')');

$timezone = $this->config->getSystemValue('logtimezone', self::DEFAULT_TIMEZONE);
$output->writeln('Log timezone: '.$timezone);
Expand All @@ -133,51 +125,6 @@ protected function validateTimezone($timezone) {
new \DateTimeZone($timezone);
}

/**
* @param string $level
* @return int
* @throws \InvalidArgumentException
*/
protected function convertLevelString($level) {
$level = strtolower($level);
switch ($level) {
case 'debug':
return 0;
case 'info':
return 1;
case 'warning':
case 'warn':
return 2;
case 'error':
case 'err':
return 3;
case 'fatal':
return 4;
}
throw new \InvalidArgumentException('Invalid log level string');
}

/**
* @param int $levelNum
* @return string
* @throws \InvalidArgumentException
*/
protected function convertLevelNumber($levelNum) {
switch ($levelNum) {
case 0:
return 'Debug';
case 1:
return 'Info';
case 2:
return 'Warning';
case 3:
return 'Error';
case 4:
return 'Fatal';
}
throw new \InvalidArgumentException('Invalid log level number');
}

/**
* @param string $optionName
* @param CompletionContext $context
Expand Down
4 changes: 2 additions & 2 deletions lib/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@
use OC\Share20\Hooks;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Group\Events\UserRemovedEvent;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUserSession;
use OCP\Server;
use OCP\Share;
use OCP\User\Events\UserChangedEvent;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use function OCP\Log\logger;

Expand Down Expand Up @@ -628,7 +628,7 @@ public static function init(): void {
$eventLogger->start('boot', 'Initialize');

// Override php.ini and log everything if we're troubleshooting
if (self::$config->getValue('loglevel') === ILogger::DEBUG) {
if (self::$config->getValue('loglevel') === LogLevel::DEBUG) {
error_reporting(E_ALL);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/private/BackgroundJob/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

use OCP\BackgroundJob\IJob;
use OCP\BackgroundJob\IJobList;
use OCP\ILogger;
use Psr\Log\LoggerInterface;

/**
* @deprecated internal class, use \OCP\BackgroundJob\Job
Expand All @@ -42,7 +42,7 @@ abstract class Job implements IJob {
/** @var mixed */
protected $argument;

public function execute(IJobList $jobList, ILogger $logger = null) {
public function execute(IJobList $jobList, LoggerInterface $logger = null) {
$jobList->setLastRun($this);
if ($logger === null) {
$logger = \OC::$server->getLogger();
Expand Down
47 changes: 25 additions & 22 deletions lib/private/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
use OCP\Log\IFileBased;
use OCP\Log\IWriter;
use OCP\Support\CrashReport\IRegistry;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

use function strtr;

/**
Expand All @@ -58,7 +61,7 @@
*
* MonoLog is an example implementing this interface.
*/
class Log implements ILogger, IDataLogger {
class Log implements LoggerInterface, IDataLogger {
private IWriter $logger;
private ?SystemConfig $config;
private ?bool $logConditionSatisfied = null;
Expand Down Expand Up @@ -94,8 +97,8 @@ public function __construct(IWriter $logger, SystemConfig $config = null, Normal
* @param array $context
* @return void
*/
public function emergency(string $message, array $context = []) {
$this->log(ILogger::FATAL, $message, $context);
public function emergency($message, array $context = []) {
$this->log(LogLevel::ERROR, $message, $context);
}

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

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

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

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

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

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

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


Expand All @@ -195,7 +198,7 @@ public function debug(string $message, array $context = []) {
* @param array $context
* @return void
*/
public function log(int $level, string $message, array $context = []) {
public function log($level, $message, array $context = []) {
$minLevel = $this->getLogLevel($context);

array_walk($context, [$this->normalizer, 'format']);
Expand Down Expand Up @@ -269,7 +272,7 @@ public function getLogLevel($context) {

// if log condition is satisfied change the required log level to DEBUG
if ($this->logConditionSatisfied) {
return ILogger::DEBUG;
return LogLevel::DEBUG;
}

if (isset($context['app'])) {
Expand All @@ -282,11 +285,11 @@ public function getLogLevel($context) {
if (!empty($logCondition)
&& isset($logCondition['apps'])
&& in_array($app, $logCondition['apps'], true)) {
return ILogger::DEBUG;
return LogLevel::DEBUG;
}
}

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

/**
Expand All @@ -299,7 +302,7 @@ public function getLogLevel($context) {
*/
public function logException(Throwable $exception, array $context = []) {
$app = $context['app'] ?? 'no app in context';
$level = $context['level'] ?? ILogger::ERROR;
$level = $context['level'] ?? LogLevel::ERROR;

$minLevel = $this->getLogLevel($context);
if ($level < $minLevel && ($this->crashReporters === null || !$this->crashReporters->hasReporters())) {
Expand Down
Loading

0 comments on commit 26be809

Please sign in to comment.