michaeljennings/snapshot 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

michaeljennings/snapshot

Composer 安装命令:

composer require michaeljennings/snapshot

包简介

A PHP package that takes a snapshot of your application and allows you to retrieve it later to help with debugging.

README 文档

README

A PHP package that stores a whoops-like snapshot of your application and allows you to retrieve it later to help with debugging.

The package also comes with Slack integration so you can receive notifications whenever a snapshot is captured.

Planned Features

  • File Store
  • More views to choose from

Installation

This package requires PHP 5.4+ and includes laravel support.

To install through composer you can either use composer require michaeljennings/snapshot or include the package in your composer.json.

"michaeljennings/snapshot": "0.3.*"

Then run either composer install or composer update to download the package.

Laravel Integration

To use the package with Laravel 5 add the snapshot service provider to the list of service providers in config/app.php.

'providers' => array(

  'Michaeljennings\Snapshot\SnapshotServiceProvider'
  
);

Then add the Snapshot facade to the aliases array.

'aliases' => array(

  'Snapshot' => 'Michaeljennings\Snapshot\Facades\Snapshot',

);

Then use php artisan vendor:publish to publish the config and database migrations.

The package comes with database migrations if you want to use a database store. To run the migrations use php artisan migrate to set up the snapshot database tables.

To use the package you can either use the Snapshot facade or if you prefer using dependency injection snapshot is bound to the IOC container by its interface.

Snapshot::capture();

public function __construct(Michaeljennings\Snapshot\Contracts\Snapshot $snapshot)
{
    $this->snapshot = $snapshot;
}

Laravel 4 Integration

To use the package with Laravel 4 add the snapshot service provider to the list of service providers in app/config/app.php.

'providers' => array(

  'Michaeljennings\Snapshot\Laravel4ServiceProvider'
  
);

Then add the Snapshot facade to the aliases array.

'aliases' => array(

  'Snapshot' => 'Michaeljennings\Snapshot\Facades\Snapshot',

);

Then use php artisan config:publish --path=vendor/michaeljennings/snapshot/config michaeljennings/snapshot to publish the config.

The package comes with database migrations if you want to use a database store. To run the migrations use php artisan migrate --package="michaeljennings/snapshot" --path=vendor/michaeljennings/snapshot/database/migrations to set up the snapshot database tables.

To use the package you can either use the Snapshot facade or if you prefer using dependency injection snapshot is bound to the IOC container by its interface.

Snapshot::capture();

public function __construct(Michaeljennings\Snapshot\Contracts\Snapshot $snapshot)
{
    $this->snapshot = $snapshot;
}

Usage

To get started we first need to instantiate the snapshot class.

If you are using the laravel integration this is done for you when you register the service provider.

// Require the composer autoload
require "vendor/autoload.php";

// Get the package config
$config = require "path/to/config/snapshot.php";

// Instantiate the dependencies 
$store = new $config['store']['class']($config);
$renderer = new $config['renderer'];
$dispatcher = new \Michaeljennings\Snapshot\Dispatcher(new \League\Event\Emitter());

// Create the snapshot class
$snapshot = new Snapshot($store, $renderer, $dispatcher, $config);

Taking a Snapshot

To take a snapshot of your application use the capture method.

$snapshot = new Snapshot($store, $renderer, $dispatcher, $config);

$snapshot->capture();

This will store all of the debug stack trace and any server, post, get, file, cookie, session and environment variables.

If you want to store any additional data, i.e. the current user, you can pass an array of data to the capture method.

$snapshot->capture(['user_id' => 1]);

If you are specifically capturing an exception you can use the captureException method. The benefit of this method is you can override the exception message and code by passing them in. You can still store any additional data by passing it as the 4th parameter.

$snapshot->captureException($exception);

$snapshot->captureException($exception, 500, 'Internal Server Error', ['user_id' => 1]);

Rendering a Snapshot

To render a snapshot use the render method, this takes one parameter which is the id of the snapshot.

$snapshot->render(1);

Finding a Snapshot

Alternatively if you want to choose how to render the snapshot yourself you can get the snapshot by its id using the find method.

$snapshot->find(1);

Slack Integration

Snapshot also comes with Slack support through the excellent mankz/slack package.

In your config file just enable slack integration, and then add in your incoming webhook endpoint, channel, and username and you shall start receiving slack messages whenever a snapshot is captured.

This is very useful for catching errors before they are reported.

Customising the Message

By default the message will be #{snapshot-id} A new snapshot has been captured.

If you want to customise this message then you can extend the Michaeljennings\Snapshot\Listeners\SendToSlack.php event listener and override the getMessage method.

Then you simply need to update the listener subscribed in the snapshot config to the Michaeljennings\Snapshot\Events\SnapshotCaptured event to your new listener.

Adding Event Listeners

It is possible that you want something to happen every time a snapshot is captured. An example of this is how we send a message to slack whenever a snapshot is captured.

To do this we add in event listeners. There are two ways we can go about this, either you can subscribe you listeners in the listeners array in the config, or you can call the addListener method on the slack class.

To add one into the config set the key as the event you are subscribing to, and the value as an array of listeners.

'listeners' => [

    'Michaeljennings\Snapshot\Events\SnapshotCaptured' => [
        'Michaeljennings\Snapshot\Listeners\SendToSlack',
        'MyCustomListener'
    ]

]

To subscribe a listener using the addListener method pass the event name you are subscribing to as the first parameter, and then either a class name or a closure to use as a listener.

$snapshot->addListener('Michaeljennings\Snapshot\Events\SnapshotCaptured', 'Michaeljennings\Snapshot\Listeners\SendToSlack');

$snapshot->addListener('Michaeljennings\Snapshot\Events\SnapshotCaptured', function($event) {
    // Handle event
});

michaeljennings/snapshot 适用场景与选型建议

michaeljennings/snapshot 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.97k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2015 年 05 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 michaeljennings/snapshot 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 8.97k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 4
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-05-03