将代码移动到新的ServiceProvider后,Laravel日志记录到Sentry不会过滤

时间:2018-01-26 02:42:49

标签: logging laravel-5.5 sentry

我有代码在AppServiceProvider中添加RavenHandler,它工作正常,只按预期记录Logger::INFO及以上。我只是将它移动到一个单独的ExternalLogServiceProvider以便在其他项目中重用,现在它将所有内容从@@ -3,26 +3,12 @@ declare(strict_types=1); namespace Healthcheck\Providers; -use Healthcheck\Mail\Handlers\LaravelMailerHandler; -use Healthcheck\Helpers\Helpers; - use Aws\Ec2\Ec2Client; use Aws\S3\S3Client; use Aws\Sqs\SqsClient; use Illuminate\Support\ServiceProvider; -use Monolog\Formatter\LineFormatter; -use Monolog\Handler\RavenHandler; -use Monolog\Logger; -use Monolog\Processor\IntrospectionProcessor; - -use Raven_Client; - -use Somsip\Logger\Formatter\CallerInlineFormatter; - -use Log; - class AppServiceProvider extends ServiceProvider { /** @@ -39,10 +25,10 @@ class AppServiceProvider extends ServiceProvider * Register any application services. * * @return void + * @codeCoverageIgnore */ public function register() { - // @codeCoverageIgnoreStart $this->app->bind(Ec2Client::class, function ($app) { $params = [ 'region' => 'us-east-1', @@ -75,46 +61,5 @@ class AppServiceProvider extends ServiceProvider } return new S3Client($params); }); - // @codeCoverageIgnoreEnd - - // Setup some custom logging - // FIXME: This could be in it's own ServiceProvider - $monolog = Log::getMonolog(); - // Change the default formatter - $monolog->getHandlers()[0]->setFormatter(new CallerInlineFormatter()); - $monolog->getHandlers()[0]->setLevel(config('app.logLevel')); - // Get all output from logger, but ignore references to non-app classes - $ignores = [ - 'Writer', - 'Facade' - ]; - $monolog->pushProcessor(new IntrospectionProcessor(Logger::DEBUG, $ignores)); - - // @codeCoverageIgnoreStart - // FIXME: This could be in it's own ServiceProvider - if (!app()->environment('testing')) { - // Email critical errors to admin - $monolog->pushHandler( - new LaravelMailerHandler( - config('mail.admin'), - 'Healthcheck: CRITICAL ERROR encountered on ' . strtoupper(config('services.aws.account')), - Logger::CRITICAL - ) - ); - - // Use central log if available - if (config('services.raven.dsn')) { - $client = new Raven_Client(config('services.raven.dsn')); - $handler = new RavenHandler( - $client, - Logger::ERROR - ); - $handler->setFormatter(new LineFormatter("%message% %context% %extra%\n")); - $monolog->pushHandler($handler); - } - } - // @codeCoverageIgnoreEnd } } ----------------- app/Providers/ExternalLogServiceProvider.php ----------------- new file mode 100644 index 0000000..957c414 @@ -0,0 +1,53 @@ +<?php +declare(strict_types=1); + +namespace Healthcheck\Providers; + +use Healthcheck\Mail\Handlers\LaravelMailerHandler; + +use Illuminate\Support\ServiceProvider; + +use Monolog\Formatter\LineFormatter; +use Monolog\Handler\RavenHandler; +use Monolog\Logger; + +use Raven_Client; + +use Log; + +class ExternalLogServiceProvider extends ServiceProvider +{ + /** + * Setup external logging + * + * @return void + * @codeCoverageIgnore + */ + public function register() + { + if (!app()->environment('testing')) { + // Email critical errors to admin + $monolog = Log::getMonolog(); + $monolog->pushHandler( + new LaravelMailerHandler( + config('mail.admin'), + 'Healthcheck: CRITICAL ERROR encountered on ' . strtoupper(config('services.aws.account')); + Logger::CRITICAL + ) + ); + + // Use central log if available + if (config('services.raven.dsn')) { + $client = new Raven_Client(config('services.raven.dsn')); + $handler = new RavenHandler( + $client, + Logger::ERROR + ); + $handler->setFormatter(new LineFormatter("%message% %context% %extra%\n")); + $monolog->pushHandler($handler); + } + } + } +} ------------------ app/Providers/InlineLogServiceProvider.php ------------------ new file mode 100644 index 0000000..de97081 @@ -0,0 +1,37 @@ +<?php +declare(strict_types=1); + +namespace Healthcheck\Providers; + +use Illuminate\Support\ServiceProvider; + +use Monolog\Logger; +use Monolog\Processor\IntrospectionProcessor; + +use Raven_Client; + +use Somsip\Logger\Formatter\CallerInlineFormatter; + +use Log; + +class InlineLogServiceProvider extends ServiceProvider +{ + /** + * Setup some custom logging + * + * @return void + */ + public function register() + { + $monolog = Log::getMonolog(); + // Change the default formatter + $monolog->getHandlers()[0]->setFormatter(new CallerInlineFormatter()); + $monolog->getHandlers()[0]->setLevel(config('app.logLevel')); + // Get all output from logger, but ignore references to non-app classes + $ignores = [ + 'Writer', + 'Facade' + ]; + $monolog->pushProcessor(new IntrospectionProcessor(Logger::DEBUG, $ignores)); + } +} -------------------------------- config/app.php -------------------------------- index b72115c..cfb3820 100644 @@ -174,6 +174,8 @@ return [ */ Healthcheck\Providers\AppServiceProvider::class, Healthcheck\Providers\AuthServiceProvider::class, + Healthcheck\Providers\ExternalLogServiceProvider::class, + Healthcheck\Providers\InlineLogServiceProvider::class, Healthcheck\Providers\EventServiceProvider::class, Healthcheck\Providers\RouteServiceProvider::class, ], 向上发送到Sentry。

我无法理解为什么会发生这种情况。当然要么会记录,要么不会记录。我不明白为什么过滤器突然被忽略了......

这是差异:

//below is my Factory snippet
factory.getStocks = function(){
var promise =  firebase.database().ref().child("Stock").once('value');
var qpromise = $q.when(promise).then(callback)
return stock;
};


function callback(snapshot){   
var i=0;
snapshot.forEach(function(child)
{
   stocks[i] = {
        name: child.key,
        holding: child.val(),
        price: -1,
        value: -1
    };
   i = i+1;
  });
  return stocks;
}

InlineLogServiceProvider只是将Class :: method()添加到每个日志条目,并且工作正常。它只是同时拆分为它自己的ServiceProvider。

谢谢,

1 个答案:

答案 0 :(得分:0)

当AppServiceProvider没有设置时,调用CallerInlineFormatter没有按预期工作。日志条目未按预期进行,并且格式也表示它们已通过RavenHandler的过滤器。不太确定为什么,但在这个美容修复上花了太长时间,所以只需将它全部恢复到AppServiceProvider中。它正在做它应该做的事情。