Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tag: v1.0.0
Fetching contributors…

Cannot retrieve contributors at this time

112 lines (74 sloc) 2.74 kB

What is that for?

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.

Installation

Add this line to your application's Gemfile:

For Rails 4.x

gem 'rails-client-logger'

For Rails 3.x

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

Usage

Logging a message

jsLogger.info("simple info message");
jsLogger.warn("a warning");

Log Error

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);
};

View Logs

The logged messages will appear in the normal rails log (i.e. development.log or staging.log or production.log).

Authorization

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.

CanCan

  1. Create a new controller logger_controller.rb that inherits from RailsClientLoggersController like this:

    class LoggerController < RailsClientLogger::RailsClientLoggersController
      skip_authorization_check
    end
  2. Add a new route in routes.rb

    match 'logger/rails_client_logger/log' => 'logger#log'
    mount RailsClientLogger::Engine, :at => "logger"

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Credits

meghali - for the idea

girishso - for the implementation

elthariel - for Rails 4 support and CoffeeScript implementation

License

MIT License

Copyright (c) 2013 Girish Sonawane (girish dot sonawane at gmail dot com)

Jump to Line
Something went wrong with that request. Please try again.