简体   繁体   English

当Slim和Monolog发生错误时,如何获取包含日志详细信息的电子邮件

[英]How to get an email with log details when errors occur with Slim and Monolog

My website is built over Slim Framework V3, and uses Monolog. 我的网站基于Slim Framework V3构建,并使用Monolog。 I want to get an email notification when something goes wrong on my website (any log message above Error). 当我的网站出现问题时(错误上方的任何日志消息),我想收到一封电子邮件通知。 In other words, I want to know when a Critical, Alert or Emergency message is logged. 换句话说,我想知道何时记录紧急,警报或紧急消息。

Ideally, I want to also get the logs right before the error also in the same email, to make it easier to debug. 理想情况下,我也希望在同一封电子邮件中也能在错误发生之前得到日志,以使其更易于调试。

This is my current Monolog configuration (Slim default): 这是我当前的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;
}

These are the log settings: 这些是日志设置:

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,
    ]
];

I came up with a solution which involves the standard StreamHandler , the NativeMailHandler and the FingersCrossedHandler . 我想出了一个涉及标准StreamHandlerNativeMailHandlerFingersCrossedHandler的解决方案。

The StreamHandler simply logs everything above the given level, which is fine. StreamHandler只需记录高于给定级别的所有内容,就可以了。

Now, the trick to get detailed emails when a certain level of error happens is to combine a NativeMailHandler with a FingersCrossedHandler . 现在,在发生一定程度的错误时获取详细电子邮件的技巧是将NativeMailHandlerFingersCrossedHandler结合使用。

// 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;
};

These are my logger settings (in settings.php): 这些是我的记录器设置(在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,
    ]
];

How it works 这个怎么运作

According to the monolog source code , the FingersCrossedHandler does the following : 根据monolog源代码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. 只有真正触发错误(或您的actionLevel不管是什么)的请求才会出现在日志中,但它们将包含所有记录,不仅是那些超出级别阈值的记录。

Therefore, this statement will make sure our NativeMailerHandler is used by the FingersCrossedHandler when a message with level Critical or more is called: 因此,此语句将确保在调用级别为Critical或更高级别的消息时,FingersCrossedHandler使用我们的NativeMailerHandler:

new Monolog\Handler\FingersCrossedHandler($mailHandler, Monolog\Logger::CRITICAL)

Hope this helps someone! 希望这对某人有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM