Skip to content

Instantly share code, notes, and snippets.

@CarsonF
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CarsonF/ddb402d2534a84f23c7e to your computer and use it in GitHub Desktop.
Save CarsonF/ddb402d2534a84f23c7e to your computer and use it in GitHub Desktop.
<?php
namespace Log;
use Common\Config;
use GMO\Cache\CacheKeys;
use GMO\Common\String;
use Log\Handler\LogstashHandler;
use Log\Processor\EnvProcessor;
use Log\Processor\RequestProcessor;
use Monolog\Handler\ChromePHPHandler;
use Monolog\Logger;
use Routing\RouterListener;
use Silex\Application;
use Silex\ServiceProviderInterface;
class LogServiceProvider implements ServiceProviderInterface
{
public function register(Application $app) {
$app['logger.redis'] = $app->share(function ($app) {
/** @var \GMO\Cache\Redis $redis */
$redis = $app[CacheKeys::REDIS_NEW];
$redis->selectDb(4);
return $redis;
});
$app['logger.handlers'] = $app->share(function ($app) {
$handlers = [
new LogstashHandler($app['logger.redis'], 'GL', 'gl_logging'),
];
if (PHP_SAPI !== 'cli' && Config::isDevelopment()) {
$handlers[] = new ChromePHPHandler(Logger::DEBUG, false);
}
return $handlers;
});
$app['logger.processors'] = $app->share(function ($app) {
return [
new EnvProcessor(Config::getEnvironment()),
new RequestProcessor($app['request_stack'])
];
});
$app['logger.new'] = $app->protect(function ($name) use ($app) {
$name = String::className($name) ?: $name;
return new Logger($name, $app['logger.handlers'], $app['logger.processors']);
});
$app['logger'] = $app->share(function ($app) {
return $app['logger.new']('App');
});
$app->register(new RouterListener());
}
public function boot(Application $app) {
$app['dispatcher']->addSubscriber(new HttpLogListener($app['logger']));
}
}
<?php
namespace Log\Processor;
use Symfony\Component\HttpFoundation\RequestStack;
class RequestProcessor
{
protected $requestStack;
public function __construct(RequestStack $requestStack) {
$this->requestStack = $requestStack;
}
public function __invoke(array $record) {
if (!$request = $this->requestStack->getCurrentRequest()) {
return $record;
}
$params = [
'method' => $request->getMethod(),
'host' => $request->getHost(),
'path' => $request->getPathInfo(),
'query' => $request->query->all(),
];
if ($request->isMethod('POST')) {
$params['body'] = $request->request->all();
}
$record['extra']['request'] = $params;
return $record;
}
}
@CarsonF
Copy link
Author

CarsonF commented Apr 9, 2015

So I end up calling $app['logger.new']('channelName') in lots of places.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment