Commit 8022d089 authored by Igor Wiedler's avatar Igor Wiedler

MonologExtension for logging of requests and errors

Sample usage:

$app = new Application();

$app->register(new MonologExtension(), array(
	'monolog.class_path' => __DIR__.'/vendor/monolog/src',
	'monolog.logfile' => __DIR__.'/application.log',
));

$app->get('/hello', function() use ($app) {
	$app['monolog']->addDebug('currently at hello');
	return 'Hello World!';
});

$app->get('/error', function() {
	throw new RuntimeException('Some error');
});

$app->run();
parent 87929817
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex\Extension;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Silex\Application;
use Silex\ExtensionInterface;
use Symfony\Component\HttpKernel\Exception\BaseHttpException;
class MonologExtension implements ExtensionInterface
{
public function register(Application $app)
{
$app['monolog'] = $app->share(function () use ($app) {
$log = new Logger('myapp');
$app['monolog.configure']($log);
return $log;
});
$app['monolog.configure'] = $app->protect(function ($log) use ($app) {
$log->pushHandler(new StreamHandler($app['monolog.logfile'], Logger::DEBUG));
});
if (isset($app['monolog.class_path'])) {
$app['autoloader']->registerNamespace('Monolog', $app['monolog.class_path']);
}
$app->before(function() use ($app) {
$app['monolog']->addInfo($app['request']->getMethod() . ' ' . $app['request']->getRequestUri());
});
$app->error(function(\Exception $e) use ($app) {
if ($e instanceof BaseHttpException) {
$app['monolog']->addWarning($e->getStatusCode() . ' ' . $app['request']->getMethod() . ' ' . $app['request']->getRequestUri());
} else {
$app['monolog']->addError($e->getMessage());
}
});
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment