Skip to content

Commit

Permalink
WIP: Replace more ILogger method calls
Browse files Browse the repository at this point in the history
This commit replaces more ILogger method calls with
 `Psr\Log\LoggerInterface` as we gradually move away from the
 custom ILogger.

Signed-off-by: fenn-cs <fenn25.fn@gmail.com>
  • Loading branch information
Fenn-CS committed Mar 30, 2023
1 parent 8ee52d3 commit a9678df
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 165 deletions.
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
4 changes: 2 additions & 2 deletions lib/private/Template/JSConfigHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
use OCP\IL10N;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\ILogger;
use Psr\Log\LogLevel;
use OCP\IUser;
use OCP\User\Backend\IPasswordConfirmationBackend;
use OCP\Util;
Expand Down Expand Up @@ -169,7 +169,7 @@ public function getConfig(): string {
'auto_logout' => $this->config->getSystemValue('auto_logout', false),
'blacklist_files_regex' => FileInfo::BLACKLIST_FILES_REGEX,
'loglevel' => $this->config->getSystemValue('loglevel_frontend',
$this->config->getSystemValue('loglevel', ILogger::WARN)
$this->config->getSystemValue('loglevel', LogLevel::WARNING)
),
'lost_password_link' => $this->config->getSystemValue('lost_password_link', null),
'modRewriteWorking' => $this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true',
Expand Down
6 changes: 3 additions & 3 deletions lib/private/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
use OCP\EventDispatcher\IEventDispatcher;
use OCP\HintException;
use OCP\IConfig;
use OCP\ILogger;
use OCP\Util;
use OC\App\AppManager;
use OC\DB\Connection;
Expand All @@ -62,6 +61,7 @@
use OC\Repair\Events\RepairWarningEvent;
use OC_App;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

/**
* Class that handles autoupdating of ownCloud
Expand Down Expand Up @@ -112,9 +112,9 @@ public function __construct(IConfig $config,
public function upgrade(): bool {
$this->logAllEvents();

$logLevel = $this->config->getSystemValue('loglevel', ILogger::WARN);
$logLevel = $this->config->getSystemValue('loglevel', LogLevel::WARNING);
$this->emit('\OC\Updater', 'setDebugLogLevel', [ $logLevel, $this->logLevelNames[$logLevel] ]);
$this->config->setSystemValue('loglevel', ILogger::DEBUG);
$this->config->setSystemValue('loglevel', LogLevel::DEBUG);

$wasMaintenanceModeEnabled = $this->config->getSystemValueBool('maintenance');

Expand Down
4 changes: 2 additions & 2 deletions lib/public/BackgroundJob/IJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
namespace OCP\BackgroundJob;

use OCP\ILogger;
use Psr\Log\LoggerInterface;

/**
* This interface represend a backgroud job run with cron
Expand Down Expand Up @@ -55,7 +55,7 @@ interface IJob {
* @deprecated since 25.0.0 Use start() instead. This method will be removed
* with the ILogger interface
*/
public function execute(IJobList $jobList, ILogger $logger = null);
public function execute(IJobList $jobList, LoggerInterface $logger = null);

/**
* Start the background job with the registered argument
Expand Down
5 changes: 2 additions & 3 deletions lib/public/BackgroundJob/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
namespace OCP\BackgroundJob;

use OCP\AppFramework\Utility\ITimeFactory;
use OCP\ILogger;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -56,11 +55,11 @@ public function __construct(ITimeFactory $time) {
*
*
* @param IJobList $jobList
* @param ILogger|null $logger
* @param LoggerInterface|null $logger
*
* @since 15.0.0
*/
public function execute(IJobList $jobList, ILogger $logger = null) {
public function execute(IJobList $jobList, LoggerInterface $logger = null) {
$this->start($jobList);
}

Expand Down
10 changes: 0 additions & 10 deletions lib/public/Log/ILogFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
*/
namespace OCP\Log;

use OCP\ILogger;
use Psr\Log\LoggerInterface;

/**
Expand All @@ -40,15 +39,6 @@ interface ILogFactory {
*/
public function get(string $type): IWriter;

/**
* @param string $path
* @return ILogger
* @since 14.0.0
* @deprecated use \OCP\Log\ILogFactory::getCustomPsrLogger
* @see \OCP\Log\ILogFactory::getCustomPsrLogger
*/
public function getCustomLogger(string $path): ILogger;

/**
* @param string $path
* @param string $type
Expand Down
Loading

0 comments on commit a9678df

Please sign in to comment.