定制 sil-org/yii2-email-log-target 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sil-org/yii2-email-log-target

最新稳定版本:1.1.2

Composer 安装命令:

composer require sil-org/yii2-email-log-target

包简介

Yii2 log target for sending data to email without trace information

README 文档

README

Custom version of yii\log\EmailTarget to exclude trace information from messages.

What & Why

The built in yii\log\EmailTarget class does a nice job of serializing and sending log and exception messages via email. Unfortunately when the message is an exception itself the exception is serialized to string using default \Exception::__toString() method which includes stack trace information. The Yii logger component allows configuration of whether or not to include trace information but it does not apply to exception messages.

This class is a modified version of yii\log\EmailTarget to ensure no trace information is set in the email.

Have the log prefix (if used)

Example configuration ['components']['log']['targets'] array):

[
    '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;
    },
],

License

This is released under the MIT license (see LICENSE file).

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 6
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-15