md-logstasher 1.6.0 → 1.8.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: 628b152041e0a0d8218761c1ab723f43b16ff6a9746fcf102e5b0a8d03aea76e
4
- data.tar.gz: a3283254f14aef786d70dda74cae35d081d607e67a37522fff292ddab6119e80
3
+ metadata.gz: '0348868a167435982eb59967a93c089a748c0c186fd9e4047a64437e868b0023'
4
+ data.tar.gz: b5f0441ca7b83ce1a3aaf1bb0fab537fdab07a6d0c420f34ac75dbcbf426fbe4
5
5
  SHA512:
6
- metadata.gz: 68806d2f4d08aa90a7c61487d2101e9cbfa2802d1021986dc39d1d9469196d8524d6e07cdcb63b3df104c0198beff6b9510f3f2d876ddc79b4c565b5f2d2fe1e
7
- data.tar.gz: 38f876594090f8a545dcfcd5380142bbfb1d9b34c726b2d0cb6de96d547ee41aaf5463c245279adbe50d694f9ab6be0a30fd6cdc45ffd0ccf5efd1e66322065f
6
+ metadata.gz: d7b469e1c11272f3ebabdbf8b218068ca979342a9aa5d2a2e0ea5e7d20f723e369d2f4304fe72650e60af1b76dc8763fbb3764891453eb16622a5a7f6f563a4a
7
+ data.tar.gz: 84b8ca35715352709060707a86889dd3170985d2ed1224a55a583d478e73175b2d57f9d2b81c6a049d5801bbd56e92bbf7086b2c33f9e44f747c8627a20a4b9d
@@ -1,3 +1,3 @@
1
1
  module LogStasher
2
- VERSION = "1.6.0"
2
+ VERSION = "1.8.0"
3
3
  end
data/lib/logstasher.rb CHANGED
@@ -9,6 +9,28 @@ module LogStasher
9
9
  attr_writer :serialize_parameters
10
10
  attr_writer :silence_standard_logging
11
11
  attr_accessor :metadata
12
+ attr_accessor :default_device
13
+
14
+ def load_from_config(config)
15
+ ::LogStasher.logger = ::Logger.new("/dev/null")
16
+
17
+ config.each do |key,value|
18
+ key = key.to_s
19
+ case key
20
+ when 'metadata'
21
+ ::LogStasher.metadata = value
22
+ when 'device'
23
+ ::LogStasher.default_device = ::LogStasher::Device.factory(value)
24
+ ::LogStasher.logger = ::Logger.new(::LogStasher.default_device)
25
+ when 'include_parameters'
26
+ ::LogStasher.include_parameters = value
27
+ when 'serialize_parameters'
28
+ ::LogStasher.serialize_parameters = value
29
+ when 'silence_standard_logging'
30
+ ::LogStasher.silence_standard_logging = value
31
+ end
32
+ end
33
+ end
12
34
 
13
35
  def append_fields(&block)
14
36
  @append_fields_callback = block
@@ -46,18 +68,20 @@ module LogStasher
46
68
 
47
69
  def log_as_json(payload, as_logstash_event: false)
48
70
  payload = payload.dup
49
- payload.merge!(:metadata => metadata) if !metadata&.empty? && payload.is_a?(::Hash)
71
+
72
+ # Merge in metadata if configured. This supports a Hash and a fully formed
73
+ # LogStash::Event.
74
+ if !metadata.empty?
75
+ payload.merge!(:metadata => metadata) if payload.is_a?(::Hash)
76
+ payload.append(:metadata => metadata) if payload.is_a?(::LogStash::Event)
77
+ end
50
78
 
51
79
  # Wrap the hash in a logstash event if the caller wishes for a specific
52
80
  # formatting applied to the hash. This is used by log subscriber, for
53
81
  # example.
54
- json_payload = if as_logstash_event
55
- ::LogStash::Event.new(payload).to_json
56
- else
57
- payload.to_json
58
- end
82
+ payload = ::LogStash::Event.new(payload) if as_logstash_event
59
83
 
60
- logger << json_payload + $INPUT_RECORD_SEPARATOR
84
+ logger << payload.to_json + $INPUT_RECORD_SEPARATOR
61
85
  end
62
86
 
63
87
  def logger
@@ -36,6 +36,14 @@ describe ::LogStasher do
36
36
  ::LogStasher.log_as_json(:yolo => :brolo)
37
37
  end
38
38
 
39
+ it "merges metadata for LogStash::Event types" do
40
+ expect(::LogStasher.logger).to receive(:<<) do |json|
41
+ expect(::JSON.parse(json)).to match(a_hash_including("yolo" => "brolo", "metadata" => { "namespace" => "cooldude" }))
42
+ end
43
+
44
+ ::LogStasher.log_as_json(::LogStash::Event.new(:yolo => :brolo))
45
+ end
46
+
39
47
  it "does not merge metadata on an array" do
40
48
  expect(::LogStasher.logger).to receive(:<<) do |json|
41
49
  expect(::JSON.parse(json)).to eq([{ "yolo" => "brolo" }])
@@ -45,4 +53,83 @@ describe ::LogStasher do
45
53
  end
46
54
  end
47
55
  end
56
+
57
+ describe "#load_from_config" do
58
+ before(:each) do
59
+ ::LogStasher.metadata = {}
60
+ ::LogStasher.serialize_parameters = true
61
+ ::LogStasher.silence_standard_logging = false
62
+ end
63
+
64
+ it "loads with multiple config keys" do
65
+ config = {
66
+ metadata: {
67
+ namespace: 'kirby',
68
+ logged_via: 'logstasher',
69
+ },
70
+ device: {
71
+ type: 'stdout'
72
+ }
73
+ }
74
+
75
+ ::LogStasher.load_from_config(config)
76
+ expect(::LogStasher.metadata).to eq({:namespace => 'kirby', :logged_via => 'logstasher'})
77
+ expect(::LogStasher.default_device).to eq(STDOUT)
78
+ end
79
+
80
+ it "loads metadata" do
81
+ config = {
82
+ metadata: {
83
+ namespace: 'kirby',
84
+ logged_via: 'logstasher',
85
+ }
86
+ }
87
+
88
+ ::LogStasher.load_from_config(config)
89
+ expect(::LogStasher.metadata).to eq({:namespace => 'kirby', :logged_via => 'logstasher'})
90
+ end
91
+
92
+ it "loads parameters" do
93
+ config = {
94
+ include_parameters: false,
95
+ serialize_parameters: false,
96
+ silence_standard_logging: true,
97
+ silence_creation_message: false
98
+ }
99
+
100
+ ::LogStasher.load_from_config(config)
101
+ expect(::LogStasher.instance_variable_get(:@include_parameters)).to be false
102
+ expect(::LogStasher.instance_variable_get(:@serialize_parameters)).to be false
103
+ expect(::LogStasher.instance_variable_get(:@silence_standard_logging)).to be true
104
+ end
105
+
106
+ it "loads with a stdout device" do
107
+ config = {
108
+ metadata: {
109
+ namespace: 'kirby',
110
+ logged_via: 'logstasher',
111
+ }
112
+ }
113
+
114
+ ::LogStasher.load_from_config(config)
115
+ expect(::LogStasher.metadata).to eq({:namespace => 'kirby', :logged_via => 'logstasher'})
116
+ end
117
+
118
+ it "loads with a syslog device" do
119
+ config = {
120
+ device:
121
+ {
122
+ type: 'syslog',
123
+ identity: 'logstasher',
124
+ facility: 'LOG_LOCAL1',
125
+ priority: 'LOG_INFO',
126
+ flags: ['LOG_PID', 'LOG_CONS']
127
+ }
128
+ }
129
+ ::LogStasher.load_from_config(config)
130
+ expect(::LogStasher.metadata).to eq({})
131
+ expect(::LogStasher.default_device).to be_a ::LogStasher::Device::Syslog
132
+ end
133
+ end
134
+
48
135
  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.6.0
4
+ version: 1.8.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-09-09 00:00:00.000000000 Z
11
+ date: 2024-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: logstash-event
@@ -123,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
125
  requirements: []
126
- rubygems_version: 3.2.28
126
+ rubygems_version: 3.4.21
127
127
  signing_key:
128
128
  specification_version: 4
129
129
  summary: Awesome rails logs