silinternational/yii2-email-log-target

用于发送数据到电子邮件而不包含跟踪信息的Yii2日志目标

1.0.1 2017-04-13 14:30 UTC

This package is auto-updated.

Last update: 2024-09-17 19:59:42 UTC


README

yii\log\EmailTarget的定制版本,用于从消息中排除跟踪信息。

是什么 & 为什么

内置的yii\log\EmailTarget类在序列化和通过电子邮件发送日志和异常消息方面做得很好。不幸的是,当消息本身是一个异常时,异常会被使用默认的 \Exception::__toString()方法序列化为字符串,该方法包含堆栈跟踪信息。Yii日志组件允许配置是否包含跟踪信息,但这不适用于异常消息。

这个类是yii\log\EmailTarget的修改版本,以确保电子邮件中不设置跟踪信息。

保留日志前缀(如果使用)

示例配置 ['components']['log']['targets'] 数组)

[
    'class' => 'Sil\Log\EmailTarget',
    'levels' => ['error'],
    'except' => [
        'yii\web\HttpException:401',
        'yii\web\HttpException:404',
    ],
    'logVars' => [], // Disable logging of _SERVER, _POST, etc.
    'prefix' => function($message) use ($appEnv) {
        $prefix = 'env=' . $appEnv . PHP_EOL;

        // There is no user when a console command is run
        try {
            $appUser = \Yii::$app->user;
        } catch (\Exception $e) {
            $appUser = Null;
        }
        if ($appUser && ! \Yii::$app->user->isGuest){
            $prefix .= 'user='.\Yii::$app->user->identity->email . PHP_EOL;
        }

        // Try to get requested url and method
        try {
            $request = \Yii::$app->request;
            $prefix .= 'Requested URL: ' . $request->getUrl() . PHP_EOL;
            $prefix .= 'Request method: ' . $request->getMethod() . PHP_EOL;
        } catch (\Exception $e) {
            $prefix .= 'Requested URL: not available';
        }

        return PHP_EOL . $prefix;
    },
],

许可证

此软件基于MIT许可证发布(见LICENSE文件)。