Developed with love by KnpLabs Hire us for your project!
126

NelmioJsLoggerBundle

by nelmio

This bundle allows you to log JavaScript errors and others in your backend's Monolog logs

NelmioJsLoggerBundle

The NelmioJsLoggerBundle bundle allows you to log errors happening in the frontend.

Installation

Require the nelmio/js-logger-bundle package in your composer.json and update your dependencies.

$ composer require nelmio/js-logger-bundle

Register the bundle in app/AppKernel.php:

// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new Nelmio\JsLoggerBundle\NelmioJsLoggerBundle(),
    );
}

Import the bundle's routing definition in app/config/routing.yml:

# app/config/routing.yml
NelmioJsLoggerBundle:
    resource: "@NelmioJsLoggerBundle/Resources/config/routing.xml"
    prefix:   /nelmio-js-logger

Automated Error Logging

The bundle exposes two twig functions that you should put in your site
template somewhere.

To enable automatic logging of javascript errors, use nelmio_js_error_logger():

{{ nelmio_js_error_logger() }}

You can optionally change the level (default is ERROR) and remove the surrounding
<script>..</script> tags - don't forget to add them manually!:

<script>
    {{ nelmio_js_error_logger('WARNING', false) }}
</script>

You can also optionally give some extra context information by defining a global
window.nelmio_js_logger_custom_context in the page:

<script>
    window.nelmio_js_logger_custom_context = { userinfo: 'some info', appinfo: 'another useful info' };
    {{ nelmio_js_error_logger('ERROR', false) }}
</script>

Manual Logging from JavaScript

To expose the log() function to your JS code, use nelmio_js_logger():

{{ nelmio_js_logger() }}

You can also change the function name if log is too generic for you:

{{ nelmio_js_logger('my_log_function') }}

The function signature is as such: log(level, message, context). The level and
message are mandatory. The context is a data object that can contain any additional
details you want to store.

Configuration

You can restrict the logging levels accessible from javascript. The point
is that if some of your logging levels email you or notify you in some way,
you probably do not want to allow anyone to send requests and wake you up
at 2AM.

Here is the default configuration that exposes all levels:

# app/config/config.yml
nelmio_js_logger:
    allowed_levels: ['DEBUG', 'INFO', 'NOTICE', 'WARNING', 'ERROR', 'CRITICAL', 'ALERT', 'EMERGENCY']

You can also restrict the logging by ignoring some messages or scripts URLs
with this configuration:

# app/config/config.yml
nelmio_js_logger:
    ignore_messages:
        - originalCreateNotification
    ignore_url_prefixes:
        - https://graph.facebook.com

The URL matches as a prefix to the script URL, and the message will match if
the ignored string is found anywhere in the message.

Properly tracking scripts in other domains

If an error occurs in a script from another domain, browser same origin policy will
make it to be logged with a generic message, file and line number (like
Script error. {"file":"","line":"0", ...}). To properly track these scripts move
them to your domain or load them using CORS:

<script src="//code.jquery.com/jquery-1.9.0.min.js" crossorigin></script>

Note that browser support for <script crossorigin> varies:

As of this writing, only Firefox supports reporting errors for cross-domain
scripts. All WebKit browsers including Chrome is expected to support this very
soon. This isn't a problem with IE at all, since IE already reports errors
to window.onerror irrespective of the domain (yay, security!).

Copyright (c) 2012 Nelmio

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
nelmio_js_logger:
allowed_levels:

# Defaults:
- DEBUG
- INFO
- NOTICE
- WARNING
- ERROR
- CRITICAL
- ALERT
- EMERGENCY
ignore_messages: []
ignore_url_prefixes: []
  • Update install instructions
    By Seldaek, 2 years ago
  • Update changelog
    By Seldaek, 3 years ago
  • Fix undefined index error
    By Seldaek, 3 years ago
  • Update changelog and branch alias
    By Seldaek, 3 years ago
  • Add custom context logging to the regular log function as well, and make it read the value every time a log record is created, refs #6
    By Seldaek, 3 years ago
  • Merge remote-tracking branch 'guillaumepotier/onerror-custom-context'
    By Seldaek, 3 years ago
  • - changed too generic customContext var name. Escaped key and removed unnecessary existance check for customContext. Updated doc accordindly
    By guillaumepotier, 3 years ago
  • - added small doc in README
    By guillaumepotier, 3 years ago
  • - added the possibility to pass a global customContext object to the default nelmio onerror logger, without implementing a custom logger function behavior
    By guillaumepotier, 3 years ago
  • Fix xml configs
    By Seldaek, 3 years ago
  • Update changelog
    By Seldaek, 3 years ago
  • Keep signature BC
    By Seldaek, 3 years ago
  • Rename errors_to_ignore to ignore_messages, scripts_to_ignore to ignore_url_prefixes, fix CS, refs #5
    By Seldaek, 3 years ago
  • Merge remote-tracking branch 'blaugueux/add-configuration'
    By Seldaek, 3 years ago
  • Added configuration to ignore some errors or scripts.
    By blaugueux, 3 years ago
  • Merge pull request #3 from frosas/patch-1
    By Seldaek, 3 years ago
  • Merge pull request #4 from stof/patch-1
    By Seldaek, 3 years ago
  • Added the branch alias for master
    By stof, 3 years ago
  • Note on properly tracking scripts in other domains
    By frosas, 3 years ago
  • Promote 1.0 instead of master branch
    By Seldaek, 4 years ago
  • Add gitignore
    By Seldaek, 4 years ago
  • Update requirements
    By Seldaek, 4 years ago
  • Merge pull request #2 from havvg/master
    By Seldaek, 4 years ago
  • fix Notice: "Undefined index: WARNING" in Logger
    By havvg, 4 years ago
  • Minify image a bit
    By Seldaek, 4 years ago
  • Return a proper image as a response, fixes #1
    By Seldaek, 4 years ago
  • Remove whitespace from the JS code
    By Seldaek, 4 years ago
  • Maintain previous error handler
    By Seldaek, 4 years ago
  • Use proper methods from the LoggerInterface
    By Seldaek, 4 years ago
  • Fix homepage url
    By Seldaek, 4 years ago