From e5f59d39268b552c6b8416d5f0d8294ec19e26f3 Mon Sep 17 00:00:00 2001 From: n-peugnet Date: Tue, 21 Apr 2020 01:44:13 +0200 Subject: feat: add Logger class to log catched errors Previously, if Sentry was not enabled, the Exceptions weren't logged anywhere. I expect you to use this class in the future @vincent-peugnet ! --- app/class/Logger.php | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 app/class/Logger.php (limited to 'app') diff --git a/app/class/Logger.php b/app/class/Logger.php new file mode 100644 index 0000000..939dbf8 --- /dev/null +++ b/app/class/Logger.php @@ -0,0 +1,94 @@ + 0) { + self::write('ERROR', $msg, $args); + } + } + + /** + * Log a xarning message using printf format. + */ + public static function warning(string $msg, ...$args) + { + if (self::$verbosity > 1) { + self::write('WARN', $msg, $args); + } + } + + /** + * Log an info message using printf format. + */ + public static function info(string $msg, ...$args) + { + if (self::$verbosity > 2) { + self::write('INFO', $msg, $args); + } + } + + /** + * Log a debug message using printf format. + */ + public static function debug(string $msg, ...$args) + { + if (self::$verbosity > 3) { + self::write('DEBUG', $msg, $args); + } + } + + /** + * Log an exception. + */ + public static function exception(Throwable $e, bool $withtrace = false) + { + if (self::$verbosity > 0) { + $msg = $e->getMessage(); + if ($withtrace) { + // TODO: Maybe print a more beautiful stack trace. + $msg .= PHP_EOL . $e->getTraceAsString(); + } + self::write('ERROR', $msg); + } + } +} -- cgit v1.2.3