nelmio/js-logger-bundle
Composer 安装命令:
composer require nelmio/js-logger-bundle
包简介
Adds logging of JS errors in your Symfony application
关键字:
README 文档
README
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.php"
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.
Optional: Log the whole javascript stack trace with Stacktrace.js
Stacktrace.js is a small js-library to create javascript stack traces anywhere.
# app/config/config.yml
nelmio_js_logger:
use_stacktrace_js: ~
If stacktrace.js is loaded before an error occurs, an array with stack trace information (file, line, column) will be logged additionally to the other information.
By default, the stacktracejs javascript file is loaded from https://cdnjs.cloudflare.com/ajax/libs/stacktrace.js/1.3.1/stacktrace.min.js you can change this by setting the path value in the config.yml
# app/config/config.yml
nelmio_js_logger:
use_stacktrace_js:
path: 'your-url-for-stacktracejs'
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!).
nelmio/js-logger-bundle 适用场景与选型建议
nelmio/js-logger-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.26M 次下载、GitHub Stars 达 176, 最近一次更新时间为 2012 年 08 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「log」 「logging」 「javascript」 「JS」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nelmio/js-logger-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nelmio/js-logger-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nelmio/js-logger-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Generates a Blade directive exporting all of your named Laravel routes. Also provides a nice route() helper function in JavaScript.
Caching and compression for Twig assets (JavaScript and CSS).
A pretty nice way to expose your translation messages to your JavaScript.
A Zend Framework module that sets up Monolog for logging in applications.
Asynchronous Sentry for Symfony - Fire and forget
PHP client for the Google Closure Compiler API in one file.
统计信息
- 总下载量: 1.26M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 179
- 点击次数: 20
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2012-08-20