aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorn-peugnet <n.peugnet@free.fr>2020-04-23 00:29:34 +0200
committerVincent Peugnet <33429034+vincent-peugnet@users.noreply.github.com>2020-04-25 17:38:14 +0200
commit94714d94f82d49cf8536d16505c47aceffb01e91 (patch)
treeae12d092fe5e4f4e97f431fe0a24295769380822
parentdf545ce931b32d340e40831d0c3afbc6eabda29d (diff)
downloadwcms-94714d94f82d49cf8536d16505c47aceffb01e91.tar.gz
wcms-94714d94f82d49cf8536d16505c47aceffb01e91.zip
feat: warning, info & debug directly a throwable
-rw-r--r--app/class/Logger.php54
-rw-r--r--index.php8
-rw-r--r--tests/LoggerTest.php142
3 files changed, 179 insertions, 25 deletions
diff --git a/app/class/Logger.php b/app/class/Logger.php
index 104e23a..e2ab4b6 100644
--- a/app/class/Logger.php
+++ b/app/class/Logger.php
@@ -2,7 +2,7 @@
namespace Wcms;
-use InvalidArgumentException;
+use RuntimeException;
use Throwable;
/**
@@ -19,17 +19,18 @@ class Logger
*
* @param string $path the logfile's path
* @param int $verbosity 0: no log, 1: errors only, 2: add warn, 3: add info, 4: add debug.
+ * @throws RuntimeException if failed to create logfile.
*/
- public static function init(string $path, int $verbosity = 4)
+ public static function init(string $path, int $verbosity = 4): void
{
if (!is_dir(dirname($path))) {
- throw new InvalidArgumentException("Parent directory of '$path' does not exist.");
+ throw new RuntimeException("Parent directory of '$path' does not exist.");
}
if (!is_writable(dirname($path))) {
- throw new InvalidArgumentException("Parent directory of '$path' is not writable.");
+ throw new RuntimeException("Parent directory of '$path' is not writable.");
}
if (is_file($path) && !is_writable($path)) {
- throw new InvalidArgumentException("The logfile '$path' is not writable.");
+ throw new RuntimeException("The logfile '$path' is not writable.");
}
self::$file = fopen($path, "a");
if (self::$file === false) {
@@ -49,6 +50,11 @@ class Logger
vfprintf(self::$file, date('c') . " %-9s %s(%d) $msg\n", $args);
}
+ protected static function exceptionmessage(Throwable $e): string
+ {
+ return "{$e->getMessage()} in {$e->getFile()}({$e->getLine()})";
+ }
+
/**
* Log an error message using printf format.
*/
@@ -60,7 +66,7 @@ class Logger
}
/**
- * Log a xarning message using printf format.
+ * Log a warning message using printf format.
*/
public static function warning(string $msg, ...$args)
{
@@ -90,12 +96,12 @@ class Logger
}
/**
- * Log an exception.
+ * Log an exception as an error.
*/
- public static function exception(Throwable $e, bool $withtrace = false)
+ public static function errorex(Throwable $e, bool $withtrace = false)
{
if (self::$verbosity > 0) {
- $msg = "{$e->getMessage()} in {$e->getFile()}({$e->getLine()})";
+ $msg = self::exceptionmessage($e);
if ($withtrace) {
// TODO: Maybe print a more beautiful stack trace.
$msg .= PHP_EOL . $e->getTraceAsString();
@@ -103,4 +109,34 @@ class Logger
self::write('ERROR', $msg);
}
}
+
+ /**
+ * Log an exception as a warning.
+ */
+ public static function warningex(Throwable $e)
+ {
+ if (self::$verbosity > 1) {
+ self::write('WARN', self::exceptionmessage($e));
+ }
+ }
+
+ /**
+ * Log an exception as an info.
+ */
+ public static function infoex(Throwable $e)
+ {
+ if (self::$verbosity > 2) {
+ self::write('INFO', self::exceptionmessage($e));
+ }
+ }
+
+ /**
+ * Log an exception as a debug.
+ */
+ public static function debugex(Throwable $e)
+ {
+ if (self::$verbosity > 3) {
+ self::write('DEBUG', self::exceptionmessage($e));
+ }
+ }
}
diff --git a/index.php b/index.php
index fb05952..230b0e6 100644
--- a/index.php
+++ b/index.php
@@ -7,7 +7,11 @@ session_start();
require('./vendor/autoload.php');
-Logger::init('w_error.log', 2);
+try {
+ Logger::init('w_error.log', 2);
+} catch (Throwable $e) {
+ die('Unable to init logs: ' . $e->getMessage());
+}
$app = new Wcms\Application();
$app->wakeup();
@@ -33,6 +37,6 @@ try {
if (isreportingerrors()) {
Sentry\captureException($e);
}
- Logger::exception($e, true);
+ Logger::errorex($e, true);
echo '<h1>⚠ Woops ! There is a little problem : </h1>', $e->getMessage(), "\n";
}
diff --git a/tests/LoggerTest.php b/tests/LoggerTest.php
index b4b87f4..eb237ce 100644
--- a/tests/LoggerTest.php
+++ b/tests/LoggerTest.php
@@ -3,7 +3,7 @@
namespace Wcms\Tests;
use Exception;
-use InvalidArgumentException;
+use RuntimeException;
use PHPUnit\Framework\TestCase;
use Throwable;
use Wcms\Logger;
@@ -40,7 +40,7 @@ class LoggerTest extends FilesTest
$dir = 'not/existing/path';
$file = "$dir/w_error.log";
$this->assertDirectoryNotExists($dir);
- $this->expectException(InvalidArgumentException::class);
+ $this->expectException(RuntimeException::class);
$this->expectExceptionMessage("Parent directory of '$file' does not exist.");
Logger::init($file);
$this->assertFileNotExists($file);
@@ -55,7 +55,7 @@ class LoggerTest extends FilesTest
$file = "$dir/w_error.log";
$this->assertDirectoryExists($dir);
$this->assertDirectoryNotIsWritable($dir);
- $this->expectException(InvalidArgumentException::class);
+ $this->expectException(RuntimeException::class);
$this->expectExceptionMessage("Parent directory of '$file' is not writable.");
Logger::init($file);
$this->assertFileNotExists($file);
@@ -70,7 +70,7 @@ class LoggerTest extends FilesTest
$this->assertDirectoryExists(dirname($file));
$this->assertDirectoryIsWritable(dirname($file));
$this->assertNotIsWritable($file);
- $this->expectException(InvalidArgumentException::class);
+ $this->expectException(RuntimeException::class);
$this->expectExceptionMessage("The logfile '$file' is not writable.");
Logger::init($file);
$this->assertFileNotExists($file);
@@ -224,35 +224,35 @@ class LoggerTest extends FilesTest
/**
* @test
- * @dataProvider exceptionNotLoggedProvider
+ * @dataProvider errorexNotLoggedProvider
*/
- public function exceptionNotLoggedTest(int $verbosity): void
+ public function errorexNotLoggedTest(int $verbosity): void
{
Logger::init($this->logfile, $verbosity);
- Logger::exception(new Exception('Error'));
+ Logger::errorex(new Exception('Error'));
$this->assertEmpty(file_get_contents($this->logfile));
}
- public function exceptionNotLoggedProvider(): array
+ public function errorexNotLoggedProvider(): array
{
return [[0]];
}
/**
* @test
- * @dataProvider exceptionLoggedProvider
+ * @dataProvider errorexLoggedProvider
*/
- public function exceptionLoggedTest(int $verbosity, Throwable $e, string $expected, int $line)
+ public function errorexLoggedTest(int $verbosity, Throwable $e, string $expected, int $line)
{
Logger::init($this->logfile, $verbosity);
- Logger::exception($e);
+ Logger::errorex($e);
$file = __FILE__;
$line += 258;
$expected = " [ ERROR ] tests/LoggerTest.php(248) $expected in $file($line)\n";
$this->assertEquals($expected, substr(file_get_contents($this->logfile), 25));
}
- public function exceptionLoggedProvider(): array
+ public function errorexLoggedProvider(): array
{
return [
[1, new Exception('Test 1'), 'Test 1', 0],
@@ -265,13 +265,127 @@ class LoggerTest extends FilesTest
/**
* @test
*/
- public function exceptionBacktraceTest(): void
+ public function errorexBacktraceTest(): void
{
Logger::init($this->logfile, 1);
- Logger::exception(new Exception('Error'), true);
+ Logger::errorex(new Exception('Error'), true);
$content = file_get_contents($this->logfile);
$expected = " [ ERROR ] tests/LoggerTest.php(271) Error ";
$this->assertEquals($expected, substr($content, 25, 43));
$this->assertRegExp('/(#\d+ [\w\/\.]*\(\d+\): .*\)\n)+#\d+ \{main\}\n/U', $content);
}
+
+ /**
+ * @test
+ * @dataProvider warningexNotLoggedProvider
+ */
+ public function warningexNotLoggedTest(int $verbosity): void
+ {
+ Logger::init($this->logfile, $verbosity);
+ Logger::warningex(new Exception('Error'));
+ $this->assertEmpty(file_get_contents($this->logfile));
+ }
+
+ public function warningexNotLoggedProvider(): array
+ {
+ return [[0],[1]];
+ }
+
+ /**
+ * @test
+ * @dataProvider warningexLoggedProvider
+ */
+ public function warningexLoggedTest(int $verbosity, Throwable $e, string $expected, int $line)
+ {
+ Logger::init($this->logfile, $verbosity);
+ Logger::warningex($e);
+ $file = __FILE__;
+ $line += 311;
+ $expected = " [ WARN ] tests/LoggerTest.php(301) $expected in $file($line)\n";
+ $this->assertEquals($expected, substr(file_get_contents($this->logfile), 25));
+ }
+
+ public function warningexLoggedProvider(): array
+ {
+ return [
+ [2, new Exception('Test 1'), 'Test 1', 0],
+ [3, new Exception('Test 2'), 'Test 2', 1],
+ [4, new Exception('Test 3'), 'Test 3', 2],
+ ];
+ }
+
+ /**
+ * @test
+ * @dataProvider infoexNotLoggedProvider
+ */
+ public function infoexNotLoggedTest(int $verbosity): void
+ {
+ Logger::init($this->logfile, $verbosity);
+ Logger::infoex(new Exception('Error'));
+ $this->assertEmpty(file_get_contents($this->logfile));
+ }
+
+ public function infoexNotLoggedProvider(): array
+ {
+ return [[0],[1],[2]];
+ }
+
+ /**
+ * @test
+ * @dataProvider infoexLoggedProvider
+ */
+ public function infoexLoggedTest(int $verbosity, Throwable $e, string $expected, int $line)
+ {
+ Logger::init($this->logfile, $verbosity);
+ Logger::infoex($e);
+ $file = __FILE__;
+ $line += 350;
+ $expected = " [ INFO ] tests/LoggerTest.php(340) $expected in $file($line)\n";
+ $this->assertEquals($expected, substr(file_get_contents($this->logfile), 25));
+ }
+
+ public function infoexLoggedProvider(): array
+ {
+ return [
+ [3, new Exception('Test 1'), 'Test 1', 0],
+ [4, new Exception('Test 2'), 'Test 2', 1],
+ ];
+ }
+
+ /**
+ * @test
+ * @dataProvider debugexNotLoggedProvider
+ */
+ public function debugexNotLoggedTest(int $verbosity): void
+ {
+ Logger::init($this->logfile, $verbosity);
+ Logger::debugex(new Exception('Error'));
+ $this->assertEmpty(file_get_contents($this->logfile));
+ }
+
+ public function debugexNotLoggedProvider(): array
+ {
+ return [[0],[1],[2],[3]];
+ }
+
+ /**
+ * @test
+ * @dataProvider debugexLoggedProvider
+ */
+ public function debugexLoggedTest(int $verbosity, Throwable $e, string $expected, int $line)
+ {
+ Logger::init($this->logfile, $verbosity);
+ Logger::debugex($e);
+ $file = __FILE__;
+ $line += 388;
+ $expected = " [ DEBUG ] tests/LoggerTest.php(378) $expected in $file($line)\n";
+ $this->assertEquals($expected, substr(file_get_contents($this->logfile), 25));
+ }
+
+ public function debugexLoggedProvider(): array
+ {
+ return [
+ [4, new Exception('Test 4'), 'Test 4', 0],
+ ];
+ }
}