logjam_agent 0.34.3 → 0.35.1

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: f440bd17eaed288b652b4537a82ebcedaa6aae2c213e99cfce4c9a78259c8e1a
4
- data.tar.gz: 4c437031ed233e9b836714742f37e23a6b8b2bcc81155776d8ce31b17040cdd5
3
+ metadata.gz: 3f6c65ee6f965cad2efa823e0c7bbdc2445bcc03b5eb5b925a80a3cf5750440b
4
+ data.tar.gz: cfc487c0f873c6eeb63e90c2680c31394eb112231a9a8f17273864bf26f1f5a3
5
5
  SHA512:
6
- metadata.gz: d614ce5608d99b1735766d0d99a0b99d64a78bd75146891036d0063b11728ef8d92b0fcbb3de1bd95acd92999969fb0d4afcc06c9a439e986c279848eb899ee6
7
- data.tar.gz: 4f63d8cef062c3350e6fbbc26a997300fb5928430bf335a3d3efb959e97fd6ca7e14e13bd1ca15a4d58575485104005416b042b2b4546d6a1c017218b8789efa
6
+ metadata.gz: 9465b7e6caf9e029b6d68112829a5720f2ee3d3ffc31b53cf3331b24497a9c46f971f3cd30b683feba7022bb56066797fb18b8fc35bd2f542510fd2a9d844eb3
7
+ data.tar.gz: f5217174aaf9331df102b3a367d6e1d755cc2a689c8e778c7b676d8469e9ba32159b3c06d8d4dccdad956811f55ec86e50e6af935be698ad9f7bb0179b22aff6
data/README.md CHANGED
@@ -166,6 +166,16 @@ Sinatra's environment setting.
166
166
  Set the environment variable `APP_LOG_TO_STDOUT` if you want to log to `STDOUT`.
167
167
  Otherwise, logs will appear in the subdirectory `log` of your application's root.
168
168
 
169
+ ## Selective Logging
170
+
171
+ The agent adds log lines to the request information sent via ZMQ on
172
+ the logging bus and also writes log lines to the configured log
173
+ device.
174
+
175
+ Using one of the logjam helpers `LogjamAgent.logjam_only` or
176
+ `LogjamAgent.logdevice_only` it is possible to send information to
177
+ only one of those log line sinks for the duration of a given block.
178
+
169
179
 
170
180
  ## Troubleshooting
171
181
 
@@ -60,11 +60,11 @@ module LogjamAgent
60
60
  attributes = formatter.render_attributes
61
61
  message = "[#{attributes}] #{message}" if attributes
62
62
  time = Time.now
63
- if log_to_log_device
63
+ if log_to_log_device && !SelectiveLogging.logjam_only?
64
64
  formatted_message = formatter.call(format_severity(severity), time, progname, message)
65
65
  @logdev.write(formatted_message) if @logdev
66
66
  end
67
- request.add_line(severity, time, message) if request
67
+ request.add_line(severity, time, message) if request && !SelectiveLogging.logdevice_only?
68
68
  message
69
69
  end
70
70
 
@@ -0,0 +1,28 @@
1
+ module LogjamAgent
2
+ module RequestHandling
3
+ extend self
4
+
5
+ def request
6
+ Thread.current.thread_variable_get(:logjam_request)
7
+ end
8
+
9
+ def request=(request)
10
+ Thread.current.thread_variable_set(:logjam_request, request)
11
+ end
12
+
13
+ def start_request(*args)
14
+ initial_fields = args.extract_options!
15
+ app = args[0] || LogjamAgent.application_name
16
+ env = args[1] || LogjamAgent.environment_name
17
+ self.request = Request.new(app, env, initial_fields)
18
+ end
19
+
20
+ def finish_request(additional_fields = {})
21
+ if request = self.request
22
+ request.fields.merge!(additional_fields)
23
+ self.request = nil
24
+ request.forward
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,40 @@
1
+ module LogjamAgent
2
+ module SelectiveLogging
3
+ extend self
4
+
5
+ mattr_accessor :selective_logging_enabled
6
+ self.selective_logging_enabled = true
7
+
8
+ def logjam_only
9
+ old_selector = logjam_log_selector
10
+ self.logjam_log_selector = :logjam_only if selective_logging_enabled
11
+ yield
12
+ ensure
13
+ self.logjam_log_selector = old_selector
14
+ end
15
+
16
+ def logdevice_only
17
+ old_selector = logjam_log_selector
18
+ self.logjam_log_selector = :logdevice_only if selective_logging_enabled
19
+ yield
20
+ ensure
21
+ self.logjam_log_selector = old_selector
22
+ end
23
+
24
+ def logjam_log_selector
25
+ Thread.current.thread_variable_get(:logjam_log_selector)
26
+ end
27
+
28
+ def logjam_log_selector=(selector)
29
+ Thread.current.thread_variable_set(:logjam_log_selector, selector)
30
+ end
31
+
32
+ def logjam_only?
33
+ logjam_log_selector == :logjam_only
34
+ end
35
+
36
+ def logdevice_only?
37
+ logjam_log_selector == :logdevice_only
38
+ end
39
+ end
40
+ end
@@ -1,3 +1,3 @@
1
1
  module LogjamAgent
2
- VERSION = "0.34.3"
2
+ VERSION = "0.35.1"
3
3
  end
data/lib/logjam_agent.rb CHANGED
@@ -8,38 +8,13 @@ module Logger::Severity
8
8
  NONE = UNKNOWN + 1
9
9
  end
10
10
 
