/** * Handle uncaught exceptions. * * @param \Exception $exception An uncaught exception. */ public static function exception_handler(\Exception $exception) { global $CFG, $USR; $errcode = $exception->getCode(); $errmsg = $exception->getMessage(); $errcodelabel = $exception instanceof \pdyn\base\Exception ? $exception->get_string_from_code($errcode) : static::getinternalerrorlabel($errcode); if (\pdyn\base\Utils::is_cli_env() === true) { echo 'APP ERROR: ' . $errcodelabel . ': ' . $errmsg . "\n"; } else { // Atlas exceptions' error codes are HTTP status codes, so send one. if ($exception instanceof \pdyn\base\Exception && !empty($errcode) && !headers_sent()) { \pdyn\httputils\Utils::send_status_code($errcode, '', false); } else { \pdyn\httputils\Utils::send_status_code(500, '', false); } $LOG = new \pdyn\log\Logger($CFG->log_general); $LOG->error($errcodelabel . ': ' . $exception); if (isset($_GET['ajax'])) { header('Content-type: application/json'); echo json_encode(['result' => 'fail', 'msg' => $errcodelabel . ': ' . $errmsg]); } else { $debugmode = !empty($CFG) && (bool) $CFG->get('core', 'debugmode', false) === true ? true : false; $isadmin = !empty($USR) && $USR->is_admin === true ? true : false; $backtrace = $isadmin === true || $debugmode === true ? static::format_backtrace($exception->getTrace()) : null; static::write_error_html($errcodelabel, $errmsg, $errcode, $backtrace); } } die; }
/** * Test logging. * * @dataProvider dataprovider_log * @param string $level The level to log at. * @param string $message The message to log. * @param string $expected The expected log mesage. */ public function test_log($level, $message, $expected) { $this->init_logfile(); $log = new \pdyn\log\Logger($this->logfile); $standardlevels = ['emergency', 'alert', 'critical', 'error', 'warning', 'notice', 'info', 'debug']; if (in_array($level, $standardlevels) === true) { $log->{$level}($message); } else { $log->log($level, $message); } $this->assertLog($expected); }