alfred-nutile-inc/incomings-client

此包已被弃用且不再维护。未建议替代包。
关于此包的最新版本(v2.0.2)没有可用的许可证信息。

连接到incomings.io

v2.0.2 2018-05-16 13:00 UTC

README

Latest Version on Packagist Build Status Coverage Status Quality Score Total Downloads

Laravel 和非 Laravel 库连接到 Incomings.io 服务

logo

注册服务 https://incomings.io

然后在单个地方设置并开始监视你的进程,而不是在5个以上的地方!

watching

文档在下面以及 https://incomings.io/help

安装

已在 Laravel 4.2 和 5.x 上测试,更多平台将很快进行测试。

Composer 安装

composer require alfred-nutile-inc/incomings-client:">=2.0"

添加到 app.php

'AlfredNutileInc\Incomings\IncomingsServiceProvider',

注意:如果你使用 Lumen,你需要像这样在 bootstrap/app.php 中启用提供者,而不是上面的内容:

/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
*/

$app->register('AlfredNutileInc\Incomings\IncomingsServiceProvider');

在 .env 中设置

INCOMINGS_URL=https://incomings.io

INCOMINGS_TOKEN=token_of_project

Laravel 5.6

incomings 记录通道添加到你的 config/logging.php 文件

'channels' => [
    'stack' => [
        'driver' => 'stack',
        // Add incomings to the stack:
        'channels' => ['single', 'incomings'],
    ],

    'incomings' => [
        'driver' => 'incomings',
        'level' => 'debug',
    ],
],

向服务发送数据

URL

这是一个最简单的助手。每个项目都有一份

@TODO 修复损坏的图像需要找到正确的一个 url

例如,你可以在 Iron.io 上将其用作 PUSH 队列路由,因为你可以有多个。

或者在服务器上设置一个 cron 作业,每分钟发布你的服务器资源状态或安全状态。

示例 Iron.io

iron

Laravel Facade

假设你即将向队列发送

Queue::push("foo", $data);

现在尝试

$data = ['title' => 'Foo Bar', 'message' => [1,2,3]]

Incomings::send($data);

Queue::push("foo", $data);

为了使上述 Facade 能够工作,你可能需要添加

use AlfredNutileInc\Incomings\IncomingsFacade as Incomings;

注意:如果你使用 Lumen,请确保在 bootstrap/app.php 中启用 facades,如下所示 $app->withFacades();

请参阅 Laravel 文档中的失败队列 https://laravel.net.cn/docs/5.2/queues

例如,我可以使用我的 AppServiceProvider 进行注册


        Queue::failing(function (JobFailed $event) {
            $message = sprintf("Connection %s, Job %s, Exception %s %s %s",
                    $event->connectionName, implode("\n", $event->data), $event->job->getRawBody()
                );
            $data = ['title' => 'Failed Queue From FooBar', 'message' =>

                json_encode($message, JSON_PRETTY_PRINT)
            ];

            Incomings::send($data);
        });

记录器

此设置将允许你使用 Log::info("Some Message") 和其他所有 Log 方法。

你只需在类的顶部设置 use 如下所示

use AlfredNutileInc\Incomings\Log;

从那时起,你的日志消息将发送到 Incomings 然后到记录器

更好的是,你现在可以/应该这样做

    $send = [
        'title' => 'foo',
        'message' => "bar",
    ];
    Log::info($send);

IncomingLogger 将将此数组传递给 Incomings.io,为你的传入提供更多上下文,然后它将仅将消息传递给 Log,就像平常一样。所以你甚至可以做。

    $send = [
        'title' => 'foo',
        'message' => print_r($some_array_payload, 1),
    ];

就像我们在 Log::info 中做的那样,因为我们正在监视日志中的非字符串信息。或者

    $send = [
        'title' => 'foo',
        'message' => json_encode($some_array_payload, JSON_PRETTY_PRINT),
    ];

为了更美观的数据。

中间件

    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'incomings' => \AlfredNutileInc\Incomings\IncomingsMiddleWare::class
    ];

然后将其插入

Route::get('foobar', ['middleware' => 'incomings', function() {

    return "Send to incomings!";

}]);