11
- module LogjamAgent
12
- module RequestHandling
13
- def request
14
- Thread.current.thread_variable_get(:logjam_request)
15
- end
16
-
17
- def request=(request)
18
- Thread.current.thread_variable_set(:logjam_request, request)
19
- end
20
-
21
- def start_request(*args)
22
- initial_fields = args.extract_options!
23
- app = args[0] || LogjamAgent.application_name
24
- env = args[1] || LogjamAgent.environment_name
25
- self.request = Request.new(app, env, initial_fields)
26
- end
27
-
28
- def finish_request(additional_fields = {})
29
- if request = self.request
30
- request.fields.merge!(additional_fields)
31
- self.request = nil
32
- request.forward
33
- end
34
- end
35
- end
36
- end
37
-
38
11
  require "logjam_agent/version"
39
12
  require "logjam_agent/util"
40
13
  require "logjam_agent/zmq_forwarder"
41
14
  require "logjam_agent/forwarders"
42
15
  require "logjam_agent/request"
16
+ require "logjam_agent/selective_logging"
17
+ require "logjam_agent/request_handling"
43
18
  require "logjam_agent/buffered_logger"
44
19
  require "logjam_agent/logging_attributes"
45
20
  require "logjam_agent/syslog_like_formatter"
@@ -120,6 +95,7 @@ module LogjamAgent
120
95
  end
121
96
 
122
97
  extend RequestHandling
98
+ extend SelectiveLogging
123
99
 
124
100
  NO_COMPRESSION = 0
125
101
  ZLIB_COMPRESSION = 1
@@ -0,0 +1,70 @@
1
+ require_relative "test_helper.rb"
2
+
3
+ module LogjamAgent
4
+ class SelectiveLoggingTest < MiniTest::Test
5
+ class MockLogDev
6
+ attr_reader :lines
7
+ def initialize
8
+ @lines = []
9
+ end
10
+ def write(s)
11
+ @lines << s
12
+ end
13
+ end
14
+
15
+ def setup
16
+ @request = LogjamAgent.request = Request.new("app", "env", {})
17
+ @lines = @request.instance_variable_get :@lines
18
+ @logger = BufferedLogger.new(File::NULL)
19
+ @device = MockLogDev.new
20
+ @logger.logdev = @device
21
+ end
22
+
23
+ def teardown
24
+ LogjamAgent.request = nil
25
+ LogjamAgent.selective_logging_enabled = true
26
+ end
27
+
28
+ def test_normal_logging_adds_line_to_request_and_logdevice
29
+ refute LogjamAgent.logjam_only?
30
+ refute LogjamAgent.logdevice_only?
31
+ @logger.info("normal")
32
+ assert_equal 1, @lines.size
33
+ assert_equal "normal", @lines.first.last
34
+ assert_equal 1, @device.lines.size
35
+ assert_match(/normal\n/, @device.lines.first)
36
+ end
37
+
38
+ def test_logjam_only_logging_adds_line_to_request_but_not_to_logdevice
39
+ LogjamAgent.logjam_only do
40
+ assert LogjamAgent.logjam_only?
41
+ refute LogjamAgent.logdevice_only?
42
+ @logger.info("logjam_only")
43
+ end
44
+ assert_equal 1, @lines.size
45
+ assert_equal "logjam_only", @lines.first.last
46
+ assert_equal 0, @device.lines.size
47
+ end
48
+
49
+ def test_logdevice_only_logging_adds_line_to_logdevice_but_not_to_request
50
+ LogjamAgent.logdevice_only do
51
+ refute LogjamAgent.logjam_only?
52
+ assert LogjamAgent.logdevice_only?
53
+ @logger.info("logdevice_only")
54
+ end
55
+ assert_equal [], @lines
56
+ assert_equal 1, @device.lines.size
57
+ assert_match(/logdevice_only\n/, @device.lines.first)
58
+ end
59
+
60
+ def test_select_logging_can_be_globally_disabled
61
+ LogjamAgent.selective_logging_enabled = false
62
+ LogjamAgent.logdevice_only do
63
+ refute LogjamAgent.logdevice_only?
64
+ end
65
+ LogjamAgent.logjam_only do
66
+ refute LogjamAgent.logjam_only?
67
+ end
68
+ end
69
+ end
70
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logjam_agent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.34.3
4
+ version: 0.35.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Kaes
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-05 00:00:00.000000000 Z
11
+ date: 2022-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -244,12 +244,15 @@ files:
244
244
  - lib/logjam_agent/railtie.rb
245
245
  - lib/logjam_agent/receiver.rb
246
246
  - lib/logjam_agent/request.rb
247
+ - lib/logjam_agent/request_handling.rb
248
+ - lib/logjam_agent/selective_logging.rb
247
249
  - lib/logjam_agent/sinatra.rb
248
250
  - lib/logjam_agent/syslog_like_formatter.rb
249
251
  - lib/logjam_agent/util.rb
250
252
  - lib/logjam_agent/version.rb
251
253
  - lib/logjam_agent/zmq_forwarder.rb
252
254
  - test/request_test.rb
255
+ - test/selective_logging_test.rb
253
256
  - test/sinatra_app.rb
254
257
  - test/sinatra_classic_app.rb
255
258
  - test/sinatra_classic_test.rb
@@ -282,6 +285,7 @@ specification_version: 4
282
285
  summary: Logjam client library to be used with logjam
283
286
  test_files:
284
287
  - test/sinatra_app.rb
288
+ - test/selective_logging_test.rb
285
289
  - test/sinatra_classic_test.rb
286
290
  - test/sinatra_classic_app.rb
287
291
  - test/request_test.rb