定制 swayok/laravel-extended-errors 二次开发

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

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

swayok/laravel-extended-errors

Composer 安装命令:

composer require swayok/laravel-extended-errors

包简介

Laravel logging extender used to render logs and exceptions as HTML to store to files and/or send via email and telegram

README 文档

README

This package provides additional drivers (telegram and email) and log renderers for Laravel logging system. Renderers generate HTML code to be written to log files or sent to external services (slack, email, telegram).

Example of logs: screenshot_log.png

Example of exception log: screenshot_exception.png

1. Installation

Laravel <= 5.5

Add require to composer.json and run composer update

"require": {
    "swayok/laravel-extended-errors": "5.5.*",
}

Proceed using step 2 in branch laravel_up_to_5.5

Laravel 5.6+ to Laravel 9

Add require to composer.json and run composer update

"require": {
    "swayok/laravel-extended-errors": ":6.0",
}

Laravel 10+

Add require to composer.json and run composer update

"require": {
    "swayok/laravel-extended-errors": ":7.0",
}

Configuration

Service provider

Automatically added via package auto-discovery.

Renderers

HTML renderer injection into daily and single channel drivers

'single' => [
    'driver' => 'single',
    'path' => storage_path('logs/laravel.html'),
    'tap' => [\LaravelExtendedErrors\Formatter\HtmlFormatter::class],
    'level' => 'debug',
],

'daily' => [
    'driver' => 'daily',
    'path' => storage_path('logs/laravel.html'),
    'level' => 'debug',
    'days' => 7,
    'tap' => [\LaravelExtendedErrors\Formatter\HtmlFormatter::class],
], 

Drivers

All changes will be applied to 'channels' array in config/logging.php.

Telegram channel

'telegram' => [
    'driver' => 'telegram',
    'token' => env('LOG_TELEGRAM_API_KEY'),
    'chat_id' => env('LOG_TELEGRAM_CHAT_ID'),
    'proxy' => [
        'type' => env('LOG_TELEGRAM_PROXY_TYPE', 'http'),
        'host' => env('LOG_TELEGRAM_PROXY_HOST'),
        'port' => env('LOG_TELEGRAM_PROXY_PORT'),
        'user' => env('LOG_TELEGRAM_PROXY_USER'),
        'password' => env('LOG_TELEGRAM_PROXY_PASSWORD'),
    ],
    'level' => 'debug',
    'message_prefix' => env('LOG_TELEGRAM_MESSAGE_PREFIX', mb_ucfirst(env('APP_ENV', ''))),
    'bubble' => false',
]

Rendered logs and exceptions are sent as documents to provided chat_id.

Option message_prefix will wrap passed string into [] and add it in front of the message resulting in something like [Prod] *Error* Something happened. when 'message_prefix' => "Prod" and message is *Error* Something happened..

Proxy settings:

  • proxy.type can be: http, socks4, socks5, nginx;
  • proxy.user and proxy.password can be empty if proxy has no authorisation;
  • proxy.host for http, socks4 and socks5 types usually is an IP address like 192.168.1.1;
  • proxy.host for nginx type should be a full url like http://bot.yourdomain.com ,https://bot.yourdomain.com or http://bot.yourdomain.com:8080;
  • proxy.port is not used for this type.

Proxy uses Basic Auth method to send user and password. Other auth methods are not supported right now. Make an issue if you need some (make sure CURL supports it).

Nginx vhost config to proxy requests to api.telegram.org

server {
    listen 80;
    server_name bot.yourdomain.com;
    location / {
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass https://api.telegram.org/;
        client_max_body_size 100M;
    }
}

Email channel (probably won't work with Laravel 10+)

'email' => [
    'driver' => 'email',
    'level' => 'debug',
    'subject' => 'Server log',
    'sender' => 'local@test.lh',
    'receiver' => ['your@email.com'],
    'bubble' => false',
],

Warning: there is no limit for exceptions, and you may eventually get thousands of errors at once if you use this channels in high loaded project.

Sentry

Actually there is no channel driver for Sentry but here is quick tutorial on how to add exceptions reporting to Sentry via Handler.php:

Require sentry packages:

"require": {
    "sentry/sentry": "^1.8",
    "sentry/sentry-laravel": "^0.8.0",
}

In your app/Exception/Handler.php update report() method to look like:

public function report(Exception $exception) {
    if ($this->shouldReport($exception) && app()->bound('sentry')) {
        app('sentry')->captureException($exception, ['extra' => \LaravelExtendedErrors\Utils::getMoreInformationAboutRequest()]);
    }

    parent::report($exception);
}

To .env file add url provided by Sentry when you create a new project there. It will look like this:

SENTRY_DSN=https://8158bc7a6110...e7b152b@sentry.domain.com/1

Note that there is 'extra' key used to send report to Sentry. This one stores all data from current request just like exception logs generated by HTML Renderer. This provides better understanding of what happened.

Whoops exception page replacement

To replace HTML exception pages rendered by built-in Laravel's Whoops handler with exception page rendered by this package you need to add 'replace_whoops' => true, to config/logging.php

Custom user info data collector

ExceptionHtmlRenderer prints minimal set of user info: primary key and class. To print more info you can provide your own user info collector using ExceptionHtmlRenderer::setUserInfoCollector(\Closure). Closure
receives no arguments and must return null (not authenticated) or array. Also, it is on your side how you get user object. ExceptionHtmlRenderer by default uses request()->user();

swayok/laravel-extended-errors 适用场景与选型建议

swayok/laravel-extended-errors 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.36k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 06 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「logging」 「php」 「monolog」 「exception」 「error」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 swayok/laravel-extended-errors 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 swayok/laravel-extended-errors 我们能提供哪些服务?
定制开发 / 二次开发

基于 swayok/laravel-extended-errors 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-06-20