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.
Supports Rails 4 (permitted parameters) as well as a CoffeeScript implementation of the client-side code.
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 errror 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' mount RailsClientLogger::Engine, :at => "logger"
- 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
MIT License
Copyright (c) 2013 Girish Sonawane (girish dot sonawane at gmail dot com)