-
-
Save cyakimov/7ed2d8e0a34a28a550cf to your computer and use it in GitHub Desktop.
Example of how to extend Lumen monolog implementation for New Relic and potentially other handlers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Foo; | |
// app/Myapp.php | |
use Monolog\Logger; | |
use Laravel\Lumen\Application; | |
use Monolog\Handler\StreamHandler; | |
use Monolog\Formatter\LineFormatter; | |
use Monolog\Handler\NewRelicHandler; | |
class Myapp extends Application | |
{ | |
/** | |
* This replaces the inline array used in \Lumen\Application to allow multiple handlers | |
* | |
* @return void | |
*/ | |
protected function registerLogBindings() | |
{ | |
$this->singleton('Psr\Log\LoggerInterface', function () { | |
return new Logger('lumen', $this->getMonologHandler()); | |
}); | |
} | |
/** | |
* Extends the default logging implementation with additional handlers if configured in .env | |
* | |
* @return array of type \Monolog\Handler\AbstractHandler | |
*/ | |
protected function getMonologHandler() | |
{ | |
$handlers = []; | |
$handlers[] = (new StreamHandler(storage_path('logs/lumen.log'), Logger::DEBUG))->setFormatter(new LineFormatter(null, null, true, true)); | |
if (extension_loaded('newrelic')) { | |
// configure New Relic monolog Handler | |
newrelic_set_appname(env('APP_NAME', 'Hodor v2')); | |
$handlers[] = new NewRelicHandler(Logger::ERROR, true); | |
} | |
return $handlers; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// bootstrap/start.php | |
.... | |
$app = new \Foo\Myapp( | |
realpath(__DIR__.'/../') | |
); | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment