niboshi_json_formatter 0.1.0 → 0.1.1

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: 4f620bfa3ce8d8635ff3a49b7a5694bc1def3e47
4
- data.tar.gz: 4c43324b09180947fca0ca398e90ffdfd7def934
3
+ metadata.gz: c095ee3fd7f44484940febd058790be42827dab7
4
+ data.tar.gz: 6d5bfa98e4a883234ff28da56e8f2aab1fe5d691
5
5
  SHA512:
6
- metadata.gz: bd6dd60ca819f7302e79cb53a7a5da4d7ec67043fe09e115bbd35a9a63fdfbb1287b756a900c6c02a46de5da0442564b7b40d5465cbc0681446f072cc5aa6d8d
7
- data.tar.gz: 7245d14a967ea25ed8929cd4f9a7b22f2710034e1362fe1be84a3c1543d4813c5f1848180814c5207cadd3ee02d506636bbd5f7c765a610b8d896de0dd74d7b0
6
+ metadata.gz: 5b7b221ee9a318da48a2f5793823703ce190d1ae4c73cb7e591247e3ae503d57281b9680a4107364b1bed393c0c8214cc998976d09ef2bf89f04623cb4573deb
7
+ data.tar.gz: 8ae8870b7b29bda779ba42c03720893b641fde274d42c423e0908a93852855cf648ea4b47bd84c8f396ad61d3ee06dbc2841b7c5bceff6b8a974f4732329ca10
data/.gitignore CHANGED
@@ -12,3 +12,4 @@
12
12
  *.o
13
13
  *.a
14
14
  mkmf.log
15
+ *.gem
@@ -0,0 +1,25 @@
1
+ ---
2
+ language: ruby
3
+
4
+ gemfile:
5
+ - Gemfile
6
+ - Gemfile19-20
7
+
8
+ rvm:
9
+ - 1.9.3
10
+ - 2.0
11
+ - 2.1
12
+ - 2.2
13
+
14
+ script: bundle exec rspec
15
+
16
+ matrix:
17
+ exclude:
18
+ - rvm: 1.9.3
19
+ gemfile: Gemfile
20
+ - rvm: 2.0
21
+ gemfile: Gemfile
22
+ - rvm: 2.1
23
+ gemfile: Gemfile19-20
24
+ - rvm: 2.2
25
+ gemfile: Gemfile19-20
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in niboshi_logger_json_formatter.gemspec
4
+ gemspec
5
+ gem 'string-scrub'
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # NiboshiJsonFormatter
2
2
 
