nypl_log_formatter 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c5d63110408241c2f2ba556c448fb4fafe72cfea6ad8a6d1102f4bd0478816d1
4
- data.tar.gz: 276af5118f265e03f57c0c8101ed9856dfc81353b0ba9f29a2aa435e314d64c0
3
+ metadata.gz: cd40e2cd4eb74fc6ced36167ddbae1cdf7311d63ee7eefde5339eb49de30fb04
4
+ data.tar.gz: a8851f466ec347427d1f351eef57731fc9cc06055fa0bfe3421af9af961d902f
5
5
  SHA512:
6
- metadata.gz: 14586be188d33b6d9329adc82ba7a08e6e3fc66025ea9e15f1d457230de5b84f4d4c07e0e48965727476579ef5b702ddb346c12ca1fe338928ebcae2c82279f8
7
- data.tar.gz: 86bc20340c14fbb7c07da15311ac337619572518e81d3929f6975ae7ae5277c4cdf211112026afdcbdf2cb8987c5f8a598a817777191cc799c9f0aa981898b56
6
+ metadata.gz: d727480ed486f211fcd0f6997f054fce95bb97ed7cf8c3c2eb3434384310f8903573fcb4b7d2030c2381a3af85a1f0b9b39f9a1f4bb30781d60ced7957697e2b
7
+ data.tar.gz: 63beecc9f84b35a4afae85177ed9e709fc7ebbe1e8255c79604a7632d27fa2359e29d3931d612b4aaddc0404caee9d07b3ec3c43f1710175b7f9cea1509b7d0c
data/README.md CHANGED
@@ -8,7 +8,7 @@ The only advantage to subclassing is that we can bake in [custom formatting](htt
8
8
 
9
9
  ## Ruby Version Support
10
10
 
11
- Our [`.travis.yml`]('.travis.yml') tests against multiple Ruby versions.
11
+ Our [`.travis.yml`](.travis.yml) tests against multiple Ruby versions.
12
12
  Feel free to add more.
13
13
 
14
14
  ## Installation
@@ -54,6 +54,26 @@ That includes:
54
54
 
55
55
  For more info see your ruby version's documentation for the `Logger` class.
56
56
 
57
+ ### Logging Additional Key/Value Pairs
58
+
59
+ You can pass a second argument, a Hash that will end up as keys/values in the
60
+ logged JSON.
61
+
62
+ ```ruby
63
+ logger = NyplLogFormatter.new('path/to/file.log')
64
+
65
+ logger.error(
66
+ 'Something went wrong',
67
+ user: {email: 'simon@example.com', name: 'simon'},
68
+ permissions: ['admin', 'good-boy']
69
+ )
70
+
71
+ # Contents of file.log
72
+ # Logfile created on 2018-01-17 15:51:31 -0500 by logger.rb/61378
73
+ #{"level":"ERROR","message":"Something went wrong","timestamp":"2018-02-07T16:47:22.017-0500","user":{"email":"simon@example.com","name":"simon"},"permissions":["admin","good-boy"]}
74
+
75
+ ```
76
+
57
77
  ## Development
58
78
 
59
79
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -10,17 +10,42 @@ class NyplLogFormatter < ::Logger
10
10
  super(*args)
11
11
  # This has to happen _after_ call to super, otherwise ::Logger will set the formatter
12
12
  set_formatter
13
+ allow_arbitrary_keys
13
14
  end
14
15
 
15
- private
16
+ private
17
+
18
+ # Redefines #log(), #error(), etc...but explodes the `progname` arg
19
+ # to be more than a simple string.
20
+ # See original implementations here: https://github.com/ruby/ruby/blob/trunk/lib/logger.rb#L524
21
+ def allow_arbitrary_keys
22
+ Logger::Severity.constants.each do |severity_name|
23
+ define_singleton_method(severity_name.to_s.downcase.to_sym) do |*progname, &block|
24
+ add(eval(severity_name.to_s), nil, progname, &block)
25
+ end
26
+ end
27
+ end
16
28
 
17
29
  def set_formatter
18
30
  self.formatter = proc do |severity, datetime, progname, msg|
19
31
  message_hash = {
20
32
  level: severity.upcase,
21
- message: msg,
33
+ message: msg.is_a?(Array) ? msg.shift : msg,
22
34
  timestamp: Time.now.strftime("%Y-%m-%dT%H:%M:%S.%L%z")
23
35
  }
36
+
37
+ if msg.is_a?(Array)
38
+ msg.each do |additional_key_values|
39
+ additional_key_values.each do |key, value|
40
+ message_hash[key] = value
41
+ end
42
+ end
43
+ end
44
+
45
+ if progname && !progname.empty?
46
+ message_hash['programName'] = progname[0]
47
+ end
48
+
24
49
  "#{JSON.generate(message_hash)}\n"
25
50
  end
26
51
  end
@@ -1,3 +1,3 @@
1
1
  module NyplLogFormatterVersion
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nypl_log_formatter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - nodanaonlyzuul
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-05 00:00:00.000000000 Z
11
+ date: 2018-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry