simple_json_log_formatter 0.1.0 → 0.2.0

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: bae50427131ba2bc0fba6d8cbefdfc55079824aa
4
- data.tar.gz: 7c14c6b45a817d1340c58178398f36a5a6cbbe9e
3
+ metadata.gz: 08c34b6646691ead6f6b302670589ad504a4c4cf
4
+ data.tar.gz: 843a9a6a6982d7b856b3dab5cb509e4dee8834bc
5
5
  SHA512:
6
- metadata.gz: c7536be431dabf0856966b04ca18fed86bcd828747ceed1b2c0ef0da983621aaada5576f32c1f570a32db800bff7bcc1d206c883eb65a5ea466892a6b22febf6
7
- data.tar.gz: c2fbf635daaffdafe520c098e4b6e5b0c4f5faa20f62be8a76547c9abbfa1a2f27ef110cc0cd902501811030527dcce3f7919914bf04843525338f3fcd85d99a
6
+ metadata.gz: 55801d55d135b6dc21ec4e9f4d33f24ba163bac9324759f0604b4b6c1c1e13c717826b8100a359e8bbac4217c8376a23822b345635c8f03e31a1a189155c410b
7
+ data.tar.gz: fad61c69945f8739a114f3a2bf1509e95d3b807587ceca81cfba9b5e885001c1747f52831e1b5b41b82b146a551a939fcbe2cbf4822daa2c42beee458fe2235a
data/.gitignore CHANGED
@@ -1,11 +1,13 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
1
+ *.gem
2
+ .bundle/
3
+ .yardoc
4
+ _yardoc/
5
+ coverage/
6
+ doc/
7
+ log/
8
+ pkg/
9
+ spec/reports/
10
+ tmp/
9
11
  Gemfile.lock
10
12
 
11
13
  # rspec failure tracking
data/LICENSE.txt CHANGED
@@ -1,6 +1,7 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2018 taogawa
3
+ Copyright (c) 2018 Takeshi Ogawa
4
+ Copyright (c) 2015 Naotoshi Seo
4
5
 
5
6
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
7
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # SimpleJsonLogFormatter
2
2
 
3
+ Json formatter for Ruby logger.
4
+
5
+ This project is based on [LtsvLogFormatter](https://github.com/sonots/ltsv_log_formatter) and modified for json.
6
+
3
7
  ## Installation
4
8
 
5
9
  Add this line to your application's Gemfile:
@@ -18,7 +22,40 @@ Or install it yourself as:
18
22
 
19
23
  ## Usage
20
24
 
21
- TODO: Write usage instructions here
25
+ ```ruby
26
+ require 'logger'
27
+ require 'simple_json_log_formatter'
28
+ logger = Logger.new
29
+ logger.formatter = SimpleJsonLogFormatter.new
30
+ ```
31
+
32
+ ### Rails
33
+
34
+ Configure at `config/application.rb` or `config/environments/*.rb`
35
+
36
+ ```ruby
37
+ # config/application.rb
38
+ # OR
39
+ # config/environments/*.rb
40
+ config.log_formatter = SimpleJsonLogFormatter.new
41
+ ```
42
+
43
+ ### Options
44
+
45
+ - time_key
46
+ - Change the key name of the time field. Set nil to remove. Default: time
47
+ - severity_key
48
+ - Change the key name of the severity field. Set nil to remove. Default: severity
49
+ - progname_key
50
+ - Change the key name of the progname field. Set nil to remove. Default: progname
51
+ - message_key
52
+ - Change the key name for the message field. Default: message
53
+ - datetime_format
54
+ - Change date and time format of the time field. Default: `%FT%T%:z`
55
+
56
+ ```ruby
57
+ logger.formatter = SimpleJsonLogFormatter.new(time_key: "log_time", progname_key: nil)
58
+ ```
22
59
 
23
60
  ## Development
24
61
 
@@ -28,7 +65,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
28
65
 
29
66
  ## Contributing
30
67
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/simple_json_log_formatter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
68
+ Bug reports and pull requests are welcome on GitHub at https://github.com/taogawa/simple_json_log_formatter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
32
69
 
33
70
  ## License
34
71
 
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
  require "simple_json_log_formatter/version"
3
- require "logger"
4
3
  require "json"
5
4
 
6
5
  class SimpleJsonLogFormatter
@@ -10,6 +9,7 @@ class SimpleJsonLogFormatter
10
9
  # @param [Hash] opts
11
10
  # @option opts [String] time_key (default: time)
12
11
  # @option opts [String] severity_key (default: severity)
12
+ # @option opts [String] progname_key (default: progname)
13
13
  # @option opts [String] message_key (default: message)
14
14
  # @option opts [String] datetime_format (default: nil)
15
15
  def initialize(opts={})
@@ -18,31 +18,23 @@ class SimpleJsonLogFormatter
18
18
  @opts[:severity_key] = :severity unless @opts.has_key?(:severity_key)
19
19
  @opts[:progname_key] = :progname unless @opts.has_key?(:progname_key)
20
20
  @opts[:message_key] ||= :message
21
- @opts[:datetime_format] = @opts.dig(:datetime_format)
21
+ @opts[:datetime_format] = "%FT%T%:z" unless @opts.has_key?(:datetime_format)
22
22
  end
23
23
 
24
24
  def call(severity, time, progname, msg)
25
- log = [*format_time(time), *format_severity(severity), *format_progname(progname), *format_message(msg)].to_h
26
- "#{log.to_json}\n"
25
+ event = {}
26
+ event[@opts[:time_key]] = time.strftime(@opts[:datetime_format]) if @opts[:time_key]
27
+ event[@opts[:severity_key]] = severity if @opts[:severity_key]
28
+ event[@opts[:progname_key]] = progname if @opts[:progname_key]
29
+ event[@opts[:message_key]] = format_message(msg)
30
+ "#{event.to_json}\n"
27
31
  end
28
32
 
29
33
  private
30
- def format_time(time)
31
- { @opts[:time_key] => time.strftime(@opts.dig(:datetime_format) || "%FT%T%:z") } if @opts[:time_key]
32
- end
33
-
34
- def format_severity(severity)
35
- { @opts[:severity_key] => severity } if @opts[:severity_key]
36
- end
37
-
38
- def format_progname(progname)
39
- { @opts[:progname_key] => progname } if @opts[:progname_key]
40
- end
41
-
42
34
  def format_message(msg)
43
35
  if msg.is_a?(String) && msg.start_with?("{") && msg.end_with?("}")
44
36
  msg = (JSON.parse(msg) rescue nil) || msg
45
37
  end
46
- { @opts[:message_key] => msg }
38
+ msg
47
39
  end
48
40
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  class SimpleJsonLogFormatter
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_json_log_formatter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - taogawa
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-03 00:00:00.000000000 Z
11
+ date: 2019-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler