Coder Social home page Coder Social logo

grape-middleware-logger's Introduction

A logger for Grape apps

Code Climate Gem Version Build Status

Logs:

  • Request path
  • Parameters
  • Endpoint class name and handler
  • Response status
  • Duration of the request
  • Exceptions
  • Error responses from error!

Installation

Add this line to your application's Gemfile:

gem 'grape', '>= 0.17'
gem 'grape-middleware-logger'

Usage

class API < Grape::API
  # @note Make sure this is above you're first +mount+
  insert_after Grape::Middleware::Formatter, Grape::Middleware::Logger
end

Server requests will be logged to STDOUT by default.

Example output

GET

Started GET "/v1/reports/101" at 2015-12-11 15:40:51 -0800
Processing by ReportsAPI/reports/:id
  Parameters: {"id"=>"101"}
Completed 200 in 6.29ms

POST

Started POST "/v1/reports" at 2015-12-11 15:42:33 -0800
Processing by ReportsAPI/reports
  Parameters: {"name"=>"foo", "password"=>"[FILTERED]"}
  Error: {:error=>"undefined something something bad", :detail=>"Whoops"}
Completed 422 in 6.29ms

Customization

The middleware logger can be customized with the following options:

  • The :logger option can be any object that responds to .info(String)
  • The :filter option can be any object that responds to .filter(Hash) and returns a hash.

For example:

insert_after Grape::Middleware::Formatter, Grape::Middleware::Logger, {
  logger: Logger.new(STDERR),
  filter: Class.new { def filter(opts) opts.reject { |k, _| k.to_s == 'password' } end }.new
}

Using Rails?

Rails.logger and Rails.application.config.filter_parameters will be used automatically as the default logger and param filterer, respectively. This behavior can be overridden by passing the :logger or :filter option when mounting.

You may want to disable Rails logging for API endpoints, so that the logging doesn't double-up. You can achieve this by switching around some middleware. For example:

# config/application.rb
config.middleware.swap 'Rails::Rack::Logger', 'SelectiveLogger'

# config/initializers/selective_logger.rb
class SelectiveLogger
  def initialize(app)
    @app = app
  end

  def call(env)
    if env['PATH_INFO'] =~ %r{^/api}
      @app.call(env)
    else
      Rails::Rack::Logger.new(@app).call(env)
    end
  end
end

Rack

If you're using the rackup command to run your server in development, pass the -q flag to silence the default rack logger.

Credits

Big thanks to jadent's question/answer on stackoverflow for easily logging error responses. Borrowed some motivation from the grape_logging gem and would love to see these two consolidated at some point.

Contributing

  1. Fork it ( https://github.com/ridiculous/grape-middleware-logger/fork )
  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 a new Pull Request

grape-middleware-logger's People

Contributors

marceloboeira avatar ridiculous avatar sirnicolaz avatar tinexw avatar

Watchers

 avatar  avatar

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.