md-logstasher 1.4.0 → 1.6.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: 5f6a2e729c225c8fb2cf06174d9fc5282ae0b2ee660fd89d675a30e7fcd19eb3
4
- data.tar.gz: 9a8dea2258d1ed2c0fb1c01227bf7ea6b78421ca342ba122a32cc8f1351b7440
3
+ metadata.gz: 628b152041e0a0d8218761c1ab723f43b16ff6a9746fcf102e5b0a8d03aea76e
4
+ data.tar.gz: a3283254f14aef786d70dda74cae35d081d607e67a37522fff292ddab6119e80
5
5
  SHA512:
6
- metadata.gz: d23d146376ea21233a2b1189366586cbecc46f4a5694ffb1b7cae480881098828f7e9e79f171cf7f1c52372b793f53aff40d86dd444a4aee4e8a12e9334e2e7a
7
- data.tar.gz: f6a8c91e937935dac803868c098e8b6826cb0b27f72fe5718bf8fcebad2685acdeb899e8055d6ee6cc2a4420bcf3465a7e3557e99875b8b2fbf8fbccd5c90618
6
+ metadata.gz: 68806d2f4d08aa90a7c61487d2101e9cbfa2802d1021986dc39d1d9469196d8524d6e07cdcb63b3df104c0198beff6b9510f3f2d876ddc79b4c565b5f2d2fe1e
7
+ data.tar.gz: 38f876594090f8a545dcfcd5380142bbfb1d9b34c726b2d0cb6de96d547ee41aaf5463c245279adbe50d694f9ab6be0a30fd6cdc45ffd0ccf5efd1e66322065f
@@ -14,6 +14,8 @@ module LogStasher
14
14
  when "udp", :udp then
15
15
  require 'logstasher/device/udp'
16
16
  ::LogStasher::Device::UDP.new(config)
17
+ when "stdout", :stdout then
18
+ $stdout
17
19
  else
18
20
  fail ArgumentError, "Unknown type: #{type}"
19
21
  end
@@ -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
@@ -5,6 +5,7 @@ module LogStasher
5
5
  config.logstasher.include_parameters = true
6
6
  config.logstasher.serialize_parameters = true
7
7
  config.logstasher.silence_standard_logging = false
8
+ config.logstasher.silence_creation_message = true
8
9
  config.logstasher.logger = nil
9
10
  config.logstasher.log_level = ::Logger::INFO
10
11
 
@@ -49,7 +50,9 @@ module LogStasher
49
50
  def default_logger
50
51
  unless @default_logger
51
52
  path = ::Rails.root.join('log', "logstash_#{::Rails.env}.log")
52
- ::FileUtils.touch(path) # prevent autocreate messages in log
53
+ if config.logstasher.silence_creation_message
54
+ ::FileUtils.touch(path) # prevent autocreate messages in log
55
+ end
53
56
 
54
57
  @default_logger = ::Logger.new(path)
55
58
  end
@@ -1,3 +1,3 @@
1
1
  module LogStasher
2
- VERSION = "1.4.0"
2
+ VERSION = "1.6.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
@@ -58,6 +58,10 @@ describe LogStasher::Device do
58
58
  expect(device).to be_a_kind_of(::LogStasher::Device::UDP)
59
59
  end
60
60
 
61
+ it "can create stdout devices" do
62
+ device = ::LogStasher::Device.factory(:type => "stdout")
63
+ expect(device).to eq $stdout
64
+ end
61
65
 
62
66
  it "fails to create unknown devices" do
63
67
  expect {
@@ -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
@@ -18,6 +18,24 @@ end
18
18
  describe ::LogStasher::Railtie do
19
19
  let(:config) { described_class.config.logstasher }
20
20
 
21
+ describe 'logstasher should touch log file to prevent creation message by default' do
22
+ it 'should configure LogStasher' do
23
+ expect(::FileUtils).to receive(:touch)
24
+ ActiveSupport.run_load_hooks(:before_initialize)
25
+ end
26
+ end
27
+
28
+ describe 'logstasher should NOT touch log file if silence disabled' do
29
+ before { config.silence_creation_message = false }
30
+ after { config.silence_creation_message = true }
31
+
32
+ it 'should configure LogStasher' do
33
+ expect(::FileUtils).not_to receive(:touch)
34
+ ActiveSupport.run_load_hooks(:before_initialize)
35
+ end
36
+ end
37
+
38
+
21
39
  describe 'logstasher.configure' do
22
40
  it 'should configure LogStasher' do
23
41
  config.logger = ::Logger.new('/dev/null')
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.0
4
+ version: 1.6.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-09-09 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