Skip to content

Commit

Permalink
[error] log exceptions and errors consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
nkissebe committed Feb 6, 2025
1 parent 5f0f7b8 commit c9950ae
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 80 deletions.
29 changes: 17 additions & 12 deletions core/bootstrap/Administrator/Providers/ErrorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ public function register()
// Log to php's `error_log()`
$loghandler = new \Monolog\Handler\ErrorLogHandler();

$formatter = new \Monolog\Formatter\LineFormatter(
"%channel%.%level_name%: %message% %context% %extra%", // Format of message in log, default [%datetime%] %channel%.%level_name%: %message% %context% %extra%\n
null, // Datetime format
true, // allowInlineLineBreaks option, default false
true // discard empty Square brackets in the end, default false
);

$loghandler->setFormatter($formatter);

// Alternatively, if you need to a specified file
//$loghandler = new \Monolog\Handler\StreamHandler($app['config']->get('log_path') . '/error.php', 'error', true);

Expand All @@ -54,10 +63,9 @@ public function register()
public function startHandling()
{
// Set the error_reporting
switch ($this->app['config']->get('error_reporting'))
switch ($this->app['config']->get('error_reporting',0))
{
case 'default':
case '-1':
break;

case 'none':
Expand All @@ -66,23 +74,20 @@ public function startHandling()
break;

case 'simple':
error_reporting(E_ERROR | E_WARNING | E_PARSE);
ini_set('display_errors', 1);
break;
$this->app['config']->set('error_reporting', 'relaxed');

case 'maximum':
error_reporting(E_ALL);
ini_set('display_errors', 1);
case 'relaxed':
error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
break;

case 'maximum':
case 'development':
error_reporting(-1);
ini_set('display_errors', 1);
case '-1':
error_reporting(E_ALL);
break;

default:
error_reporting($this->app['config']->get('error_reporting'));
ini_set('display_errors', 1);
error_reporting($this->app['config']->get('error_reporting',0));
break;
}

Expand Down
29 changes: 17 additions & 12 deletions core/bootstrap/Api/Providers/ErrorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public function register()
// Log to php's `error_log()`
$loghandler = new \Monolog\Handler\ErrorLogHandler();

$formatter = new \Monolog\Formatter\LineFormatter(
"%channel%.%level_name%: %message% %context% %extra%", // Format of message in log, default [%datetime%] %channel%.%level_name%: %message% %context% %extra%\n
null, // Datetime format
true, // allowInlineLineBreaks option, default false
true // discard empty Square brackets in the end, default false
);

$loghandler->setFormatter($formatter);

// Alternatively, if you need to a specified file
//$loghandler = new \Monolog\Handler\StreamHandler($app['config']->get('log_path') . '/error.php', 'error', true);

Expand All @@ -53,10 +62,9 @@ public function register()
public function startHandling()
{
// Set the error_reporting
switch ($this->app['config']->get('error_reporting'))
switch ($this->app['config']->get('error_reporting',0))
{
case 'default':
case '-1':
break;

case 'none':
Expand All @@ -65,23 +73,20 @@ public function startHandling()
break;

case 'simple':
error_reporting(E_ERROR | E_WARNING | E_PARSE);
ini_set('display_errors', 1);
break;
$this->app['config']->set('error_reporting', 'relaxed');

case 'maximum':
error_reporting(E_ALL);
ini_set('display_errors', 1);
case 'relaxed':
error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
break;

case 'maximum':
case 'development':
error_reporting(-1);
ini_set('display_errors', 1);
case '-1':
error_reporting(E_ALL);
break;

default:
error_reporting($this->app['config']->get('error_reporting'));
ini_set('display_errors', 1);
error_reporting($this->app['config']->get('error_reporting',0));
break;
}

Expand Down
29 changes: 17 additions & 12 deletions core/bootstrap/Site/Providers/ErrorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public function register()
// Log to php's `error_log()`
$loghandler = new \Monolog\Handler\ErrorLogHandler();

$formatter = new \Monolog\Formatter\LineFormatter(
"%channel%.%level_name%: %message% %context% %extra%", // Format of message in log, default [%datetime%] %channel%.%level_name%: %message% %context% %extra%\n
null, // Datetime format
true, // allowInlineLineBreaks option, default false
true // discard empty Square brackets in the end, default false
);

$loghandler->setFormatter($formatter);

// Alternatively, if you need to a specified file
//$loghandler = new \Monolog\Handler\StreamHandler($app['config']->get('log_path') . '/error.php', 'error', true);

Expand All @@ -53,10 +62,9 @@ public function register()
public function startHandling()
{
// Set the error_reporting
switch ($this->app['config']->get('error_reporting'))
switch ($this->app['config']->get('error_reporting',0))
{
case 'default':
case '-1':
break;

case 'none':
Expand All @@ -65,23 +73,20 @@ public function startHandling()
break;

case 'simple':
error_reporting(E_ERROR | E_WARNING | E_PARSE);
ini_set('display_errors', 1);
break;
$this->app['config']->set('error_reporting', 'relaxed');

case 'maximum':
error_reporting(E_ALL);
ini_set('display_errors', 1);
case 'relaxed':
error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
break;

case 'maximum':
case 'development':
error_reporting(-1);
ini_set('display_errors', 1);
case '-1':
error_reporting(E_ALL);
break;

default:
error_reporting($this->app['config']->get('error_reporting'));
ini_set('display_errors', 1);
error_reporting($this->app['config']->get('error_reporting',0));
break;
}

Expand Down
5 changes: 1 addition & 4 deletions core/bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@
define('JPATH_MANIFESTS', PATH_CORE . '/manifests');
define('JPATH_API', PATH_ROOT . '/api');

define('HVERSION', '2.2.30');

error_reporting(-1);
ini_set('display_errors', 0);
define('HVERSION', '2.4.1');

date_default_timezone_set('UTC');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ COM_CONFIG_FIELD_VALUE_NO_EMAIL="No Email"
COM_CONFIG_FIELD_VALUE_NONE="None"
COM_CONFIG_FIELD_VALUE_PHP_MAIL="PHP Mail"
COM_CONFIG_FIELD_VALUE_SENDMAIL="Sendmail"
COM_CONFIG_FIELD_VALUE_RELAXED="Relaxed"
COM_CONFIG_FIELD_VALUE_SIMPLE="Simple"
COM_CONFIG_FIELD_VALUE_SITE_EMAIL="Site Email"
COM_CONFIG_FIELD_VALUE_SMTP="SMTP"
Expand Down
8 changes: 3 additions & 5 deletions core/components/com_config/models/forms/application.xml
Original file line number Diff line number Diff line change
Expand Up @@ -600,14 +600,12 @@
filter="cmd">
<option
value="default">COM_CONFIG_FIELD_VALUE_SYSTEM_DEFAULT</option>
<option
value="none">COM_CONFIG_FIELD_VALUE_NONE</option>
<option
value="simple">COM_CONFIG_FIELD_VALUE_SIMPLE</option>
<option
value="maximum">COM_CONFIG_FIELD_VALUE_MAXIMUM</option>
<option
value="development">COM_CONFIG_FIELD_VALUE_DEVELOPMENT</option>
value="relaxed">COM_CONFIG_FIELD_VALUE_RELAXED</option>
<option
value="none">COM_CONFIG_FIELD_VALUE_NONE</option>
</field>

<field
Expand Down
39 changes: 18 additions & 21 deletions core/libraries/Hubzero/Database/Driver/Pdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,34 @@ public function __construct($options)
throw new ConnectionFailedException('PDO does not appear to be installed or enabled.', 500);
}

// Make sure the DSN is set
if (!isset($options['dsn']) || !$options['dsn'])
// Try to connect
try
{
throw new ConnectionFailedException('DSN for PDO connection not set.', 500);
}
// Make sure the DSN is set
if (!isset($options['dsn']) || !$options['dsn'])
{
throw new ConnectionFailedException('DSN for PDO connection not set.', 500);
}

// Make sure extra PDO options array is set
if (!isset($options['extras']))
{
$options['extras'] = [];
}
// Make sure extra PDO options array is set
if (!isset($options['extras']))
{
$options['extras'] = [];
}

try
{
// Try to connect
$conn = new \PDO(
// Establish connection string
$this->setConnection(new \PDO(
(string)$options['dsn'],
(string)$options['user'],
(string)$options['password'],
(array)$options['extras']);
(array)$options['extras']
));
}
catch (\PDOException $e)
{
$conn = null;
throw new ConnectionFailedException($e->getMessage(), 500);
}

$this->setConnection($conn);

// Set error reporting to throw exceptions
$this->throwExceptions();

Expand All @@ -80,10 +80,7 @@ public function __construct($options)
**/
public function throwExceptions()
{
if ($this->connection)
{
$this->connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
}
$this->connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

return $this;
}
Expand Down
91 changes: 80 additions & 11 deletions core/libraries/Hubzero/Error/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,34 @@ protected function registerShutdownHandler()
register_shutdown_function(array($this, 'handleShutdown'));
}

private function _loglevel($error_level)
{

switch($error_level)
{
case E_ERROR:
case E_PARSE:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_USER_ERROR:
case E_RECOVERABLE_ERROR:
return("error");

case E_WARNING:
case E_CORE_WARNING:
case E_COMPILE_WARNING:
case E_DEPRECATED:
case E_USER_WARNING:
case E_USER_DEPRECATED:
case E_STRICT:
return("warning");

case E_NOTICE:
return("notice");

}
}

/**
* Handle a PHP error for the application.
*
Expand All @@ -141,10 +169,38 @@ protected function registerShutdownHandler()
*/
public function handleError($level, $message, $file = '', $line = 0, $context = array())
{
$trace = (new \Exception())->getTraceAsString();
$trace = " " . str_replace("\n","\n ",$trace);

$loglevel = $this->_loglevel($level);

$url = " [";
$url .= isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] . "://" : '<null>://';
$url .= isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '<null>';
$url .= isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/<null>';
$url .= "]";

$logmsg = "$message in $file on line $line\n" . $url . "\n" . $trace;

if (is_object($this->logger))
{
$this->logger->log($loglevel, $logmsg);
}
else
{
error_log(" cms.$loglevel " . $logmsg);
}

if (error_reporting() & $level)
{
throw new ErrorException($message, 0, $level, $file, $line);
$exception = new ErrorException($message, 0, $level, $file, $line);

$this->renderer->render($exception);

exit; // failsafe, render() should exit
}

return true;
}

/**
Expand All @@ -155,17 +211,30 @@ public function handleError($level, $message, $file = '', $line = 0, $context =
*/
public function handleException($exception)
{
if (is_object($this->logger) && !in_array($exception->getCode(), [403, 404]))

if ($exception->getCode() < 400 || $exception->getCode() > 499)
{
$this->logger->log(
'error',
$exception->getMessage(),
array(
'code' => $exception->getCode(),
'file' => $exception->getFile(),
'line' => $exception->getLine()
)
);
$trace = $exception->getTraceAsString();
$trace = " " . str_replace("\n","\n ",$trace);

$loglevel = 'error';

$url = " [";
$url .= isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] . "://" : '<null>://';
$url .= isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '<null>';
$url .= isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/<null>';
$url .= "]";

$logmsg = "Uncaught " . get_class($exception) . ": " . $exception->getMessage() . " in " . $exception->getFile() . " on line " . $exception->getLine() . "\n" . $url . "\n" . $trace;

if (is_object($this->logger))
{
$this->logger->log($loglevel, $logmsg);
}
else
{
error_log(" cms.$loglevel " . $logmsg);
}
}

return $this->renderer->render($exception);
Expand Down
Loading

0 comments on commit c9950ae

Please sign in to comment.