Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rack-logjam

Package Overview
Dependencies
Maintainers
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rack-logjam

  • 0.2.1
  • Rubygems
  • Socket score

Version published
Maintainers
2
Created
Source

Rack::Logjam

Logs helpful HTTP information on Rack requests.

Installation

Add this line to your application's Gemfile:

gem 'rack-logjam'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rack-logjam

Usage

Including the Middleware

Grape

Include the middleware in your API classes with the use statement

module AwesomeApp
  class ApiV1 < Grape::API
    use Rack::Logjam::Grape::Middleware
  end
end
Rails

Require the middleware in config/application.rb

config.middleware.use( 'Rack::Logjam::Middleware' )

Configuration

At a minimum one must specify the logger that rack-logjam will use.

Rack::Logjam.configure |c|
  c.logger = Rails.logger
end
Formatters

Rack::Logjam includes several default formatters that are registered by default: Array, FormUrlencoded, Json, TextPlain, and Xml. Formatters are registered and selected by mime-types. If a the mime-type of a response does not have a regostered formatter, it will output a message to the log in place of the body specifying the lack of formatter and mime-type.

Additionally, you can configure custom mime-types with formatters in the configuration file.

Rack::Logjam.configure |c|
  # register custom mime-types with inlcuded formatters
  c.register_formatter 'application/vnd.awesome-app-v1+x-www-form-urlencoded', :FormUrlencoded
  c.register_formatter 'application/vnd.awesome-app-v1+json', :Json                        
  c.register_formatter 'application/vnd.awesome-app-v1+text', :TextPlain                        
  c.register_formatter 'application/vnd.awesome-app-v1+xml', :Xml

  # register custom mime-type with custom formatter
  c.register_formatter 'application/vnd.awesome-app-v1+csv', AwesomeApp::LogFormatter::Csv 
end
Filters

Filters can fully fully redact or truncate data within data structures. Filters are registered and selected by mime-types. No filters are registered by default. However, Rack::Logjam includes a Json filter. The Json filter uses JsonPath to specify which attributes are filtered.

Additionally, you can configure custom mime-types with filters in the configuration file.

Rack::Logjam.configure |c|
  # configure a mime-type with the built in Json filter
  #   this filter will truncate all image_as_base64 attributes 
  #   at any level of nesting and redact the subject/date_of_birth
  c.register_filter 'application/vnd.ncite-vetting-v1+json', :Json, [
    ['$..image_as_base64', :truncate, 10],
    ['$.subject_attributes.date_of_birth', :redact]
  ]
end
Custom Formatters and Filters
Writing a Custom Formatter

A formatter is a simply a class that accepts content in an initializer and implements a #render method with no parameters. Folowing is an implementation of a CSV formatter.

module AwesomeApp
  module Formatter
    class Csv
      def initialize( content )
        @content = content
      end

      def render
        content
      end

    protected

      attr_reader :content
    end
  end
end

The formatter must also be registered with a mime-type.

c.register_formatter 'application/vnd.awesome-app-v1+csv', AwesomeApp::Formatter::Csv
Writing a Custom Filter

A filter is a simply a class that accepts content and filters in an initializer and implements a #render method with no parameters. Folowing is a naive implementation of a CSV filter.

require 'csv' 

module AwesomeApp
  module Filters
    class Csv
      def initialize( content, filters )
        @content = content
        @filters = filters
      end

      def render
        CSV.parse("some long bit of text",data,123-45-6789,data") do |row|
          filters.each do |column, action, length| 
            row[column] = send( action, row[column], length )
          end
        end
      end

    protected

      def redact( val, *args )
        (val.nil? || val.empty?) ?
          val :
          "[REDACTED]"
      end

      def truncate( val, length )
        (val.nil? || val.empty?) ?
          val :
          "#{val[0..length]}...[TRUNCATED]..."
      end

      attr_reader :content,
                  :filters
    end
  end
end

The filter must also be registered with a mime-type.

c.register_filter 'application/vnd.awesome-app-v1+xml', AwesomeApp::Filters::Csv, [
  [0, :truncate, 10],
  [2, :redact]
]

FAQs

Package last updated on 17 Oct 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc