Skip to content

Commit

Permalink
Graylog adaptation
Browse files Browse the repository at this point in the history
  • Loading branch information
Meat-Chopper committed Nov 12, 2019
1 parent 0b664f5 commit 935f294
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 33 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ HttpLog.configure do |config|
# You can also log in JSON format
config.json_log = false

# For Graylog you can set this to `true`
config.graylog = false

# Prettify the output - see below
config.color = false

Expand Down Expand Up @@ -118,6 +121,17 @@ HttpLog.configure do |config|
end
```

If you use Graylog and want to use its search features such as "rounded_benchmark:>1 AND method:PUT",
you can use this configuration:

```ruby
HttpLog.configure do |config|
config.logger_method = :add
config.severity = GELF::Levels::DEBUG
config.graylog = true
end
```

For more color options please refer to the [rainbow documentation](https://github.com/sickill/rainbow)

### Compact logging
Expand Down
2 changes: 2 additions & 0 deletions lib/httplog/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Configuration
attr_accessor :enabled,
:compact_log,
:json_log,
:graylog,
:logger,
:logger_method,
:severity,
Expand All @@ -28,6 +29,7 @@ def initialize
@enabled = true
@compact_log = false
@json_log = false
@graylog = false
@logger = Logger.new($stdout)
@logger_method = :log
@severity = Logger::Severity::DEBUG
Expand Down
80 changes: 47 additions & 33 deletions lib/httplog/http_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def configure
def call(options = {})
if config.json_log
log_json(options)
elsif config.graylog
log_graylog(options)
elsif config.compact_log
log_compact(options[:method], options[:url], options[:response_code], options[:benchmark])
else
Expand Down Expand Up @@ -111,7 +113,7 @@ def parse_body(body, encoding, content_type)

if body.is_a?(Net::ReadAdapter)
# open-uri wraps the response in a Net::ReadAdapter that defers reading
# the content, so the reponse body is not available here.
# the content, so the response body is not available here.
raise BodyParsingError, '(not available yet)'
end

Expand Down Expand Up @@ -147,38 +149,6 @@ def log_compact(method, uri, status, seconds)
log("#{method.to_s.upcase} #{masked(uri)} completed with status code #{status} in #{seconds.to_f.round(6)} seconds")
end

def log_json(data = {})
return unless config.json_log

data[:response_code] = transform_response_code(data[:response_code]) if data[:response_code].is_a?(Symbol)

parsed_body = begin
parse_body(data[:response_body], data[:encoding], data[:content_type])
rescue BodyParsingError => e
e.message
end

if config.compact_log
log({
method: data[:method].to_s.upcase,
url: masked(data[:url]),
response_code: data[:response_code].to_i,
benchmark: data[:benchmark]
}.to_json)
else
log({
method: data[:method].to_s.upcase,
url: masked(data[:url]),
request_body: masked(data[:request_body]),
request_headers: masked(data[:request_headers].to_h),
response_code: data[:response_code].to_i,
response_body: parsed_body,
response_headers: data[:response_headers].to_h,
benchmark: data[:benchmark]
}.to_json)
end
end

def transform_response_code(response_code_name)
Rack::Utils::HTTP_STATUS_CODES.detect { |_k, v| v.to_s.casecmp(response_code_name.to_s).zero? }.first
end
Expand All @@ -199,6 +169,50 @@ def colorize(msg)

private

def log_json(data = {})
return unless config.json_log

log(json_payload(data).to_json)
end

def log_graylog(data = {})
result = json_payload(data)

result[:rounded_benchmark] = data[:benchmark].round
result[:short_message] = result.delete(:url)
config.logger.public_send(config.logger_method, config.severity, result)
end

def json_payload(data = {})
data[:response_code] = transform_response_code(data[:response_code]) if data[:response_code].is_a?(Symbol)

parsed_body = begin
parse_body(data[:response_body], data[:encoding], data[:content_type])
rescue BodyParsingError => e
e.message
end

if config.compact_log
{
method: data[:method].to_s.upcase,
url: masked(data[:url]),
response_code: data[:response_code].to_i,
benchmark: data[:benchmark]
}
else
{
method: data[:method].to_s.upcase,
url: masked(data[:url]),
request_body: masked(data[:request_body]),
request_headers: masked(data[:request_headers].to_h),
response_code: data[:response_code].to_i,
response_body: parsed_body,
response_headers: data[:response_headers].to_h,
benchmark: data[:benchmark]
}
end
end

def masked(msg, key=nil)
return msg if config.filter_parameters.empty?
return msg if msg.nil?
Expand Down

0 comments on commit 935f294

Please sign in to comment.