ltsv_log_formatter 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3d4ed56403d40372bd8425a8bdd7a2eae1ef845d
4
- data.tar.gz: 0f27394f33bf4263789a4b2fc8bb0a7d53d881a6
3
+ metadata.gz: 9e184e787ef1f75e5af51681a1d84f7d796d017c
4
+ data.tar.gz: 1f9a3afe5bff72ef7f366814209c55857d2afda5
5
5
  SHA512:
6
- metadata.gz: 8dc326437d262858bc90cc562696d42993fbbee4c4f779e986c006c4fd1a3ca2b12b239e9924c0fed8bdaaf0c5f3968ffda709e8b1c4a00998fa743013bc4f88
7
- data.tar.gz: 53367800112c88b2a5aa46cbc27c03f89a554b68b7916cd1140b0d3e410a06b1a9bbb3fb469f560d332a87153d23b0afa9067126c9ae32fd10c0cdf4cabf1fe5
6
+ metadata.gz: ba24abdbdd5c94a3bbd64acb8e4db34648d66cef39802ae8c9c7d8e2f8d7eafd64b7972d35c91f2d80bd0bf03a5fdd79f685356f10889cc0a29c42f5134f0b72
7
+ data.tar.gz: d2a6ad4d752a905b3b0516aa5e88f70fadf0a950c13f0c42217d15f40a1ad95ff4ae25e95164c507db1d714c6dd8f03a01ab752c57cb36f1e2bb84c77bb0b659
@@ -1,3 +1,10 @@
1
+ # 0.0.2 (2017/01/07)
2
+
3
+ Enhancements:
4
+
5
+ * Escape "\t" character to "\\t"
6
+
1
7
  # 0.0.1 (2015/04/23)
2
8
 
3
9
  First version
10
+
data/README.md CHANGED
@@ -35,8 +35,8 @@ config.logger.formatter = LtsvLogFormatter.new
35
35
  Passing a hash parameter:
36
36
 
37
37
  ```
38
- irb> logger.info({foo: "bar"})
39
- time:20150423T00:00:00+09:00\tlevel:INFO\tfoo:bar
38
+ irb> logger.info({foo: "foo", bar: "bar"})
39
+ time:20150423T00:00:00+09:00\tlevel:INFO\tfoo:foo\tbar:bar
40
40
  ```
41
41
 
42
42
  Passing a string parameter: `message` key is used as default
@@ -46,13 +46,20 @@ irb> logger.info("foo")
46
46
  time:20150423T00:00:00+09:00\tlevel:INFO\tmessage:foo
47
47
  ```
48
48
 
49
- Line feed: Notice that the line feed character `\n` is converted into `\\n`
49
+ NOTE1: Notice that the line feed character `\n` is converted into `\\n` because LTSV format does not allow to break lines
50
50
 
51
51
  ```ruby
52
52
  irb> logger.info("foo\nbar")
53
53
  time:20150423T00:00:00+09:00\tlevel:INFO\tmessage:foo\\nbar
54
54
  ```
55
55
 
56
+ NOTE2: Notice that the tab character `\t` is converted into `\\t` because message might have a string as "\t<string>:<string>"
57
+
58
+ ```ruby
59
+ irb> logger.info("foo\tbar:baz")
60
+ time:20150423T00:00:00+09:00\tlevel:INFO\tmessage:foo\\tbar:baz
61
+ ```
62
+
56
63
  ## Options
57
64
 
58
65
  * time_key
@@ -28,10 +28,16 @@ class LtsvLogFormatter
28
28
  "#{@opts[:level_key]}:#{severity}\t" if @opts[:level_key]
29
29
  end
30
30
 
31
+ LF = "\n".freeze
32
+ TAB = "\t".freeze
33
+ ESCAPED_LF = "\\n".freeze
34
+ ESCAPED_TAB = "\\t".freeze
35
+ ESCAPE_TARGET = /[#{LF}#{TAB}]/
36
+
31
37
  def format_message(msg)
32
38
  unless msg.is_a?(Hash)
33
39
  msg = { @opts[:message_key] => msg }
34
40
  end
35
- msg.map {|k, v| "#{k}:#{v.to_s.gsub(/\n/, "\\n")}" }.join("\t")
41
+ msg.map {|k, v| "#{k}:#{v.to_s.gsub(ESCAPE_TARGET, LF => ESCAPED_LF, TAB => ESCAPED_TAB)}" }.join(TAB)
36
42
  end
37
43
  end
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |gem|
6
6
  gem.name = "ltsv_log_formatter"
7
- gem.version = "0.0.1"
7
+ gem.version = "0.0.2"
8
8
  gem.authors = ["Naotoshi Seo"]
9
9
  gem.email = ["sonots@gmail.com"]
10
10
  gem.description = %q{A logger formatter to output log in LTSV format}
@@ -135,4 +135,20 @@ describe LtsvLogFormatter do
135
135
  end
136
136
  end
137
137
  end
138
+
139
+ describe 'escape LF and TAB' do
140
+ context 'with LF' do
141
+ it do
142
+ logger.info(foo: "bar\nbar")
143
+ expect(gets).to eq "time:#{now}\tlevel:INFO\tfoo:bar\\nbar\n"
144
+ end
145
+ end
146
+
147
+ context 'with TAB' do
148
+ it do
149
+ logger.info(foo: "bar\tbar")
150
+ expect(gets).to eq "time:#{now}\tlevel:INFO\tfoo:bar\\tbar\n"
151
+ end
152
+ end
153
+ end
138
154
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ltsv_log_formatter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Naotoshi Seo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-24 00:00:00.000000000 Z
11
+ date: 2017-01-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A logger formatter to output log in LTSV format
14
14
  email:
@@ -47,7 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
47
47
  version: '0'
48
48
  requirements: []
49
49
  rubyforge_project:
50
- rubygems_version: 2.2.2
50
+ rubygems_version: 2.5.2
51
51
  signing_key:
52
52
  specification_version: 4
53
53
  summary: A logger formatter to output log in LTSV format