Permalink
Browse files

Add basic error logging and logger functionality

  • Loading branch information...
1 parent 520d8e2 commit 9978e08b07532d11297cd56a7209b8df37cd22e5 @Seldaek Seldaek committed Aug 19, 2012
View
6 Controller/LogController.php
@@ -10,7 +10,11 @@ class LogController extends Controller
{
public function createAction(Request $request)
{
- if ($this->get('nelmio_js_logger.logger')->handle($request)) {
+ $level = $request->query->get('level');
+ $message = $request->query->get('msg');
+ $context = $request->query->get('context', array());
+
+ if ($this->get('nelmio_js_logger.logger')->write($level, $message, $context)) {
return new Response('OK', 201);
}
View
5 DependencyInjection/Configuration.php
@@ -26,9 +26,8 @@ public function getConfigTreeBuilder()
$rootNode
->children()
->arrayNode('allowed_levels')
- ->addDefaultsIfNotSet()
- ->default($levels)
- ->children()
+ ->defaultValue($levels)
+ ->prototype('scalar')
->validate()
->ifNotInArray($levelsCI)
->thenInvalid('The level %s is not supported. Please choose one of '.json_encode($levels))
View
6 DependencyInjection/NelmioJsLoggerExtension.php
@@ -22,8 +22,10 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
- // TODO
- $container->setParameter('nelmio_js_logger.allowed_levels', array());
+ $levels = array_map(function ($level) {
+ return strtolower($level);
+ }, $config['allowed_levels']);
+ $container->setParameter('nelmio_js_logger.allowed_levels', $levels);
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
View
31 Logger.php
@@ -0,0 +1,31 @@
+<?php
+
+namespace Nelmio\JsLoggerBundle;
+
+use Symfony\Component\HttpKernel\Log\LoggerInterface;
+
+class Logger
+{
+ protected $logger;
+ protected $allowedLevels;
+
+ public function __construct(LoggerInterface $logger, array $allowedLevels)
+ {
+ $this->logger = $logger;
+ $this->allowedLevels = $allowedLevels;
+ }
+
+ public function write($level, $message, array $context = array())
+ {
+ if (!in_array(strtolower($level), $this->allowedLevels)) {
+ return false;
+ }
+ if (!$message) {
+ return false;
+ }
+
+ $this->logger->{'add'.$level}($message, $context);
@stof
stof Aug 20, 2012

This does not rely on the method on the LoggerInterface but expects getting the Monolog implementation

+
+ return true;
+ }
+}
View
2 Resources/config/routing.xml
@@ -4,7 +4,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
- <route id="NelmioJsLoggerBundle_log" pattern="/logs">
+ <route id="nelmio_js_logger_log" pattern="/log">
<default key="_controller">NelmioJsLoggerBundle:Log:create</default>
</route>
</routes>
View
12 Resources/config/services.xml
@@ -5,14 +5,20 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
- <parameter key="nelmio_js_monolog.logger.class">Nelmio\JsLoggerBundle\Logger</parameter>
+ <parameter key="nelmio_js_logger.logger.class">Nelmio\JsLoggerBundle\Logger</parameter>
+ <parameter key="nelmio_js_logger.twig_extension.class">Nelmio\JsLoggerBundle\TwigExtension</parameter>
</parameters>
<services>
- <service id="nelmio_js_monolog.logger" class="%nelmio_js_monolog.logger.class%">
+ <service id="nelmio_js_logger.logger" class="%nelmio_js_logger.logger.class%">
<argument type="service" id="logger" />
- <argument>%nelmio_js_monolog.allowed_levels%</argument>
+ <argument>%nelmio_js_logger.allowed_levels%</argument>
<tag name="monolog.logger" channel="frontend" />
</service>
+
+ <service id="nelmio_js_logger.twig_extension" class="%nelmio_js_logger.twig_extension.class%">
+ <argument type="service" id="router" />
+ <tag name="twig.extension" />
+ </service>
</services>
</container>
View
74 TwigExtension.php
@@ -0,0 +1,74 @@
+<?php
+
+namespace Nelmio\JsLoggerBundle;
+
+use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
+
+class TwigExtension extends \Twig_Extension
+{
+ private $router;
+
+ public function __construct(UrlGeneratorInterface $router)
+ {
+ $this->router = $router;
+ }
+
+ public function getFunctions()
+ {
+ return array(
+ 'nelmio_js_error_logger' => new \Twig_Function_Method($this, 'initErrorLogger', array('is_safe' => array('html', 'js'))),
+ 'nelmio_js_logger' => new \Twig_Function_Method($this, 'initLogger', array('is_safe' => array('html', 'js'))),
+ );
+ }
+
+ public function initErrorLogger($level = 'error', $includeScriptTag = true)
+ {
+ $url = addslashes($this->router->generate('nelmio_js_logger_log'));
+
+ $js = <<<JS
+window.onerror = function(errorMsg, file, line) {
@stof
stof Aug 20, 2012

what about keeping the existing one and calling it ?

@Seldaek
Seldaek Aug 20, 2012

Sure, though it's quite rare people listen to that in my experience. Fixed.

+ var e = encodeURIComponent;
+ (new Image()).src = '$url?msg=' + e(errorMsg) +
+ '&level=$level' +
+ '&context[file]=' + e(file) +
+ '&context[line]=' + e(line) +
+ '&context[page]=' + e(document.location.href);
+};
+JS;
+
+ if ($includeScriptTag) {
+ $js = "<script>\n$js\n</script>";
+ }
+
+ return $js;
+ }
+
+ public function initLogger($function = 'log', $includeScriptTag = true)
+ {
+ $url = addslashes($this->router->generate('nelmio_js_logger_log'));
+
+ $js = <<<JS
+var $function = function(level, message, contextData) {
+ var context, key, e = encodeURIComponent;
+ context = '';
+ if (contextData) {
+ for (key in contextData) {
+ context += '&context[' + key + ']=' + e(contextData[key]);
+ }
+ }
+ (new Image()).src = '$url?msg=' + e(message) + '&level=' + e(level) + context;
+};
+JS;
+
+ if ($includeScriptTag) {
+ $js = "<script>\n$js\n</script>";
+ }
+
+ return $js;
+ }
+
+ public function getName()
+ {
+ return 'nelmio_js_logger';
+ }
+}

0 comments on commit 9978e08

Please sign in to comment.