Skip to content

Commit

Permalink
Rework Graylog support. Allow different Graylog log formats.
Browse files Browse the repository at this point in the history
  • Loading branch information
Meat-Chopper committed Jan 16, 2020
1 parent 3a9afe5 commit 34eb49a
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 23 deletions.
28 changes: 23 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,37 @@ HttpLog.configure do |config|
end
```

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

### Graylog logging

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

```ruby
FORMATTER = Lograge::Formatters::KeyValue.new

HttpLog.configure do |config|
config.logger = <your GELF::Logger>
config.logger_method = :add
config.severity = GELF::Levels::DEBUG
config.graylog = true
config.logger = <your GELF::Logger>
config.logger_method = :add
config.severity = GELF::Levels::DEBUG
config.graylog_formatter = FORMATTER
end
```

For more color options please refer to the [rainbow documentation](https://github.com/sickill/rainbow)
You also can use GELF Graylog format this way:

```ruby
class Lograge::Formatters::Graylog2HttpLog < Lograge::Formatters::Graylog2
def short_message data
data.is_a?(Hash) ? "[httplog] [#{data[:response_code]}] #{data[:method]} #{data[:url]}" : data
end
end

FORMATTER = Lograge::Formatters::Graylog2HttpLog.new
```

Or define your own class that implements the `call` method

### Compact logging

Expand Down
4 changes: 2 additions & 2 deletions lib/httplog/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Configuration
attr_accessor :enabled,
:compact_log,
:json_log,
:graylog,
:graylog_formatter,
:logger,
:logger_method,
:severity,
Expand All @@ -31,7 +31,7 @@ def initialize
@enabled = true
@compact_log = false
@json_log = false
@graylog = false
@graylog_formatter = nil
@logger = Logger.new($stdout)
@logger_method = :log
@severity = Logger::Severity::DEBUG
Expand Down
7 changes: 3 additions & 4 deletions lib/httplog/http_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def call(options = {})
parse_request(options)
if config.json_log
log_json(options)
elsif config.graylog
elsif config.graylog_formatter
log_graylog(options)
elsif config.compact_log
log_compact(options[:method], options[:url], options[:response_code], options[:benchmark])
Expand Down Expand Up @@ -205,8 +205,6 @@ def dump_json(data)

def log_graylog(data)
result = json_payload(data)

result[:short_message] = result.delete(:url)
begin
send_to_graylog result
rescue
Expand All @@ -217,7 +215,8 @@ def log_graylog(data)
end

def send_to_graylog data
config.logger.public_send(config.logger_method, config.severity, data)
data.compact!
config.logger.public_send(config.logger_method, config.severity, config.graylog_formatter.call(data))
end

def json_payload(data = {})
Expand Down
24 changes: 12 additions & 12 deletions spec/lib/http_log_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
describe HttpLog do
subject { log } # see spec_helper

let(:secret) { 'my secret' }
let(:host) { 'localhost' }
let(:port) { 9292 }
let(:path) { '/index.html' }
let(:headers) { { 'accept' => '*/*', 'foo' => secret } }
let(:data) { "foo=#{secret}&bar=foo" }
let(:params) { { 'foo' => secret, 'bar' => 'foo:form-data' } }
let(:html) { File.read('./spec/support/index.html') }
let(:json) { JSON.parse(log.match(/\[httplog\]\s(.*)/).captures.first) }
let(:secret) { 'my secret' }
let(:host) { 'localhost' }
let(:port) { 9292 }
let(:path) { '/index.html' }
let(:headers) { { 'accept' => '*/*', 'foo' => secret } }
let(:data) { "foo=#{secret}&bar=foo" }
let(:params) { { 'foo' => secret, 'bar' => 'foo:form-data' } }
let(:html) { File.read('./spec/support/index.html') }
let(:json) { JSON.parse(log.match(/\[httplog\]\s(.*)/).captures.first) }
let(:gray_log) { JSON.parse("{#{log.match(/{(.*)/).captures.first}") }

# Default configuration
Expand All @@ -31,7 +31,7 @@
let(:prefix_response_lines) { HttpLog.configuration.prefix_response_lines }
let(:prefix_line_numbers) { HttpLog.configuration.prefix_line_numbers }
let(:json_log) { HttpLog.configuration.json_log }
let(:graylog) { HttpLog.configuration.graylog }
let(:graylog_formatter) { HttpLog.configuration.graylog_formatter }
let(:compact_log) { HttpLog.configuration.compact_log }
let(:url_blacklist_pattern) { HttpLog.configuration.url_blacklist_pattern }
let(:url_whitelist_pattern) { HttpLog.configuration.url_whitelist_pattern }
Expand All @@ -55,7 +55,7 @@ def configure
c.prefix_response_lines = prefix_response_lines
c.prefix_line_numbers = prefix_line_numbers
c.json_log = json_log
c.graylog = graylog
c.graylog_formatter = graylog_formatter
c.compact_log = compact_log
c.url_blacklist_pattern = url_blacklist_pattern
c.url_whitelist_pattern = url_whitelist_pattern
Expand Down Expand Up @@ -299,7 +299,7 @@ def configure
end

context 'with Graylog config' do
let(:graylog) { true }
let(:graylog_formatter) { Formatter.new }
let(:logger) { GelfMock.new @log }

it_behaves_like 'logs JSON', adapter_class, true
Expand Down
5 changes: 5 additions & 0 deletions spec/loggers/formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Formatter
def call(data)
data
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

require 'httplog'

require 'loggers/formatter'
require 'loggers/gelf_mock'
require 'adapters/http_base_adapter'
Dir[File.dirname(__FILE__) + '/adapters/*.rb'].sort.each { |f| require f }
Expand Down

0 comments on commit 34eb49a

Please sign in to comment.