3
+ [![Circle CI](https://circleci.com/gh/ZCloud-Firstserver/niboshi_json_formatter/tree/master.svg?style=svg)](https://circleci.com/gh/ZCloud-Firstserver/niboshi_json_formatter/tree/master)
4
+
3
5
  A Ruby gem that makes your app log in JSON format. It may be useful if you ship all your logs to [ElasticSearch](http://www.elasticsearch.org/).
4
6
 
5
7
  ## Installation
@@ -7,7 +9,7 @@ A Ruby gem that makes your app log in JSON format. It may be useful if you ship
7
9
  Add this line to your application's Gemfile:
8
10
 
9
11
  ```ruby
10
- gem 'niboshi_json_formatter', github: "ZCloud-Firstserver/niboshi_json_formatter"
12
+ gem 'niboshi_json_formatter'
11
13
  ```
12
14
 
13
15
  And then execute:
@@ -20,11 +22,25 @@ Or install it yourself as:
20
22
 
21
23
  ## Usage
22
24
 
23
- Add this to your `config/environments/*.rb` files:
25
+ ### Rails project
26
+
27
+ Add this to your `config/environments/*.rb`:
24
28
 
25
29
  ```
26
30
  config.log_formatter = Niboshi::JsonFormatter.new
27
31
  ```
32
+ ### Logger
33
+
34
+ Update the formatter:
35
+
36
+ ```
37
+ require 'niboshi_json_formatter'
38
+ @log = Logger.new(STDOUT)
39
+ @log.formatter = Niboshi::JsonFormatter.new
40
+ @log.info "foo bar"
41
+
42
+ #=> {"time":1417595700.318135,"formatted_time":"2014-12-03 17:35:00 +0900","hostname":"macmini.local","pid":66419,"tid":70165887050860,"level":"INFO","program_name":null,"message":"foo bar"}
43
+ ```
28
44
 
29
45
  ## Contributing
30
46
 
@@ -0,0 +1,7 @@
1
+ ---
2
+ dependencies:
3
+ pre:
4
+ - gem install bundler
5
+ test:
6
+ override:
7
+ - bundle exec rspec
@@ -3,7 +3,7 @@ require "json"
3
3
 
4
4
  module Niboshi
5
5
  class JsonFormatter < ::Logger::Formatter
6
- Version = VERSION = "0.1.0"
6
+ Version = VERSION = "0.1.1"
7
7
 
8
8
  def hostname
9
9
  @hostname ||= Socket.gethostname
@@ -14,7 +14,7 @@ module Niboshi
14
14
  end
15
15
 
16
16
  def call(severity, time, program_name, message)
17
- {
17
+ data = {
18
18
  time: time.to_f,
19
19
  formatted_time: time,
20
20
  hostname: hostname,
@@ -23,7 +23,12 @@ module Niboshi
23
23
  level: severity,
24
24
  program_name: program_name,
25
25
  message: message
26
- }.to_json + "\n"
26
+ }
27
+
28
+ data.to_json + "\n"
29
+
30
+ rescue JSON::GeneratorError
31
+ (data.each_pair {|k,v| data[k] = v.scrub("?") if v.is_a?(String) }).to_json + "\n"
27
32
  end
28
33
  end
29
34
  end
@@ -1,3 +1,4 @@
1
+ # coding: utf-8
1
2
  require 'spec_helper'
2
3
 
3
4
  describe Niboshi::JsonFormatter do
@@ -17,4 +18,28 @@ describe Niboshi::JsonFormatter do
17
18
  it { expect(subject["program_name"]).to eql program_name }
18
19
  it { expect(subject["message"]).to eql message }
19
20
  end
21
+
22
+ context "cantain illegal utf-8 string" do
23
+ describe "fallback into scrub" do
24
+ let(:severity) { "INFO" }
25
+ let(:time) { Time.now }
26
+ let(:program_name) { "RSpec" }
27
+ let(:message) { "あいうえお".encode("SHIFT_JIS", "UTF-8").force_encoding("UTF-8") }
28
+ subject { JSON.parse(Niboshi::JsonFormatter.new.call(severity, time, program_name, message)) }
29
+
30
+ it { expect(subject["time"]).not_to be_nil }
31
+ it { expect(subject["formatted_time"]).not_to be_nil }
32
+ it { expect(subject["hostname"]).to eql Socket.gethostname }
33
+ it { expect(subject["pid"]).not_to be_nil }
34
+ it { expect(subject["tid"]).not_to be_nil }
35
+ it { expect(subject["level"]).to eql severity }
36
+ it { expect(subject["program_name"]).to eql program_name }
37
+
38
+ if RbConfig::CONFIG['ruby_version'] < "2.1.0"
39
+ it { expect(subject["message"]).to eql "\x82\xA0\x82\xA2\x82\xA4\x82\xA6\x82\xA8" }
40
+ else
41
+ it { expect(subject["message"]).to eql "??????????" }
42
+ end
43
+ end
44
+ end
20
45
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: niboshi_json_formatter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - azukiwasher
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-05 00:00:00.000000000 Z
11
+ date: 2015-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -61,11 +61,13 @@ extra_rdoc_files: []
61
61
  files:
62
62
  - ".gitignore"
63
63
  - ".rspec"
64
+ - ".travis.yml"
64
65
  - Gemfile
66
+ - Gemfile19-20
65
67
  - LICENSE
66
- - LICENSE.txt
67
68
  - README.md
68
69
  - Rakefile
70
+ - circle.yml
69
71
  - lib/niboshi/json_formatter.rb
70
72
  - lib/niboshi_json_formatter.rb
71
73
  - niboshi_json_formatter.gemspec
@@ -1,22 +0,0 @@
1
- Copyright (c) 2014 azukiwasher
2
-
3
- MIT License
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.