logjam_agent 0.34.3 → 0.35.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -0
- data/lib/logjam_agent/buffered_logger.rb +2 -2
- data/lib/logjam_agent/request_handling.rb +28 -0
- data/lib/logjam_agent/selective_logging.rb +37 -0
- data/lib/logjam_agent/version.rb +1 -1
- data/lib/logjam_agent.rb +3 -27
- data/test/selective_logging_test.rb +59 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c22b624762a595ee627fba548bc5e41a3ac3a0180fdc8fa067969138963dd116
|
4
|
+
data.tar.gz: ee6d22ec9272fea8c786e16ca7088c7711b258bf1e6a7dd665270a5e5b9a5b62
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aac5ad227a70fa16e232c01abb6cea2b4852d717a7b81dc681a137bbe91d71f3cf7fc23901fc280a6db65eece15ac6c1608bf22e140351a8436020ef80a7b1db
|
7
|
+
data.tar.gz: d63f3d7758b328f01b2f4855c2dc9ef51cd1c1ed1152c900c46a942d389d38915d554ee732255c0ab946118209afbf528e47186a0d9825e1dfa351363615c3ab
|
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,37 @@
|
|
1
|
+
module LogjamAgent
|
2
|
+
module SelectiveLogging
|
3
|
+
extend self
|
4
|
+
|
5
|
+
def logjam_only
|
6
|
+
old_selector = logjam_log_selector
|
7
|
+
self.logjam_log_selector = :logjam_only
|
8
|
+
yield
|
9
|
+
ensure
|
10
|
+
self.logjam_log_selector = old_selector
|
11
|
+
end
|
12
|
+
|
13
|
+
def logdevice_only
|
14
|
+
old_selector = logjam_log_selector
|
15
|
+
self.logjam_log_selector = :logdevice_only
|
16
|
+
yield
|
17
|
+
ensure
|
18
|
+
self.logjam_log_selector = old_selector
|
19
|
+
end
|
20
|
+
|
21
|
+
def logjam_log_selector
|
22
|
+
Thread.current.thread_variable_get(:logjam_log_selector)
|
23
|
+
end
|
24
|
+
|
25
|
+
def logjam_log_selector=(selector)
|
26
|
+
Thread.current.thread_variable_set(:logjam_log_selector, selector)
|
27
|
+
end
|
28
|
+
|
29
|
+
def logjam_only?
|
30
|
+
logjam_log_selector == :logjam_only
|
31
|
+
end
|
32
|
+
|
33
|
+
def logdevice_only?
|
34
|
+
logjam_log_selector == :logdevice_only
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/logjam_agent/version.rb
CHANGED
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,59 @@
|
|
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
|
+
end
|
26
|
+
|
27
|
+
def test_normal_logging_adds_line_to_request_and_logdevice
|
28
|
+
assert !LogjamAgent.logjam_only?
|
29
|
+
assert !LogjamAgent.logdevice_only?
|
30
|
+
@logger.info("normal")
|
31
|
+
assert_equal 1, @lines.size
|
32
|
+
assert_equal "normal", @lines.first.last
|
33
|
+
assert_equal 1, @device.lines.size
|
34
|
+
assert_match(/normal\n/, @device.lines.first)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_logjam_only_logging_adds_line_to_request_but_not_to_logdevice
|
38
|
+
LogjamAgent.logjam_only do
|
39
|
+
assert LogjamAgent.logjam_only?
|
40
|
+
assert !LogjamAgent.logdevice_only?
|
41
|
+
@logger.info("logjam_only")
|
42
|
+
end
|
43
|
+
assert_equal 1, @lines.size
|
44
|
+
assert_equal "logjam_only", @lines.first.last
|
45
|
+
assert_equal 0, @device.lines.size
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_logdevice_only_logging_adds_line_to_logdevice_but_not_to_request
|
49
|
+
LogjamAgent.logdevice_only do
|
50
|
+
assert !LogjamAgent.logjam_only?
|
51
|
+
assert LogjamAgent.logdevice_only?
|
52
|
+
@logger.info("logdevice_only")
|
53
|
+
end
|
54
|
+
assert_equal [], @lines
|
55
|
+
assert_equal 1, @device.lines.size
|
56
|
+
assert_match(/logdevice_only\n/, @device.lines.first)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
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.
|
4
|
+
version: 0.35.0
|
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-
|
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
|