Rails engine to log from Client side (Browser) javascript to server log file. To catch those nasty production Javascript errors. Provides a variety of safe logging functions e.g. jsLogger.debug(), jsLogger.error().
There are 5 levels of logging: debug, info, warn, error and fatal.
If exception_notification is found, sends
notifications for error
and fatal
levels.
Supports Rails 4 (permitted parameters).
For Rails 3.x use gem version 0.0.2
.
Add this line to your application's Gemfile:
gem 'rails-client-logger'
gem 'rails-client-logger', '0.0.2'
And then execute:
$ bundle install
Then simply execute following generator command. It inserts the required routes and javascript files and you're ready to rock!
$ rails g rails_client_logger
jsLogger.info("simple info message");
jsLogger.warn("a warning");
try {
throw new Error('unhandled exception');
}
catch (e) {
jsLogger.fatal(e);
}
Log all unhandled javascript errors:
window.onerror = function (message, url, line_number) {
jsLogger.fatal("Uncaught error in: " + url + ":" + line_number + "\nDetails: " + message);
};
The logged messages will appear in the normal rails log (i.e. development.log or staging.log or production.log).
The gem uses a controller to send messages to the server, in some cases you may need to authorize the controller actions for it to work correctly (otherwise you will get an authorization error). Below is a how-to guide for CanCan, but the same principles can be applied to other authorization gems.
-
Create a new controller
logger_controller.rb
that inherits fromRailsClientLoggersController
like this:class LoggerController < RailsClientLogger::RailsClientLoggersController skip_authorization_check end
-
Add a new route in
routes.rb
match 'logger/rails_client_logger/log' => 'logger#log', via: :post mount RailsClientLogger::Engine, :at => "logger"
In case you need more flexibility with the url handling, for example if you have an API only app.
- Modify routes.rb to include namespaced API routes
namespace :api do
namespace :v1 do
mount RailsClientLogger::Engine, :at => "apilogger"
end
end
- Specify global vars like
window.jsLoggerBasePath = "/api/v1"
window.jsLoggerUrl = "/apilogger/rails_client_logger/log"
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
meghali - for the idea
girishso - for the implementation
elthariel - for Rails 4 support and CoffeeScript implementation
rhino232 - for namespaced URLs
MIT License
Copyright (c) 2013 Girish Sonawane (girish dot sonawane at gmail dot com)