Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #1 #2

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# RubyMine project files
.idea

*.gem
*.rbc
.bundle
Expand Down
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Include the middleware in your API classes with the `use` statement

Require the middleware in `config/application.rb`

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

### Configuration

Expand Down
1 change: 1 addition & 0 deletions lib/rack/logjam/filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Logjam
module Filters

autoload :Base, 'rack/logjam/filters/base'
autoload :FormUrlencoded, 'rack/logjam/filters/form_urlencoded'
autoload :Json, 'rack/logjam/filters/json'
autoload :Nil, 'rack/logjam/filters/nil'

Expand Down
71 changes: 71 additions & 0 deletions lib/rack/logjam/filters/form_urlencoded.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require 'jsonpath'

module Rack
module Logjam
module Filters
class FormUrlencoded < Rack::Logjam::Filters::Base

def render
return content if blank_content?
apply_filters
query_hash.to_query
end

protected

def blank_content?
content.nil? || content.strip.empty?
end

def apply_filters
filters.each do |param, action, length|
if value = query_hash[param]
if value.is_a?(Array)
query_hash[param] = value.map { |array_item| filter(array_item, action, length) }
else
query_hash[param] = filter(value, action, length)
end
end
end
end

def filter(value, action, length)
method_name = "#{action}_#{value.class.name.downcase}"
if respond_to?( method_name, true )
send( method_name, value, length )
else
"[#{action.upcase}ED: #{value.class.name}]"
end
end

def redact_nilclass( val, *args )
nil
end

def truncate_nilclass( val, length )
nil
end

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

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

def query_hash
# This gives a weird response where every value is an array, even when there's no key
# @query_hash ||= CGI.parse(content)

@query_hash ||= Rack::Utils.parse_nested_query(content)
end

end
end
end
end
14 changes: 7 additions & 7 deletions lib/rack/logjam/grape/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ module Grape
class Middleware < ::Grape::Middleware::Base

def before
return unless api_request?( env )

logger.log_request( env )
end

def after
return unless api_request?( env )

status = @app_response.first
headers = @app_response[1]
body = @app_response.last.body.last
if @app_response.nil?
::Rack::Logjam::logger.info "@app_response is nil. WTF Grape? https://github.com/ruby-grape/grape/issues/1265"
return
end
status = @app_response.status
headers = @app_response.header
body = @app_response.body.last

#logger.log_response( env, status, headers, response )
logger.log_response( env, status, headers, body )
Expand Down
81 changes: 37 additions & 44 deletions lib/rack/logjam/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,82 +5,75 @@ module Rack
module Logjam
class Logger

def log_request( env )
_logger.info <<-end_info
[#{ANSI.green { 'api' }}] #{ANSI.cyan { '--- Request Env ---' }}
#{ANSI.magenta { JSON.pretty_generate( request_log_data( env )) }}
[#{ANSI.green { 'api' }}] #{ANSI.cyan { '--- Request Body ---' }}
#{ANSI.cyan { formatted_request_body( env ) }}
end_info
def log_request(env)
_logger.info "HTTP Request: #{ANSI.magenta { request_log_data(env).inspect }}"
formatted_body = formatted_request_body(env)
_logger.info "HTTP Request Body: #{ANSI.cyan { formatted_body }}" if formatted_body
end

def log_response( env, status, headers, response )
_logger.info <<-end_info
[#{ANSI.green { 'api' }}] #{ANSI.cyan { '--- Response ---' }}
Status: #{status}
Headers: #{headers.inspect}
Body:
#{ANSI.cyan { format_body( (response.body rescue response), accept( env ), env ) }}
end_info
def log_response(env, status, headers, response)
_logger.info "HTTP Response: #{status} #{headers.inspect}"
formatted_body = format_body((response.body rescue response), accept(env), env)
_logger.info "HTTP Response Body: #{ANSI.cyan { formatted_body }}" if formatted_body
end

protected
protected

def request_log_data( env )
def request_log_data(env)
request_data = {
content_type: content_type( env ),
content_length: env['CONTENT_LENGTH'],
accept: accept( env ),
accept_version: env['HTTP_ACCEPT_VERSION'],
method: env['REQUEST_METHOD'],
path: path_info( env ),
query: query( env )
content_type: content_type(env),
content_length: env['CONTENT_LENGTH'],
accept: accept(env),
accept_version: env['HTTP_ACCEPT_VERSION'],
method: env['REQUEST_METHOD'],
path: path_info(env),
query: query(env)
}
#request_data[:user_id] = current_user.id if current_user
request_data
end

def content_type( env )
def content_type(env)
env['CONTENT_TYPE']
end

def accept( env )
def accept(env)
env['HTTP_ACCEPT']
end

def path_info( env )
def path_info(env)
env['PATH_INFO']
end

def query( env )
URI.unescape( env['QUERY_STRING'] )
def query(env)
URI.unescape(env['QUERY_STRING'])
end

def formatted_request_body( env )
format_body( rack_input_content( env ), content_type( env ), env )
def formatted_request_body(env)
format_body(rack_input_content(env), content_type(env), env)
end

def format_body( body, format, env )
filter = fetch_filter( format, body )
def format_body(body, format, env)
return if body.nil? || body.strip.empty?
return 'Body too large to log' if body.length > 4096
filter = fetch_filter(format, body)
filtered = filter.render

formatter = fetch_formatter( format, body ).new( filtered, format )
formatter = fetch_formatter(format, body).new(filtered, format)
formatter.render
end

def fetch_filter( format, body )
klass, filters = Rack::Logjam::Filters.get( format )
klass.new( body, filters )
def fetch_filter(format, body)
klass, filters = Rack::Logjam::Filters.get(format)
klass.new(body, filters)
end

def fetch_formatter( format, body )
return Rack::Logjam::Formatters::Empty if body.nil? || body.strip.empty?
return Rack::Logjam::Formatters::Array if body.is_a?( Array )
Rack::Logjam::Formatters.get( format )
def fetch_formatter(format, body)
return Rack::Logjam::Formatters::Array if body.is_a?(Array)
Rack::Logjam::Formatters.get(format) unless body.nil? || body.strip.empty?
end

def rack_input_content( env )
( rack_input = env['rack.input'] ).read.tap do |content|
def rack_input_content(env)
(rack_input = env['rack.input']).read.tap do |content|
rack_input.rewind
end
end
Expand Down
18 changes: 9 additions & 9 deletions lib/rack/logjam/rails/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ def initialize( app )
end

def call( env )
before env

app.call( env ).tap do |rack_response|
after env, *rack_response
if api_request?(env)
app.call(env)
else
before env

app.call( env ).tap do |rack_response|
after env, *rack_response
end
end
end

Expand All @@ -23,19 +27,15 @@ def call( env )
attr_reader :app

def before( env )
return unless api_request?( env )

logger.log_request( env )
end

def after( env, status, headers, response )
return unless api_request?( env )

logger.log_response( env, status, headers, response )
end

def api_request?( env )
path_info( env ) =~ /^\/api\//
!(path_info( env ) =~ /^\/api\//).nil?
end

def path_info( env )
Expand Down
2 changes: 1 addition & 1 deletion lib/rack/logjam/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Rack

module Logjam

VERSION = '0.2.1'
VERSION = '0.2.1.1'

end

Expand Down