This commit is contained in:
2023-04-18 23:47:31 +08:00
parent 88c2c430c6
commit af284dbe4b
6 changed files with 62 additions and 37 deletions
+35 -32
View File
@@ -17,7 +17,7 @@ use ReflectionException;
*/
class Logger implements LoggerInterface
{
const EMERGENCY = 'emergency';
const ALERT = 'alert';
const CRITICAL = 'critical';
@@ -26,11 +26,11 @@ class Logger implements LoggerInterface
const NOTICE = 'notice';
const INFO = 'info';
const DEBUG = 'debug';
const LOGGER_LEVELS = [Logger::EMERGENCY, Logger::ALERT, Logger::CRITICAL, Logger::ERROR, Logger::WARNING, Logger::NOTICE, Logger::INFO, Logger::DEBUG];
/**
* @param string $message
* @param array $context
@@ -42,8 +42,8 @@ class Logger implements LoggerInterface
// TODO: Implement emergency() method.
$this->log(Logger::EMERGENCY, $message, $context);
}
/**
* @param string $message
* @param array $context
@@ -55,8 +55,8 @@ class Logger implements LoggerInterface
// TODO: Implement alert() method.
$this->log(Logger::ALERT, $message, $context);
}
/**
* @param string $message
* @param array $context
@@ -68,8 +68,8 @@ class Logger implements LoggerInterface
// TODO: Implement critical() method.
$this->log(Logger::CRITICAL, $message, $context);
}
/**
* @param string $message
* @param array $context
@@ -79,8 +79,8 @@ class Logger implements LoggerInterface
// TODO: Implement error() method.
$this->log(Logger::ERROR, $message, $context);
}
/**
* @param string $message
* @param array $context
@@ -90,7 +90,7 @@ class Logger implements LoggerInterface
// TODO: Implement warning() method.
$this->log(Logger::WARNING, $message, $context);
}
/**
* @param string $message
* @param array $context
@@ -100,8 +100,8 @@ class Logger implements LoggerInterface
// TODO: Implement notice() method.
$this->log(Logger::NOTICE, $message, $context);
}
/**
* @param string $message
* @param array $context
@@ -111,8 +111,8 @@ class Logger implements LoggerInterface
// TODO: Implement info() method.
$this->log(Logger::INFO, $message, $context);
}
/**
* @param string $message
* @param array $context
@@ -122,8 +122,8 @@ class Logger implements LoggerInterface
// TODO: Implement debug() method.
$this->log(Logger::DEBUG, $message, $context);
}
/**
* @param mixed $level
* @param string $message
@@ -135,7 +135,7 @@ class Logger implements LoggerInterface
// TODO: Implement log() method.
$levels = Config::get('log.level', Logger::LOGGER_LEVELS);
if (in_array($level, $levels)) {
$_string = '[' . now() . ']' . ucfirst($level) . ': ' . $message . PHP_EOL;
$_string = "[" . now() . ']' . ucfirst($level) . ': ' . $message . PHP_EOL;
if (!empty($context)) {
$_string .= $this->_string($context);
}
@@ -143,14 +143,14 @@ class Logger implements LoggerInterface
return;
}
file_put_contents('php://output', $_string);
$filename = storage('log-' . date('Y-m-d') . '.log', 'log/');
file_put_contents($filename, $_string, FILE_APPEND);
}
}
/**
* @return void
* @throws Exception
@@ -159,8 +159,8 @@ class Logger implements LoggerInterface
{
$this->removeFile(storage());
}
/**
* @param string $dirname
* @return void
@@ -180,8 +180,8 @@ class Logger implements LoggerInterface
@unlink($path->getRealPath());
}
}
/**
* @param $context
* @return string
@@ -189,11 +189,14 @@ class Logger implements LoggerInterface
private function _string($context): string
{
if ($context instanceof \Throwable) {
$context = ['file' => $context->getFile(), 'line' => $context->getLine()];
$context = 'file -> ' . $context->getFile() . PHP_EOL . 'line -> ' . $context->getLine() . PHP_EOL;
}
if (is_array($context) && isset($context[0]) && $context[0] instanceof \Throwable) {
$context = ['file' => $context[0]->getFile(), 'line' => $context[0]->getLine()];
$context = 'file -> ' . $context[0]->getFile() . PHP_EOL . 'line -> ' . $context[0]->getLine() . PHP_EOL;
}
return print_r($context, true) . PHP_EOL;
if (is_string($context)) {
return $context . PHP_EOL;
}
return implode(PHP_EOL, $context) . PHP_EOL;
}
}