GithubHelp home page GithubHelp logo

exception_logger's Introduction

Exception Logger for Rails 4

This gem is evolved from the outdated exception_logger gem. I refined it with the mountable rails engine, making it convenient to mount to any existing rails app.

Install

Inside your Gemfile:

# Rails 4.1.x
gem "exception_logger", :github => "ryancheung/exception_logger"

# Rails 4.0.x
gem "exception_logger", :github => "ryancheung/exception_logger", tag: 'v0.1.0'

and then run:

bundle install

Gem Dependencies

Please check if all those requirements are satisfied on your environment.

  • will_paginate >= 3.0.5 # (used for pagination)

  • rails >= 4.0.0

Inside your ApplicationController:

include ExceptionLogger::ExceptionLoggable # loades the module
rescue_from Exception, :with => :log_exception_handler # tells rails to forward the 'Exception' (you can change the type) to the handler of the module

Inside your config/routes.rb

mount ExceptionLogger::Engine => "/exception_logger"

Database Setup

Use

rake exception_logger:install:migrations

This will create a database migration

TIMESTAMP_create_exception_logger_logged_exceptions.exception_logger.rb

inside db/migrations

Then

rake db:migrate

Authentication

If you want, and believe me you want that, you can add the following lines to your environment to enable Authentication

Example uses the declarative_authorization gem

in config/environment/production.rb:

config.after_initialize do
  ExceptionLogger::LoggedExceptionsController.class_eval do
    # include any custom auth modules you need
    # (declarative authorization gem used)
    filter_resource_access
    # just add any code you want to be included in the controller ;)
    # optional, sets the application name for the rss feeds
    self.application_name = "Your Application Name"
  end
end

and (if you use declarative_authorization) add this to your authorization_rules.rb file:

role :admin do
  has_permission_on :logged_exceptions, :to => :manage
  ...

Testing & Development

Not implemented yet.

Usage

To localize the interface:

en:
  exception_logger:
    logged_exceptions:
      index:
        title: Logged Exceptions
        filters: Filters
        latest_exceptions: Latest Exceptions
        exception: Exception
        controller_action: Controller / Action
        dates: Dates
        today: Today
        last_few_days: Last few days
        last_7_days: Last 7 days
        last_30_days: Last 30 days
      show:
        title: Logged Exception

To log extra data, in your ApplicationController:

self.exception_data = Proc.new { |controller| "Extra exception infomation here!" }

or

self.exception_data = :extra_exception_data

def extra_exception_data
  "Extra exception infomation here!"
end

To add authentication, in config/application.rb or the specific environment file:

# set up authencation of exception_logger
config.after_initialize do
  ExceptionLogger::LoggedExceptionsController.class_eval do
    before_filter :authorize

    protected

    def authorize
      redirect_to main_app.admin_login_path unless admin_loggined?
    end

    def admin_loggined?
      session[:admin_user_id].present?
    end
  end
end

License

MIT License. See LICENSE for details.

exception_logger's People

Contributors

dangerouse avatar dependabot[bot] avatar descovi avatar eduardoazevedo3 avatar ryancheung avatar spint avatar viniciuscostass avatar zhouguangming avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

exception_logger's Issues

wrong number of params exception

https://github.com/ryancheung/exception_logger/blob/master/lib/exception_logger.rb#L62
def filter_sub_parameters(params, rails_filter_parameters) params.each do |key, value| if(value.class != Hash and value.class != ActiveSupport::HashWithIndifferentAccess) params[key] = '[FILTERED]' if rails_filter_parameters.include?(key.to_sym) else params[key] = filter_sub_parameters(value) end end params end

it keep raise error : as we call the same method with wrong number of params

Rails 4

Any chance you got this working with Rails 4 ?

Thanks

Mass assignment error

Hi there,

I'm using the gem for Rails 4 (thanks for making that happen!) but am having an issue with mass assignment when the whitelist is true in rails config:

WARNING: Can't mass-assign protected attributes for ExceptionLogger::LoggedException: exception_class, controller_name, action_name, message, backtrace, request

Could you add the fields at attr_accessible or adjust the controller accordingly to avoid having to turn whitelisted attributes off?

Thanks!

Catch routing error

I dont understand how is possible to catch routing error
I tried with

rescue_from ActionController::RoutingError, with: :log_exception_handler

Mongoid support

Does this gem only work with ActiveRecord? I have Rails 4.1.8 with Mongoid 4.0.2 and I get

uninitialized constant ExceptionLogger::ActiveRecord

Logging exception outside the context of controller

I'm using exception logger in classes that are not within a controller context. This is the case for example for resque workers.
Resque workers are classes with a class method named perform in order to perform background jobs. For logging an exception I need a class method that logs exceptions. I include this module to all my workers:

module WorkerExceptionLogger

  def self.included(target)
    target.extend(ClassMethods)
  end

  module ClassMethods
    def log_exception(exception, data = nil)
      message = exception.message.inspect
      message << "\n* Extra Data\n\n#{data}" unless data.blank?
      ExceptionLogger::LoggedException.create! \
          :exception_class => exception.class.name,
          :controller_name => self.to_s,
          :action_name => caller_locations(1, 1)[0].label,
          :message => message,
          :backtrace => exception.backtrace
    end
  end

This method sets "controller_name" to the name of the class and "action_name" to the name of the calling function.

It would be cool to have this functionallity directly in the gem:

  1. class method for logging an exception
  2. universal create_from_exception (i.e. that not only works with conrollers)

Add mail notification

I'm trying in my branch to add notification by mail.
Do you think is a good idea?
Bye

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.