Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module Lograge | ||
module Formatters | ||
class LTSV | ||
def call(data) | ||
fields = fields_to_display(data) | ||
|
||
event = fields.map { |key| format(key, data[key]) } | ||
event.join('\t') | ||
end | ||
|
||
def fields_to_display(data) | ||
data.keys | ||
end | ||
|
||
def format(key, value) | ||
if key == :error | ||
# Exactly preserve the previous output | ||
# Parsing this can be ambigious if the error messages contains | ||
# a single quote | ||
value = "'#{escape value}'" | ||
else | ||
# Ensure that we always have exactly two decimals | ||
value = Kernel.format('%.2f', value) if value.is_a? Float | ||
end | ||
|
||
"#{key}:#{value}" | ||
end | ||
|
||
private | ||
|
||
def escape(string) | ||
value = string.is_a?(String) ? string.dup : string.to_s | ||
|
||
value.gsub!('\\', '\\\\') | ||
value.gsub!('\n', '\\n') | ||
value.gsub!('\r', '\\r') | ||
value.gsub!('\t', '\\t') | ||
|
||
value | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require 'lograge' | ||
|
||
describe Lograge::Formatters::LTSV do | ||
let(:payload) do | ||
{ | ||
custom: 'data', | ||
status: 200, | ||
method: 'GET', | ||
path: '/', | ||
controller: 'welcome', | ||
action: 'index', | ||
will_escaped: '\t' | ||
} | ||
end | ||
|
||
subject { described_class.new.call(payload) } | ||
|
||
it "includes the 'controller' key:value" do | ||
expect(subject).to include('controller:welcome') | ||
end | ||
|
||
it "includes the 'action' key:value" do | ||
expect(subject).to include('action:index') | ||
end | ||
|
||
it 'escapes escape sequences as value' do | ||
expect(subject).to include('will_escaped:\\t') | ||
end | ||
end |