json_tagged_logger 0.2.1 → 0.4.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
  SHA256:
3
- metadata.gz: 5d6a91dae71312da17de8ad46580f46a3a5d7c6c20d8e1444107734cb7775c49
4
- data.tar.gz: 159b9a61bf557b8324c2d1c1f3dd03e4fc2f37625996046e7de29fd2b2f05a0f
3
+ metadata.gz: f290019420ad4691086787da1f7c2982383eaae9812bc2eb1874778ce5d00718
4
+ data.tar.gz: 9a52e70773163bd3428612d56b1f723bd50550763f84710eda06aec1a02cd9a7
5
5
  SHA512:
6
- metadata.gz: 43d1c8b1d1905f04271bdb538618e4b24d6b1eefc1d2864d01c38949ae5b1f6dcfccc0af60254136667cef7dd345becf52cf65169030818f911d4d56c7abdf4f
7
- data.tar.gz: 06fd82f28251e33f49a31273d415f76d4d9a5b1fc085b05a707722b3be9046f96148731f64324c9a44f6cbc86c2a3769d44652c893713f7fd4553173e35df1d9
6
+ metadata.gz: 902135acc6779b5d4a5f63313a8f0ced0125d08998d61ebf77064dd4159764cb14bac9353a11f0efc70a02cc99bed628eaea25dbbd95169dc79c3ef5b782d786
7
+ data.tar.gz: 3ad70d7851f82976ac866d1ad3553670f3122c8476429de4064d71333214cadbebcfb7e9af1d013151c04f83ff5d8038ce52cc7fd88fb453c7bf8529074aae50
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # JsonTaggedLogger
2
2
 
3
+ [![Build Status](https://github.com/santry/json_tagged_logger/actions/workflows/ci.yml/badge.svg)](https://github.com/santry/json_tagged_logger/actions/workflows/ci.yml)
4
+
3
5
  `JsonTaggedLogger` works in conjunction with [`ActiveSupport::TaggedLogging`](https://api.rubyonrails.org/classes/ActiveSupport/TaggedLogging.html) and (optionally) [Lograge](https://github.com/roidrage/lograge) to produce JSON-formatted log output. By itself, `ActiveSupport::TaggedLogging` supports simple tagging. With `JsonTaggedLogger`, you can compose key/value pairs, simple tags, and the log message itself into a single JSON document for easy consumption and parsing in log aggregators.
4
6
 
5
7
  ## Usage
@@ -60,7 +62,7 @@ will get you something like
60
62
  }
61
63
  ```
62
64
 
63
- [Note: I've pretty-printed the output in these examples for easier reading. The actual log output will be on a single line without extra whitespace.]
65
+ [_Note_: By default, `JsonTaggedLogger::Formatter` outputs logs as single lines without extra whitespace. Setting `JsonTaggedLogger::Formatter#pretty_print` to `true` will pretty print the logs, as I've done in these examples.]
64
66
 
65
67
  Importantly, if the controller action (or any code it calls along the way) has an explicit call to `Rails.logger.tagged("TAG").info("tagged log message")`, you'll get the same key/value tags (`request_id`, `host`, `my_param`, &c.) in the JSON document along with a `tags` key:
66
68
 
@@ -1,5 +1,14 @@
1
+ require 'active_support/core_ext/hash/keys'
2
+ require 'json'
3
+
1
4
  module JsonTaggedLogger
2
5
  class Formatter
6
+ attr_accessor :pretty_print
7
+
8
+ def initialize(pretty_print: false)
9
+ @pretty_pretty = pretty_print
10
+ end
11
+
3
12
  def call(severity, _time, _progname, message)
4
13
  log = {
5
14
  level: severity,
@@ -10,7 +19,7 @@ module JsonTaggedLogger
10
19
  json_tags.each { |t| log.merge!(t) }
11
20
 
12
21
  if text_tags.present?
13
- log[:tags] = text_tags
22
+ log[:tags] = text_tags.to_a
14
23
  end
15
24
 
16
25
  bare_message = message_without_tags(message)
@@ -21,13 +30,19 @@ module JsonTaggedLogger
21
30
  parsed_message = bare_message
22
31
  ensure
23
32
  if parsed_message.is_a?(Hash)
24
- log.merge!(parsed_message.symbolize_keys)
33
+ parsed_message.symbolize_keys!
34
+ if log.has_key?(:tags) && parsed_message.has_key?(:tags)
35
+ parsed_message[:tags] = parsed_message[:tags] + log[:tags]
36
+ end
37
+
38
+ log.merge!(parsed_message)
25
39
  else
40
+ #binding.irb
26
41
  log.merge!(msg: parsed_message.strip)
27
42
  end
28
43
  end
29
44
 
30
- log.compact.to_json + "\n"
45
+ format_for_output(log)
31
46
  end
32
47
 
33
48
  private
@@ -60,5 +75,17 @@ module JsonTaggedLogger
60
75
  message
61
76
  end
62
77
  end
78
+
79
+ def format_for_output(log_hash)
80
+ compacted_log = log_hash.compact
81
+
82
+ output_json = if pretty_print
83
+ JSON.pretty_generate(compacted_log)
84
+ else
85
+ JSON.generate(compacted_log)
86
+ end
87
+
88
+ output_json + "\n"
89
+ end
63
90
  end
64
91
  end
@@ -1,13 +1,17 @@
1
+ require 'action_dispatch'
2
+
1
3
  module JsonTaggedLogger
2
4
  class LogTagsConfig
3
5
  def self.generate(*tags)
4
6
  tags.map do |tag|
5
- if tag.is_a?(Proc) && tag.arity == 1
7
+ if tag.is_a?(String)
8
+ tag
9
+ elsif tag.is_a?(Proc) && tag.arity == 1
6
10
  tag
7
11
  elsif tag.is_a?(Symbol) && ActionDispatch::Request.method_defined?(tag)
8
12
  -> (request) { { tag => request.send(tag) }.to_json }
9
13
  else
10
- raise ArgumentError, "Only symbols that ActionDispatch::Request responds to or single-argument Procs allowed. You provided '#{tag.inspect}'."
14
+ raise ArgumentError, "Only strings, symbols that ActionDispatch::Request responds to or single-argument Procs allowed. You provided '#{tag.inspect}'."
11
15
  end
12
16
  end
13
17
  end
@@ -1,3 +1,6 @@
1
+ require 'active_support'
2
+ require 'active_support/tagged_logging'
3
+
1
4
  module JsonTaggedLogger
2
5
  module Logger
3
6
  def self.new(logger)
@@ -1,3 +1,5 @@
1
+ require 'action_dispatch'
2
+
1
3
  module JsonTaggedLogger
2
4
  module TagFromSession
3
5
  def self.get(log_label, session_key = log_label)
@@ -1,3 +1,3 @@
1
1
  module JsonTaggedLogger
2
- VERSION = "0.2.1"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_tagged_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Santry
@@ -9,7 +9,77 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 1980-01-01 00:00:00.000000000 Z
12
- dependencies: []
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '2.2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '2.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '5.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '5.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '6.1'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '6.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: actionpack
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '6.1'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '6.1'
13
83
  description: Formatter for logging with ActiveSupport::TaggedLogging as JSON
14
84
  email:
15
85
  - sean@santry.us
@@ -38,7 +108,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
38
108
  requirements:
39
109
  - - ">="
40
110
  - !ruby/object:Gem::Version
41
- version: '0'
111
+ version: '2.7'
42
112
  required_rubygems_version: !ruby/object:Gem::Requirement
43
113
  requirements:
44
114
  - - ">="