当 Slim 和 Monolog 发生错误时如何获取包含日志详细信息的电子邮件
How to get an email with log details when errors occur with Slim and Monolog
我的网站基于 Slim Framework V3 构建,并使用 Monolog。我想在我的网站出现问题时收到电子邮件通知(错误上方的任何日志消息)。换句话说,我想知道何时记录了严重、警报或紧急消息。
理想情况下,我还想在同一封电子邮件中获取错误之前的日志,以便于调试。
这是我当前的 Monolog 配置(Slim 默认配置):
// monolog
$container['logger'] = function ($c) {
/** @var \Slim\Container $c */
$settings = $c->get('settings')['logger'];
$logger = new Monolog\Logger($settings['name']);
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
$logger->pushHandler(new Monolog\Handler\StreamHandler($settings['path'], $settings['level']));
return $logger;
}
这些是日志设置:
return [
// Monolog settings
'logger' => [
'name' => 'slim-app',
'path' => __DIR__ . '/logs/' . $logDate->format('Y-m-d') . '_app.log', //TODO: Make sure logs are above DocumentRoot
'level' => \Monolog\Logger::DEBUG,
]
];
我提出了一个涉及标准 StreamHandler
、NativeMailHandler
和 FingersCrossedHandler
的解决方案。
StreamHandler
只是记录高于给定级别的所有内容,这很好。
现在,当发生一定程度的错误时获取详细电子邮件的技巧是将 NativeMailHandler
与 FingersCrossedHandler
结合起来。
// monolog
$container['logger'] = function ($c) {
/** @var \Slim\Container $c */
$settings = $c->get('settings')['logger'];
$logger = new Monolog\Logger($settings['name']);
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
$logger->pushHandler(new Monolog\Handler\StreamHandler($settings['path'], $settings['level']));
//Handler to send email on critical (or above) errors
//Uses the FingersCrossed strategy which buffers all messages preceeding the critical error
$dt = new DateTime();
$mailHandler = new Monolog\Handler\NativeMailerHandler(
'me@example.com', //TODO: The email address where to send emails
'[ERROR] : Unexpected error happened that requires immediate attention ' . $dt->format('Y-m-d'),
'no-reply@example.com',
$settings['level'],
true,
2000
);
$logger->pushHandler(new Monolog\Handler\FingersCrossedHandler($mailHandler, Monolog\Logger::CRITICAL));
return $logger;
};
这些是我的记录器设置(在 settings.php 中):
return [
// Monolog settings
'logger' => [
'name' => 'slim-app',
'path' => __DIR__ . '/logs/' . $logDate->format('Y-m-d') . '_app.log', //TODO: Make sure logs are above DocumentRoot
'level' => \Monolog\Logger::DEBUG,
]
];
工作原理
根据 monolog source code,FingersCrossedHandler
执行以下操作:
Only requests which actually trigger an error (or whatever your
actionLevel is) will be in the logs, but they will contain all
records, not only those above the level threshold.
因此,当调用级别为 Critical 或更高级别的消息时,此语句将确保我们的 NativeMailerHandler 被 FingersCrossedHandler 使用:
new Monolog\Handler\FingersCrossedHandler($mailHandler, Monolog\Logger::CRITICAL)
希望这对某人有所帮助!
我的网站基于 Slim Framework V3 构建,并使用 Monolog。我想在我的网站出现问题时收到电子邮件通知(错误上方的任何日志消息)。换句话说,我想知道何时记录了严重、警报或紧急消息。
理想情况下,我还想在同一封电子邮件中获取错误之前的日志,以便于调试。
这是我当前的 Monolog 配置(Slim 默认配置):
// monolog
$container['logger'] = function ($c) {
/** @var \Slim\Container $c */
$settings = $c->get('settings')['logger'];
$logger = new Monolog\Logger($settings['name']);
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
$logger->pushHandler(new Monolog\Handler\StreamHandler($settings['path'], $settings['level']));
return $logger;
}
这些是日志设置:
return [
// Monolog settings
'logger' => [
'name' => 'slim-app',
'path' => __DIR__ . '/logs/' . $logDate->format('Y-m-d') . '_app.log', //TODO: Make sure logs are above DocumentRoot
'level' => \Monolog\Logger::DEBUG,
]
];
我提出了一个涉及标准 StreamHandler
、NativeMailHandler
和 FingersCrossedHandler
的解决方案。
StreamHandler
只是记录高于给定级别的所有内容,这很好。
现在,当发生一定程度的错误时获取详细电子邮件的技巧是将 NativeMailHandler
与 FingersCrossedHandler
结合起来。
// monolog
$container['logger'] = function ($c) {
/** @var \Slim\Container $c */
$settings = $c->get('settings')['logger'];
$logger = new Monolog\Logger($settings['name']);
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
$logger->pushHandler(new Monolog\Handler\StreamHandler($settings['path'], $settings['level']));
//Handler to send email on critical (or above) errors
//Uses the FingersCrossed strategy which buffers all messages preceeding the critical error
$dt = new DateTime();
$mailHandler = new Monolog\Handler\NativeMailerHandler(
'me@example.com', //TODO: The email address where to send emails
'[ERROR] : Unexpected error happened that requires immediate attention ' . $dt->format('Y-m-d'),
'no-reply@example.com',
$settings['level'],
true,
2000
);
$logger->pushHandler(new Monolog\Handler\FingersCrossedHandler($mailHandler, Monolog\Logger::CRITICAL));
return $logger;
};
这些是我的记录器设置(在 settings.php 中):
return [
// Monolog settings
'logger' => [
'name' => 'slim-app',
'path' => __DIR__ . '/logs/' . $logDate->format('Y-m-d') . '_app.log', //TODO: Make sure logs are above DocumentRoot
'level' => \Monolog\Logger::DEBUG,
]
];
工作原理
根据 monolog source code,FingersCrossedHandler
执行以下操作:
Only requests which actually trigger an error (or whatever your actionLevel is) will be in the logs, but they will contain all records, not only those above the level threshold.
因此,当调用级别为 Critical 或更高级别的消息时,此语句将确保我们的 NativeMailerHandler 被 FingersCrossedHandler 使用:
new Monolog\Handler\FingersCrossedHandler($mailHandler, Monolog\Logger::CRITICAL)
希望这对某人有所帮助!