Last active
August 29, 2015 14:18
-
-
Save CarsonF/ddb402d2534a84f23c7e to your computer and use it in GitHub Desktop.
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 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'])); | |
} | |
} |
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 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So I end up calling
$app['logger.new']('channelName')
in lots of places.