Last active
September 2, 2020 17:53
-
-
Save stevethomas/ec497a3d2936754ab799 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 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 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__.'/../') | |
); | |
... |
No I don't, I am not using Lumen anymore.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
do you have the tutorial link ?
thank you.