Merge pull request #8553 from nextcloud/strict_logger

Make ILogger strict
This commit is contained in:
Morris Jobke 2018-02-27 11:20:06 +01:00 committed by GitHub
commit 7081bdbea6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 30 deletions

View File

@ -41,7 +41,7 @@ class TestLogger extends Log {
//disable original constructor //disable original constructor
} }
public function log($level, $message, array $context = array()) { public function log(int $level, string $message, array $context = array()) {
$this->level = $level; $this->level = $level;
$this->message = $message; $this->message = $message;
} }

View File

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1);
/** /**
* @copyright Copyright (c) 2016, ownCloud, Inc. * @copyright Copyright (c) 2016, ownCloud, Inc.
* *
@ -129,7 +130,7 @@ class Log implements ILogger {
if($logger === null) { if($logger === null) {
$logType = $this->config->getValue('log_type', 'file'); $logType = $this->config->getValue('log_type', 'file');
$this->logger = static::getLogClass($logType); $this->logger = static::getLogClass($logType);
call_user_func(array($this->logger, 'init')); call_user_func([$this->logger, 'init']);
} else { } else {
$this->logger = $logger; $this->logger = $logger;
} }
@ -148,7 +149,7 @@ class Log implements ILogger {
* @param array $context * @param array $context
* @return void * @return void
*/ */
public function emergency($message, array $context = array()) { public function emergency(string $message, array $context = []) {
$this->log(Util::FATAL, $message, $context); $this->log(Util::FATAL, $message, $context);
} }
@ -162,7 +163,7 @@ class Log implements ILogger {
* @param array $context * @param array $context
* @return void * @return void
*/ */
public function alert($message, array $context = array()) { public function alert(string $message, array $context = []) {
$this->log(Util::ERROR, $message, $context); $this->log(Util::ERROR, $message, $context);
} }
@ -175,7 +176,7 @@ class Log implements ILogger {
* @param array $context * @param array $context
* @return void * @return void
*/ */
public function critical($message, array $context = array()) { public function critical(string $message, array $context = []) {
$this->log(Util::ERROR, $message, $context); $this->log(Util::ERROR, $message, $context);
} }
@ -187,7 +188,7 @@ class Log implements ILogger {
* @param array $context * @param array $context
* @return void * @return void
*/ */
public function error($message, array $context = array()) { public function error(string $message, array $context = []) {
$this->log(Util::ERROR, $message, $context); $this->log(Util::ERROR, $message, $context);
} }
@ -201,7 +202,7 @@ class Log implements ILogger {
* @param array $context * @param array $context
* @return void * @return void
*/ */
public function warning($message, array $context = array()) { public function warning(string $message, array $context = []) {
$this->log(Util::WARN, $message, $context); $this->log(Util::WARN, $message, $context);
} }
@ -212,7 +213,7 @@ class Log implements ILogger {
* @param array $context * @param array $context
* @return void * @return void
*/ */
public function notice($message, array $context = array()) { public function notice(string $message, array $context = []) {
$this->log(Util::INFO, $message, $context); $this->log(Util::INFO, $message, $context);
} }
@ -225,7 +226,7 @@ class Log implements ILogger {
* @param array $context * @param array $context
* @return void * @return void
*/ */
public function info($message, array $context = array()) { public function info(string $message, array $context = []) {
$this->log(Util::INFO, $message, $context); $this->log(Util::INFO, $message, $context);
} }
@ -236,7 +237,7 @@ class Log implements ILogger {
* @param array $context * @param array $context
* @return void * @return void
*/ */
public function debug($message, array $context = array()) { public function debug(string $message, array $context = []) {
$this->log(Util::DEBUG, $message, $context); $this->log(Util::DEBUG, $message, $context);
} }
@ -244,12 +245,12 @@ class Log implements ILogger {
/** /**
* Logs with an arbitrary level. * Logs with an arbitrary level.
* *
* @param mixed $level * @param int $level
* @param string $message * @param string $message
* @param array $context * @param array $context
* @return void * @return void
*/ */
public function log($level, $message, array $context = array()) { public function log(int $level, string $message, array $context = []) {
$minLevel = min($this->config->getValue('loglevel', Util::WARN), Util::FATAL); $minLevel = min($this->config->getValue('loglevel', Util::WARN), Util::FATAL);
$logCondition = $this->config->getValue('log.condition', []); $logCondition = $this->config->getValue('log.condition', []);
@ -272,7 +273,7 @@ class Log implements ILogger {
$app = 'no app in context'; $app = 'no app in context';
} }
// interpolate $message as defined in PSR-3 // interpolate $message as defined in PSR-3
$replace = array(); $replace = [];
foreach ($context as $key => $val) { foreach ($context as $key => $val) {
$replace['{' . $key . '}'] = $val; $replace['{' . $key . '}'] = $val;
} }
@ -318,7 +319,7 @@ class Log implements ILogger {
if ($level >= $minLevel) { if ($level >= $minLevel) {
$logger = $this->logger; $logger = $this->logger;
call_user_func(array($logger, 'write'), $app, $message, $level); call_user_func([$logger, 'write'], $app, $message, $level);
} }
} }
@ -330,20 +331,20 @@ class Log implements ILogger {
* @return void * @return void
* @since 8.2.0 * @since 8.2.0
*/ */
public function logException($exception, array $context = array()) { public function logException(\Throwable $exception, array $context = []) {
$level = Util::ERROR; $level = Util::ERROR;
if (isset($context['level'])) { if (isset($context['level'])) {
$level = $context['level']; $level = $context['level'];
unset($context['level']); unset($context['level']);
} }
$data = array( $data = [
'Exception' => get_class($exception), 'Exception' => get_class($exception),
'Message' => $exception->getMessage(), 'Message' => $exception->getMessage(),
'Code' => $exception->getCode(), 'Code' => $exception->getCode(),
'Trace' => $exception->getTraceAsString(), 'Trace' => $exception->getTraceAsString(),
'File' => $exception->getFile(), 'File' => $exception->getFile(),
'Line' => $exception->getLine(), 'Line' => $exception->getLine(),
); ];
$data['Trace'] = preg_replace('!(' . implode('|', $this->methodsWithSensitiveParameters) . ')\(.*\)!', '$1(*** sensitive parameters replaced ***)', $data['Trace']); $data['Trace'] = preg_replace('!(' . implode('|', $this->methodsWithSensitiveParameters) . ')\(.*\)!', '$1(*** sensitive parameters replaced ***)', $data['Trace']);
if ($exception instanceof HintException) { if ($exception instanceof HintException) {
$data['Hint'] = $exception->getHint(); $data['Hint'] = $exception->getHint();
@ -362,7 +363,7 @@ class Log implements ILogger {
* @return string * @return string
* @internal * @internal
*/ */
public static function getLogClass($logType) { public static function getLogClass(string $logType): string {
switch (strtolower($logType)) { switch (strtolower($logType)) {
case 'errorlog': case 'errorlog':
return \OC\Log\Errorlog::class; return \OC\Log\Errorlog::class;

View File

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1);
/** /**
* @copyright Copyright (c) 2016, ownCloud, Inc. * @copyright Copyright (c) 2016, ownCloud, Inc.
* *
@ -43,7 +44,7 @@ interface ILogger {
* @return null * @return null
* @since 7.0.0 * @since 7.0.0
*/ */
public function emergency($message, array $context = array()); public function emergency(string $message, array $context = []);
/** /**
* Action must be taken immediately. * Action must be taken immediately.
@ -53,7 +54,7 @@ interface ILogger {
* @return null * @return null
* @since 7.0.0 * @since 7.0.0
*/ */
public function alert($message, array $context = array()); public function alert(string $message, array $context = []);
/** /**
* Critical conditions. * Critical conditions.
@ -63,7 +64,7 @@ interface ILogger {
* @return null * @return null
* @since 7.0.0 * @since 7.0.0
*/ */
public function critical($message, array $context = array()); public function critical(string $message, array $context = []);
/** /**
* Runtime errors that do not require immediate action but should typically * Runtime errors that do not require immediate action but should typically
@ -74,7 +75,7 @@ interface ILogger {
* @return null * @return null
* @since 7.0.0 * @since 7.0.0
*/ */
public function error($message, array $context = array()); public function error(string $message, array $context = []);
/** /**
* Exceptional occurrences that are not errors. * Exceptional occurrences that are not errors.
@ -84,7 +85,7 @@ interface ILogger {
* @return null * @return null
* @since 7.0.0 * @since 7.0.0
*/ */
public function warning($message, array $context = array()); public function warning(string $message, array $context = []);
/** /**
* Normal but significant events. * Normal but significant events.
@ -94,7 +95,7 @@ interface ILogger {
* @return null * @return null
* @since 7.0.0 * @since 7.0.0
*/ */
public function notice($message, array $context = array()); public function notice(string $message, array $context = []);
/** /**
* Interesting events. * Interesting events.
@ -104,7 +105,7 @@ interface ILogger {
* @return null * @return null
* @since 7.0.0 * @since 7.0.0
*/ */
public function info($message, array $context = array()); public function info(string $message, array $context = []);
/** /**
* Detailed debug information. * Detailed debug information.
@ -114,18 +115,18 @@ interface ILogger {
* @return null * @return null
* @since 7.0.0 * @since 7.0.0
*/ */
public function debug($message, array $context = array()); public function debug(string $message, array $context = []);
/** /**
* Logs with an arbitrary level. * Logs with an arbitrary level.
* *
* @param mixed $level * @param int $level
* @param string $message * @param string $message
* @param array $context * @param array $context
* @return mixed * @return mixed
* @since 7.0.0 * @since 7.0.0
*/ */
public function log($level, $message, array $context = array()); public function log(int $level, string $message, array $context = []);
/** /**
* Logs an exception very detailed * Logs an exception very detailed
@ -143,5 +144,5 @@ interface ILogger {
* @return void * @return void
* @since 8.2.0 * @since 8.2.0
*/ */
public function logException($exception, array $context = array()); public function logException(\Throwable $exception, array $context = []);
} }

View File

@ -17,7 +17,7 @@ class NullLogger extends Log {
//disable original constructor //disable original constructor
} }
public function log($level, $message, array $context = array()) { public function log(int $level, string $message, array $context = array()) {
//noop //noop
} }
} }