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

Migrate custom loggers to PSR #25588

Merged
merged 2 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions apps/workflowengine/lib/Service/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
use OCP\ILogger;
use OCP\Log\IDataLogger;
use OCP\Log\ILogFactory;
use Psr\Log\LoggerInterface;

class Logger {
/** @var ILogger */
protected $generalLogger;
/** @var ILogger */
/** @var LoggerInterface */
protected $flowLogger;
/** @var IConfig */
private $config;
Expand All @@ -54,7 +55,7 @@ protected function initLogger() {
$default = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/flow.log';
$logFile = trim((string)$this->config->getAppValue(Application::APP_ID, 'logfile', $default));
if ($logFile !== '') {
$this->flowLogger = $this->logFactory->getCustomLogger($logFile);
$this->flowLogger = $this->logFactory->getCustomPsrLogger($logFile);
}
}

Expand Down
8 changes: 8 additions & 0 deletions lib/private/Log/LogFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use OCP\IServerContainer;
use OCP\Log\ILogFactory;
use OCP\Log\IWriter;
use Psr\Log\LoggerInterface;

class LogFactory implements ILogFactory {
/** @var IServerContainer */
Expand Down Expand Up @@ -70,6 +71,13 @@ public function getCustomLogger(string $path):ILogger {
return new Log($log, $this->systemConfig);
}

public function getCustomPsrLogger(string $path): LoggerInterface {
$log = $this->buildLogFile($path);
return new PsrLoggerAdapter(
new Log($log, $this->systemConfig)
);
}

protected function buildLogFile(string $logFile = ''):File {
$defaultLogFile = $this->systemConfig->getValue('datadirectory', \OC::$SERVERROOT.'/data').'/nextcloud.log';
if ($logFile === '') {
Expand Down
12 changes: 9 additions & 3 deletions lib/private/Log/PsrLoggerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,21 @@

namespace OC\Log;

use OC\Log;
use OCP\ILogger;
use OCP\Log\IDataLogger;
use Psr\Log\InvalidArgumentException;
use Psr\Log\LoggerInterface;
use Throwable;
use function array_key_exists;
use function array_merge;

final class PsrLoggerAdapter implements LoggerInterface {
final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {

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

public function __construct(ILogger $logger) {
public function __construct(Log $logger) {
$this->logger = $logger;
}

Expand Down Expand Up @@ -260,4 +262,8 @@ public function log($level, $message, array $context = []) {
$this->logger->log($level, $message, $context);
}
}

public function logData(string $message, array $data, array $context = []): void {
$this->logger->logData($message, $data, $context);
}
}
10 changes: 10 additions & 0 deletions lib/public/Log/ILogFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
namespace OCP\Log;

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

/**
* Interface ILogFactory
Expand All @@ -43,6 +44,15 @@ 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
* @return LoggerInterface
* @since 22.0.0
*/
public function getCustomPsrLogger(string $path): LoggerInterface;
}