然后通过POST、GET等方式进入的数据将被发送到Incomings,以便了解数据是否正确进入我的系统等。

您还可以传递一个标题

Route::get('foobar', ['middleware' => 'incomings:My Title', function() {

    return "Send to incomings!";

}]);

Laravel异常处理

只需编辑您的 app/Exceptions/Handler.php,使其使用Incomings异常处理器

之前

     <?php

     namespace App\Exceptions;

     use Exception;
     use Symfony\Component\HttpKernel\Exception\HttpException;
     use IncomingsExceptionHandler as ExceptionHandler;

     class Handler extends ExceptionHandler
     {

之后

    <?php

    namespace App\Exceptions;

    use Exception;
    use Symfony\Component\HttpKernel\Exception\HttpException;
    use AlfredNutileInc\Incomings\IncomingsExceptionHandler as ExceptionHandler;

    class Handler extends ExceptionHandler
    {

如果您使用Lumen,则需要使用IncomingsExceptionHandlerForLumen,如下所示

    <?php

    namespace App\Exceptions;

    use Exception;
    use Symfony\Component\HttpKernel\Exception\HttpException;
    use AlfredNutileInc\Incomings\IncomingsExceptionHandlerForLumen as ExceptionHandler;

    class Handler extends ExceptionHandler
    {

然后,如本路由所示,它将首先将消息发送到Incomings.io

Route::get('/example_exception', function() {

    throw new \Exception("Yo Incomings!!!");

});

将发送如下消息

Bugsnag Too

如果您使用BugSnag等类似服务,请按照他们的说明操作,这样您的app/Exceptions/Handler.php将看起来像这样。

<?php namespace App\Exceptions;

use Exception;
use Bugsnag\BugsnagLaravel\BugsnagExceptionHandler as ExceptionHandler;
use AlfredNutileInc\Incomings\IncomingsFacade as Incomings;

class Handler extends ExceptionHandler
{

    protected $dontReport = [
        HttpException::class,
    ];

    public function report(Exception $e)
    {
        $data = [
            'title' => 'Application Exception Error',
            'message' => sprintf(
                "Error Filename %s \n on line %d \n with message %s \n with Code %s",
                $e->getFile(),
                $e->getLine(),
                $e->getMessage(),
                $e->getCode()
            ),
        ];
        Incomings::send($data);

        return parent::report($e);
    }

}

Laravel 4.2的过滤器

如上所述,插入您的提供者

如果您没有使用我在这里提到的DotEnv https://alfrednutile.info/posts/113

然后更新您的.env.php以包含您的令牌和URL

<?php

return array(
    'INCOMINGS_URL' => 'https://post.incomings.io',
    'INCOMINGS_TOKEN' => 'foo-bar-foo'
);

然后在您的路由中

Route::get('/', ['before' => 'incomings', function()
{
	return View::make('hello');
}]);

最后,在您的过滤器文件中添加以下内容 app/filters.php


Route::filter('incomings', function() {

    try
    {
        $incomings = new \AlfredNutileInc\Incomings\IncomingsFilter();
        $incomings->handle(\Illuminate\Support\Facades\Request::instance());
    }
    catch(\Exception $e)
    {
        Log::error(sprintf("Error with Incomings :( %s", $e->getMessage());
    }

});

这将捕获任何问题而不会破坏您的应用程序。

incomings

Curl

以下是使用Curl的一个示例。在这种情况下,我想每小时查看一下服务器的某些信息。

curl -k -H "Content-Type: application/json" -H "Accept: application/json" -X POST --data @status.json https://post.incomings.io/incomings/f4ac705d-5087-3432-8182-334de6726fc5

然后每小时我可以看到该文件的更新。CronJob将作为root运行此命令

01 * * * * apt-get upgrade -s | grep -i security > /tmp/status.json
03 * * * * curl -k -H "Content-Type: application/json" -H "Accept: application/json" -X POST --data @/tmp/status.json https://post.incomings.io/incomings/foobar

您甚至可以创建一个bash命令来运行所有这些并收集更多数据,如“上次运行”等。

Drupal 8

即将推出...

Drupal 7

即将推出...