Skip to content

Commit

Permalink
Merge pull request #332 from interagent/quote-more-strings
Browse files Browse the repository at this point in the history
Ensure all strings with spaces are quoted in logs
  • Loading branch information
bensymonds committed Jan 30, 2020
2 parents 5285883 + 71ad622 commit 3a76af1
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

## [Unreleased]
### Changed
- Ensure all strings with spaces are quoted in logs. ([#332](https://github.com/interagent/pliny/pull/332))
- Allow ActiveSupport 5 or 6. ([#331](https://github.com/interagent/pliny/pull/331))

### Fixed
Expand Down
12 changes: 7 additions & 5 deletions lib/pliny/log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ def log_to_stream(stream, data, &block)
end

def quote_string(k, v)
# try to find a quote style that fits
if !v.include?('"')
%{#{k}="#{v}"}
elsif !v.include?("'")
Expand All @@ -140,19 +139,22 @@ def unparse(attrs)

def unparse_pair(k, v)
v = v.call if v.is_a?(Proc)
# only quote strings if they include whitespace

if v == nil
nil
elsif v == true
k
elsif v.is_a?(Float)
"#{k}=#{format("%.3f", v)}"
elsif v.is_a?(String) && v =~ /\s/
quote_string(k, v)
elsif v.is_a?(Time)
"#{k}=#{v.iso8601}"
else
"#{k}=#{v}"
v = "#{v}"
if v =~ /\s/
quote_string(k, v)
else
"#{k}=#{v}"
end
end
end
end
Expand Down
63 changes: 62 additions & 1 deletion spec/log_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
@io = StringIO.new
Pliny.stdout = @io
Pliny.stderr = @io
allow(@io).to receive(:print)
end

after do
Expand Down Expand Up @@ -130,4 +129,66 @@
end
end
end

describe "unparsing" do
it "removes nils from log" do
expect(@io).to receive(:print).with("\n")

Pliny.log(foo: nil)
end

it "leaves bare keys for true values" do
expect(@io).to receive(:print).with("foo\n")

Pliny.log(foo: true)
end

it "truncates floats" do
expect(@io).to receive(:print).with("foo=3.142\n")

Pliny.log(foo: Math::PI)
end

it "outputs times in ISO8601 format" do
expect(@io).to receive(:print).with("foo=2020-01-01T00:00:00Z\n")

Pliny.log(foo: Time.utc(2020))
end

it "quotes strings that contain spaces" do
expect(@io).to receive(:print).with("foo=\"string with spaces\"\n")

Pliny.log(foo: "string with spaces")
end

it "by default interpolates objects into strings" do
expect(@io).to receive(:print).with("foo=message\n")
expect(@io).to receive(:print).with("foo=42\n")
expect(@io).to receive(:print).with("foo=bar\n")

Pliny.log(foo: StandardError.new("message"))
Pliny.log(foo: 42)

klass = Class.new do
def to_s
"bar"
end
end
Pliny.log(foo: klass.new)
end

it "quotes strings that are generated from object interpolation" do
expect(@io).to receive(:print).with("foo=\"message with space\"\n")
expect(@io).to receive(:print).with("foo=\"bar with space\"\n")

Pliny.log(foo: StandardError.new("message with space"))

klass = Class.new do
def to_s
"bar with space"
end
end
Pliny.log(foo: klass.new)
end
end
end

0 comments on commit 3a76af1

Please sign in to comment.