logstasher rspec travis-ci ruby Rubygems

3 years after

Logstasher

Awesome Logging for Rack applications

History

This is a fork of shadabahmed/logstasher. It has been updated to use the latest event schema and customized to better fit the needs of Reevoo though it is still generally useful. It is not backward compatible with its progenitor.

Purpose

This gem makes it easy to generate logstash compatible logs for your applications.

A request that looks like this in your production.log:

Started GET "/login" for 10.109.10.135 at 2013-04-30 08:59:01 -0400
Processing by SessionsController#new as HTML
  Rendered sessions/new.html.haml within layouts/application (4.3ms)
  Rendered shared/_javascript.html.haml (0.6ms)
  Rendered shared/_flashes.html.haml (0.2ms)
  Rendered shared/_header.html.haml (52.9ms)
  Rendered shared/_title.html.haml (0.2ms)
  Rendered shared/_footer.html.haml (0.2ms)
Completed 200 OK in 532ms (Views: 62.4ms | ActiveRecord: 0.0ms | ND API: 0.0ms)

Will look like this in your production.log:

{"tags":["request"],"method":"GET","path":"/","format":"html","controller":"file_servers"
,"action":"index","status":200,"duration":28.34,"view":25.96,"db":0.88,"ip":"127.0.0.1","route":"file_servers#index",
"parameters":"","ndapi_time":null,"uuid":"e81ecd178ed3b591099f4d489760dfb6","user":"[email protected]",
"site":"internal","@timestamp":"2013-04-30T13:00:46.354500+00:00","@version":"1"}

From there, it's trivial to forward them to your logstash indexer.

Installation for Rails Application

In your Gemfile:

gem 'rv-logstasher'

Configure your <environment>.rb e.g. development.rb

# Enable the logstasher logs for the current environment
config.logstasher.enabled = true

# Optionally silience the standard logging to <environment>.log
config.logstasher.silence_standard_logging = true

Logging params

By default, Logstasher will add params as a JSON encoded string. To disable, add the following to your <environment>.rb

# Disable logging of request parameters
config.logstasher.include_parameters = false

Adding custom fields to the log

Since some fields are very specific to your application, e.g., user_name, it is left upto you to add them. Here's how to do it:

# Create a file - config/initializers/logstasher.rb

if LogStasher.enabled
  LogStasher.append_fields do |fields|
    # This block is run in application_controller context,
    # so you have access to all controller methods
    fields[:user] = current_user && current_user.mail
    fields[:site] = request.path =~ /^\/api/ ? 'api' : 'user'
  end
end

Filtering out controller params

You may not want to log certain parameters which have sensitive information in them, e.g. password. This can be set using the filter_parameters option:

# Filter out some field you don't want to show
config.logstasher.filter_parameters << 'foo'

Note that by default this is set to ['password', 'password_confirmation'], so be careful when explicitly setting, as you may lose this filtering:

# NOTE: password and password_confirmation will no longer be filtered
config.logstasher.filter_parameters = ['foo']

Any filtered parameter will still show in the params field, but it's value will be [FILTERED].

Installation for Grape Application

In your Gemfile:

gem "rv-logstasher"
gem "grape_logging"

Init logger:

module TestApp
  def self.logger
    @logger ||= LogStasher.logger_for_app('app_name', Rack::Directory.new("").root, STDOUT)
  end
end

Setup Grape request/exception logging

module TestApp
  class API < Grape::API
    logger TestApp.logger
    use GrapeLogging::Middleware::RequestLogger, logger: TestApp.logger

    rescue_from TestApp::NotFound do |err|
      # Tag your exception
      API.logger.info(exception: err, tags: "rescued_exception", status: 404)
      error_response(message: "Not found", status: 404)
    end

    rescue_from :all do |e|
      API.logger.error(e)
      error_response(message: e.message, status: 500)
    end
  end
end

Copyright

Copyright (c) 2016 Reevoo Ltd, released under the MIT license

Related Repositories

martini

martini

Classy web framework for Go ...

ruby-bookmarks

ruby-bookmarks

Ruby and Ruby on Rails bookmarks collection ...

rack-logstasher

rack-logstasher

Like logstasher, but for rack apps. ...

ruby-bookmarks

ruby-bookmarks

Ruby and Ruby on Rails bookmarks collection ...

rails-logstasher

rails-logstasher

Logstash logging system for Ruby on Rails (fork from https://github.com/rurounij ...


Top Contributors

quixoten shadabahmed davidsevcik seddy amalkov m-barthelemy lpil kovyrin fergmastaflex alext brainopia Simbul zerobearing2 gouravtiwari mtwentyman reneklacan

Releases

-   v1.0.2 zip tar
-   v1.0.1 zip tar
-   v1.0.0 zip tar
-   v1.0.0.rc5 zip tar
-   v1.0.0.rc4 zip tar
-   v1.0.0.rc3 zip tar
-   v1.0.0.rc2 zip tar
-   v1.0.0.rc1 zip tar
-   v1.0.0.beta8 zip tar
-   v1.0.0.beta7 zip tar
-   v1.0.0.beta6 zip tar
-   v1.0.0.beta5 zip tar
-   v1.0.0.beta4 zip tar
-   v1.0.0.beta3 zip tar
-   v1.0.0.beta2 zip tar
-   v1.0.0.beta1 zip tar
-   v0.6.5 zip tar
-   v0.6.2 zip tar
-   v0.6.1 zip tar
-   v0.6.0 zip tar
-   v0.5.3 zip tar
-   v0.5.2 zip tar
-   v0.5.0 zip tar
-   v0.4.9 zip tar
-   v0.4.8 zip tar
-   v0.4.7 zip tar
-   v0.4.5 zip tar
-   v0.4.1 zip tar
-   v0.4.0 zip tar
-   v0.3.1 zip tar