Skip to content

Commit

Permalink
Return nil from formatters' #call method if the logstash event ha…
Browse files Browse the repository at this point in the history
…s been cancelled

- Moved the responsibility of returning the formatted message to the
  base `LogStashLogger::Formatter::Base` class, since it is there where
  the event is assembled and possibly cancelled by the user.
- Added a new private `#format_event` method that receives an event and
  returns the object to be logged to the device. This method may be
  overriden by the derived formatter classes.
- Public interface should be backwards compatible for non-cancelled
  events.
- TODO: Do all supported devices gracefully handle a `nil` response
  from the formatters?

Resolves dwbutler#133
  • Loading branch information
gdeoliveira committed Aug 21, 2017
1 parent a40fbb5 commit 9b075e8
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 27 deletions.
11 changes: 8 additions & 3 deletions lib/logstash-logger/formatter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ def initialize(customize_event: nil)
super()
end

def call(severity, time, progname, message)
@event = build_event(message, severity, time)
def call(severity, time, _progname, message)
event = build_event(message, severity, time)
format_event(event) unless event.cancelled?
end

protected
private

def build_event(message, severity, time)
data = message
Expand Down Expand Up @@ -63,6 +64,10 @@ def build_event(message, severity, time)

event
end

def format_event(event)
event
end
end
end
end
7 changes: 4 additions & 3 deletions lib/logstash-logger/formatter/cee.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module LogStashLogger
module Formatter
class Cee < Base
def call(severity, time, progname, message)
super
"@cee:#{@event.to_json}"
private

def format_event(event)
"@cee:#{event.to_json}"
end
end
end
Expand Down
18 changes: 10 additions & 8 deletions lib/logstash-logger/formatter/cee_syslog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ module LogStashLogger
module Formatter
class CeeSyslog < Cee
def call(severity, time, progname, message)
@cee = super
@progname = progname

"#{facility}:#{@cee}\n"
super
end

protected
private

def build_facility(host)
facility = host.dup
facility << " #{@progname}" if @progname
facility
end

def facility
@facility = "#{@event['host']}"
@facility << " #{@progname}" if @progname
@facility
def format_event(event)
"#{build_facility(event["host".freeze])}:#{super}\n"
end
end
end
Expand Down
7 changes: 4 additions & 3 deletions lib/logstash-logger/formatter/json.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module LogStashLogger
module Formatter
class Json < Base
def call(severity, time, progname, message)
super
@event.to_json
private

def format_event(event)
event.to_json
end
end
end
Expand Down
7 changes: 4 additions & 3 deletions lib/logstash-logger/formatter/json_lines.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module LogStashLogger
module Formatter
class JsonLines < Base
def call(severity, time, progname, message)
super
"#{@event.to_json}\n"
private

def format_event(event)
"#{event.to_json}\n"
end
end
end
Expand Down
3 changes: 0 additions & 3 deletions lib/logstash-logger/formatter/logstash_event.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
module LogStashLogger
module Formatter
class LogStashEvent < Base
def call(severity, time, progname, message)
super
end
end
end
end
22 changes: 22 additions & 0 deletions spec/formatter/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,28 @@
describe LogStashLogger::Formatter::Base do
include_context "formatter"

describe "#call" do
context "when event is not cancelled" do
it "returns a formatted message" do
expect(subject).to receive(:format_event).once.with(instance_of(LogStash::Event)).and_call_original
expect(subject.call(severity, time, progname, message)).to be_a(LogStash::Event)
end
end

context "when event is cancelled" do
before(:each) do
LogStashLogger.configure do |config|
config.customize_event(&:cancel)
end
end

it "returns `nil`" do
expect(subject).not_to receive(:format_event)
expect(subject.call(severity, time, progname, message)).to be_nil
end
end
end

describe "#build_event" do
let(:event) { formatted_message }

Expand Down
8 changes: 4 additions & 4 deletions spec/formatter/cee_syslog_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let(:facility) { "facility" }

before do
allow(subject).to receive(:facility).and_return(facility)
allow(subject).to receive(:build_facility).and_return(facility)
end

it "outputs a facility before the @cee" do
Expand All @@ -21,22 +21,22 @@
end
end

describe "#facility" do
describe "#build_facility" do
let(:host) { Socket.gethostname }

before do
formatted_message
end

it "includes hostname and progname" do
expect(subject.send(:facility)).to match(/\A#{host}\s#{progname}\z/)
expect(subject.send(:build_facility, host)).to match(/\A#{host}\s#{progname}\z/)
end

context "without progname" do
let(:progname) { nil }

it "only includes hostname" do
expect(subject.send(:facility)).to match(/\A#{host}\z/)
expect(subject.send(:build_facility, host)).to match(/\A#{host}\z/)
end
end
end
Expand Down

0 comments on commit 9b075e8

Please sign in to comment.