Merge pull request #25588 from nextcloud/techdept/custom-psr-logger
Migrate custom loggers to PSR
This commit is contained in:
commit
11e2286a82
|
@ -31,11 +31,12 @@ use OCP\IConfig;
|
||||||
use OCP\ILogger;
|
use OCP\ILogger;
|
||||||
use OCP\Log\IDataLogger;
|
use OCP\Log\IDataLogger;
|
||||||
use OCP\Log\ILogFactory;
|
use OCP\Log\ILogFactory;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
class Logger {
|
class Logger {
|
||||||
/** @var ILogger */
|
/** @var ILogger */
|
||||||
protected $generalLogger;
|
protected $generalLogger;
|
||||||
/** @var ILogger */
|
/** @var LoggerInterface */
|
||||||
protected $flowLogger;
|
protected $flowLogger;
|
||||||
/** @var IConfig */
|
/** @var IConfig */
|
||||||
private $config;
|
private $config;
|
||||||
|
@ -54,7 +55,7 @@ class Logger {
|
||||||
$default = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/flow.log';
|
$default = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/flow.log';
|
||||||
$logFile = trim((string)$this->config->getAppValue(Application::APP_ID, 'logfile', $default));
|
$logFile = trim((string)$this->config->getAppValue(Application::APP_ID, 'logfile', $default));
|
||||||
if ($logFile !== '') {
|
if ($logFile !== '') {
|
||||||
$this->flowLogger = $this->logFactory->getCustomLogger($logFile);
|
$this->flowLogger = $this->logFactory->getCustomPsrLogger($logFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -31,6 +31,7 @@ use OCP\ILogger;
|
||||||
use OCP\IServerContainer;
|
use OCP\IServerContainer;
|
||||||
use OCP\Log\ILogFactory;
|
use OCP\Log\ILogFactory;
|
||||||
use OCP\Log\IWriter;
|
use OCP\Log\IWriter;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
class LogFactory implements ILogFactory {
|
class LogFactory implements ILogFactory {
|
||||||
/** @var IServerContainer */
|
/** @var IServerContainer */
|
||||||
|
@ -70,6 +71,13 @@ class LogFactory implements ILogFactory {
|
||||||
return new Log($log, $this->systemConfig);
|
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 {
|
protected function buildLogFile(string $logFile = ''):File {
|
||||||
$defaultLogFile = $this->systemConfig->getValue('datadirectory', \OC::$SERVERROOT.'/data').'/nextcloud.log';
|
$defaultLogFile = $this->systemConfig->getValue('datadirectory', \OC::$SERVERROOT.'/data').'/nextcloud.log';
|
||||||
if ($logFile === '') {
|
if ($logFile === '') {
|
||||||
|
|
|
@ -26,19 +26,21 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace OC\Log;
|
namespace OC\Log;
|
||||||
|
|
||||||
|
use OC\Log;
|
||||||
use OCP\ILogger;
|
use OCP\ILogger;
|
||||||
|
use OCP\Log\IDataLogger;
|
||||||
use Psr\Log\InvalidArgumentException;
|
use Psr\Log\InvalidArgumentException;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
use function array_key_exists;
|
use function array_key_exists;
|
||||||
use function array_merge;
|
use function array_merge;
|
||||||
|
|
||||||
final class PsrLoggerAdapter implements LoggerInterface {
|
final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
|
||||||
|
|
||||||
/** @var ILogger */
|
/** @var Log */
|
||||||
private $logger;
|
private $logger;
|
||||||
|
|
||||||
public function __construct(ILogger $logger) {
|
public function __construct(Log $logger) {
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -260,4 +262,8 @@ final class PsrLoggerAdapter implements LoggerInterface {
|
||||||
$this->logger->log($level, $message, $context);
|
$this->logger->log($level, $message, $context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function logData(string $message, array $data, array $context = []): void {
|
||||||
|
$this->logger->logData($message, $data, $context);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
namespace OCP\Log;
|
namespace OCP\Log;
|
||||||
|
|
||||||
use OCP\ILogger;
|
use OCP\ILogger;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface ILogFactory
|
* Interface ILogFactory
|
||||||
|
@ -43,6 +44,15 @@ interface ILogFactory {
|
||||||
* @param string $path
|
* @param string $path
|
||||||
* @return ILogger
|
* @return ILogger
|
||||||
* @since 14.0.0
|
* @since 14.0.0
|
||||||
|
* @deprecated use \OCP\Log\ILogFactory::getCustomPsrLogger
|
||||||
|
* @see \OCP\Log\ILogFactory::getCustomPsrLogger
|
||||||
*/
|
*/
|
||||||
public function getCustomLogger(string $path): ILogger;
|
public function getCustomLogger(string $path): ILogger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
* @return LoggerInterface
|
||||||
|
* @since 22.0.0
|
||||||
|
*/
|
||||||
|
public function getCustomPsrLogger(string $path): LoggerInterface;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue