md-logstasher 1.4.1 → 1.5.0

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: b6d718c5b30fe09e6735c5da021eda006340fdef09dc6a5b251ab6d8a273429b
4
- data.tar.gz: 35bf7d80f1ce2d8d736de8d47bdd8f00e2ea3579992f057e10e651d9290e1f28
3
+ metadata.gz: 2a3ecf9c0501945ebb201adb054bcb8e419ab6282e4373fcc5f83c141f0fe606
4
+ data.tar.gz: 52acbb7139d4f00775f0ad88115a0237dee699aa8ce42fc8b9c0aabdd9786438
5
5
  SHA512:
6
- metadata.gz: c7e667ea8187888cc4c7662b6868584a1d829ef878f9c0a949e716cf59093141a9f8f29bd659a933ee1c6ce69f422142f351125515be264cfb32de33e9164994
7
- data.tar.gz: e167c08b5eaef0ea61e63df8e666974617684ea6a938fb13f44e62aafd9a5d157de4e1d2f3b05568e1186aa4be5160130a479fa0449a6169ffec98ea7bad905e
6
+ metadata.gz: 074e9580a2b16e2d9d25c948559a43c74d03ea3de65854b20c36fbaeb2f921d77ea11206533bebe6b9ed490d1ffc5e5f0951db1ed83f9275c5309c39cb9a78b6
7
+ data.tar.gz: 8353c5595d42b6da680afbc7cf4b673bf23b5b2ba9f784e7bb9c1e5b34c5fe0faa3d9ba9c648cd449e50299691a7bb91f69b3ee46458d85f8074558146801c07
@@ -28,9 +28,7 @@ module LogStasher
28
28
  fields.merge! extract_parameters(payload)
29
29
  fields.merge! appended_fields
30
30
 
31
- event = LogStash::Event.new(fields.merge('tags' => tags))
32
-
33
- LogStasher.logger << event.to_json + "\n"
31
+ LogStasher.log_as_json(fields.merge('tags' => tags), :as_logstash_event => true)
34
32
  end
35
33
 
36
34
  def redirect_to(event)
@@ -51,7 +49,7 @@ module LogStasher
51
49
  end
52
50
 
53
51
  def extract_request(payload)
54
- result = {
52
+ {
55
53
  :action => payload[:action],
56
54
  :controller => payload[:controller],
57
55
  :format => extract_format(payload),
@@ -61,9 +59,6 @@ module LogStasher
61
59
  :path => extract_path(payload),
62
60
  :route => "#{payload[:controller]}##{payload[:action]}"
63
61
  }
64
- metadata = ::LogStasher.metadata
65
- result.merge!(:metadata => metadata) unless metadata&.empty?
66
- result
67
62
  end
68
63
 
69
64
  # Monkey patching to enable exception logging
@@ -1,3 +1,3 @@
1
1
  module LogStasher
2
- VERSION = "1.4.1"
2
+ VERSION = "1.5.0"
3
3
  end
data/lib/logstasher.rb CHANGED
@@ -44,6 +44,22 @@ module LogStasher
44
44
  end
45
45
  end
46
46
 
47
+ def log_as_json(payload, as_logstash_event: false)
48
+ payload = payload.dup
49
+ payload.merge!(:metadata => metadata) if !metadata&.empty? && payload.is_a?(::Hash)
50
+
51
+ # Wrap the hash in a logstash event if the caller wishes for a specific
52
+ # formatting applied to the hash. This is used by log subscriber, for
53
+ # example.
54
+ json_payload = if as_logstash_event
55
+ ::LogStash::Event.new(payload).to_json
56
+ else
57
+ payload.to_json
58
+ end
59
+
60
+ logger << json_payload + $INPUT_RECORD_SEPARATOR
61
+ end
62
+
47
63
  def logger
48
64
  @logger ||= initialize_logger
49
65
  end
@@ -57,8 +57,10 @@ describe LogStasher::LogSubscriber do
57
57
 
58
58
  let(:event) { double(:payload => payload, :duration => duration) }
59
59
 
60
+ before { ::LogStasher.metadata = data }
61
+ after { ::LogStasher.metadata = {} }
62
+
60
63
  it 'logs the event in logstash format' do
61
- ::LogStasher.metadata = data
62
64
  expect(logger).to receive(:<<) do |json|
63
65
  expect(JSON.parse(json)).to eq({
64
66
  '@timestamp' => timestamp,
@@ -0,0 +1,48 @@
1
+ require "spec_helper"
2
+
3
+ describe ::LogStasher do
4
+ describe "#log_as_json" do
5
+ it "calls the logger with the payload" do
6
+ expect(::LogStasher.logger).to receive(:<<) do |json|
7
+ expect(::JSON.parse(json)).to eq("yolo" => "brolo")
8
+ end
9
+
10
+ ::LogStasher.log_as_json(:yolo => :brolo)
11
+ end
12
+
13
+ context "with event" do
14
+ it "calls logger with a logstash event" do
15
+ expect(::LogStasher.logger).to receive(:<<) do |json|
16
+ payload = ::JSON.parse(json)
17
+
18
+ expect(payload["@timestamp"]).to_not be_nil
19
+ expect(payload["@version"]).to eq("1")
20
+ expect(payload["yolo"]).to eq("brolo")
21
+ end
22
+
23
+ ::LogStasher.log_as_json({:yolo => :brolo}, :as_logstash_event => true)
24
+ end
25
+ end
26
+
27
+ context "with metadata" do
28
+ before { ::LogStasher.metadata = { :namespace => :cooldude } }
29
+ after { ::LogStasher.metadata = {} }
30
+
31
+ it "calls logger with the metadata" do
32
+ expect(::LogStasher.logger).to receive(:<<) do |json|
33
+ expect(::JSON.parse(json)).to eq("yolo" => "brolo", "metadata" => { "namespace" => "cooldude" })
34
+ end
35
+
36
+ ::LogStasher.log_as_json(:yolo => :brolo)
37
+ end
38
+
39
+ it "does not merge metadata on an array" do
40
+ expect(::LogStasher.logger).to receive(:<<) do |json|
41
+ expect(::JSON.parse(json)).to eq([{ "yolo" => "brolo" }])
42
+ end
43
+
44
+ ::LogStasher.log_as_json([{:yolo => :brolo}])
45
+ end
46
+ end
47
+ end
48
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: md-logstasher
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.1
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Devin Christensen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-26 00:00:00.000000000 Z
11
+ date: 2022-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: logstash-event
@@ -100,6 +100,7 @@ files:
100
100
  - spec/lib/logstasher/device/udp_spec.rb
101
101
  - spec/lib/logstasher/device_spec.rb
102
102
  - spec/lib/logstasher/log_subscriber_spec.rb
103
+ - spec/lib/logstasher/logstasher_spec.rb
103
104
  - spec/lib/logstasher/railtie_spec.rb
104
105
  - spec/lib/logstasher_spec.rb
105
106
  - spec/spec_helper.rb
@@ -132,6 +133,7 @@ test_files:
132
133
  - spec/lib/logstasher/device/udp_spec.rb
133
134
  - spec/lib/logstasher/device_spec.rb
134
135
  - spec/lib/logstasher/log_subscriber_spec.rb
136
+ - spec/lib/logstasher/logstasher_spec.rb
135
137
  - spec/lib/logstasher/railtie_spec.rb
136
138
  - spec/lib/logstasher_spec.rb
137
139
  - spec/spec_helper.rb