Skip to content

Instantly share code, notes, and snippets.

@stevethomas
Last active September 2, 2020 17:53
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save stevethomas/ec497a3d2936754ab799 to your computer and use it in GitHub Desktop.
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
<?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;
}
}
<?php
// bootstrap/start.php
....
$app = new \Foo\Myapp(
realpath(__DIR__.'/../')
);
...
@angellee177
Copy link

do you have the tutorial link ?
thank you.

@stevethomas
Copy link
Author

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