loggerstash 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/filtered_debug_logger.rb +42 -0
- data/lib/loggerstash.rb +65 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1ea3f2fdbe4dfa4604759d473ef117f7c1c1961b3c484be9b7deb1c2ed53dc74
|
4
|
+
data.tar.gz: ca16b574bdd0adfef725a7eb8a3821a541ba8f74bc7f88be0185888daf125ec3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bea707a8f38924bfac3b4c61d61fff5eff9d534c4beaa08024cc70865a79968a5f9a75399e9a4f7a06a9cc93351c539c7077ef4c7a11e0764adb6d0235107f56
|
7
|
+
data.tar.gz: 64d870fac502e29a4dc811ae0d4e83268a80e238642021b6294a2029b5b914b7b09a9595bb7347ce69c3d8312f3198aa590956cc0e5d009bbcef8deec7870cd8
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
3
|
+
# Filter debug-level log entries on progname
|
4
|
+
#
|
5
|
+
# Whilst well-thought-out debug logs are fantastic at showing you the
|
6
|
+
# fine-level detail of your program's execution, they can sometimes be
|
7
|
+
# "too much of a good thing". Excessively verbose debug logs can obscure
|
8
|
+
# the important debug info, and turning on debug logging on a busy service
|
9
|
+
# can quickly swamp all but the most overprovisioned of log aggregation
|
10
|
+
# systems.
|
11
|
+
#
|
12
|
+
# Hence, there's this little module. Require it in your program, and then
|
13
|
+
# set `logger.permitted_prognames = ['some', 'array']` on whatever logger
|
14
|
+
# is likely to want some debug logging. Then, whenever debug logging is
|
15
|
+
# enabled, only those calls to `logger.debug` which provide a progname exactly
|
16
|
+
# matching an entry in the list you provided will actually get logged.
|
17
|
+
#
|
18
|
+
module FilteredDebugLogger
|
19
|
+
# Set the list of prognames to log debug messages for.
|
20
|
+
#
|
21
|
+
# @param l [Array<String>] the (exact) prognames to log debug-level messages
|
22
|
+
# for. If it's not in this list, it doesn't get emitted, even if debug
|
23
|
+
# logging is enabled.
|
24
|
+
#
|
25
|
+
def permitted_prognames=(l)
|
26
|
+
raise ArgumentError, "Must provide an array" unless l.is_a?(Array)
|
27
|
+
|
28
|
+
@permitted_prognames = l
|
29
|
+
end
|
30
|
+
|
31
|
+
# Decorate Logger#add with our "reject by progname" logic.
|
32
|
+
#
|
33
|
+
def add(s, m = nil, p = nil)
|
34
|
+
return if s == Logger::DEBUG && @permitted_prognames && !@permitted_prognames.include?(p)
|
35
|
+
|
36
|
+
super
|
37
|
+
end
|
38
|
+
|
39
|
+
alias log add
|
40
|
+
end
|
41
|
+
|
42
|
+
Logger.prepend(FilteredDebugLogger)
|
data/lib/loggerstash.rb
CHANGED
@@ -14,16 +14,51 @@ class Loggerstash
|
|
14
14
|
#
|
15
15
|
class AlreadyRunningError < Error; end
|
16
16
|
|
17
|
+
# Set the formatter proc to a new proc.
|
18
|
+
#
|
19
|
+
# The passed in proc must take four arguments: `severity`, `timestamp`,
|
20
|
+
# `progname` and `message`. `timestamp` is a `Time`, all over arguments
|
21
|
+
# are `String`s, and `progname` can possibly be `nil`. It must return a
|
22
|
+
# Hash containing the parameters you wish to send to logstash.
|
23
|
+
#
|
17
24
|
attr_writer :formatter
|
18
25
|
|
19
|
-
|
26
|
+
# A new Loggerstash!
|
27
|
+
#
|
28
|
+
# @param logstash_server [String] an address:port, hostname:port, or srvname
|
29
|
+
# to which a `json_lines` logstash connection can be made.
|
30
|
+
# @param metrics_registry [Prometheus::Client::Registry] where the metrics
|
31
|
+
# which are used by the underlying `LogstashWriter` should be registered,
|
32
|
+
# for later presentation by the Prometheus client.
|
33
|
+
# @param formatter [Proc] a formatting proc which takes the same arguments
|
34
|
+
# as the standard `Logger` formatter, but rather than emitting a string,
|
35
|
+
# it should pass back a Hash containing all the fields you wish to send
|
36
|
+
# to logstash.
|
37
|
+
# @param logstash_writer [LogstashWriter] in the event that you've already
|
38
|
+
# got a LogstashWriter instance configured, you can pass it in here. Note
|
39
|
+
# that any values you've set for logstash_server and metrics_registry
|
40
|
+
# will be ignored.
|
41
|
+
#
|
42
|
+
def initialize(logstash_server:, metrics_registry: nil, formatter: nil, logstash_writer: nil)
|
20
43
|
@logstash_server = logstash_server
|
21
44
|
@metrics_registry = metrics_registry
|
22
45
|
@formatter = formatter
|
46
|
+
@logstash_writer = logstash_writer
|
23
47
|
|
24
48
|
@op_mutex = Mutex.new
|
25
49
|
end
|
26
50
|
|
51
|
+
# Associate this Loggerstash with a Logger (or class of Loggers).
|
52
|
+
#
|
53
|
+
# A single Loggerstash instance can be associated with one or more Logger
|
54
|
+
# objects, or all instances of Logger, by attaching the Loggerstash to the
|
55
|
+
# other object (or class). Attaching a Loggerstash means it can no longer
|
56
|
+
# be configured (by the setter methods).
|
57
|
+
#
|
58
|
+
# @param obj [Object] the instance or class to attach this Loggerstash to.
|
59
|
+
# We won't check that you're attaching to an object or class that will
|
60
|
+
# benefit from the attachment; that's up to you to ensure.
|
61
|
+
#
|
27
62
|
def attach(obj)
|
28
63
|
@op_mutex.synchronize do
|
29
64
|
obj.instance_variable_set(:@loggerstash, self)
|
@@ -50,21 +85,36 @@ class Loggerstash
|
|
50
85
|
end
|
51
86
|
end
|
52
87
|
|
88
|
+
# Send a logger message to logstash.
|
89
|
+
#
|
90
|
+
# @private
|
91
|
+
#
|
53
92
|
def log_message(s, t, p, m)
|
54
93
|
@op_mutex.synchronize do
|
55
94
|
if @logstash_writer.nil?
|
95
|
+
#:nocov:
|
56
96
|
run_writer
|
97
|
+
#:nocov:
|
57
98
|
end
|
58
99
|
|
59
100
|
@logstash_writer.send_event((@formatter || default_formatter).call(s, t, p, m))
|
60
101
|
end
|
61
102
|
end
|
103
|
+
|
62
104
|
private
|
63
105
|
|
106
|
+
# Do the needful to get the writer going.
|
107
|
+
#
|
108
|
+
# This will error out unless the @op_mutex is held at the time the
|
109
|
+
# method is called; we can't acquire it ourselves because some calls
|
110
|
+
# to run_writer already need to hold the mutex.
|
111
|
+
#
|
64
112
|
def run_writer
|
65
113
|
unless @op_mutex.owned?
|
114
|
+
#:nocov:
|
66
115
|
raise RuntimeError,
|
67
116
|
"Must call run_writer while holding @op_mutex"
|
117
|
+
#:nocov:
|
68
118
|
end
|
69
119
|
|
70
120
|
if @logstash_writer.nil?
|
@@ -80,6 +130,9 @@ class Loggerstash
|
|
80
130
|
end
|
81
131
|
end
|
82
132
|
|
133
|
+
# Mangle the standard sev/time/prog/msg set into a minimal logstash
|
134
|
+
# event.
|
135
|
+
#
|
83
136
|
def default_formatter
|
84
137
|
@default_formatter ||= ->(s, t, p, m) do
|
85
138
|
{
|
@@ -92,6 +145,8 @@ class Loggerstash
|
|
92
145
|
end
|
93
146
|
end
|
94
147
|
|
148
|
+
# The methods needed to turn any Logger into a Loggerstash Logger.
|
149
|
+
#
|
95
150
|
module Mixin
|
96
151
|
private
|
97
152
|
|
@@ -104,11 +159,20 @@ class Loggerstash
|
|
104
159
|
super
|
105
160
|
end
|
106
161
|
|
162
|
+
# Find where our associated Loggerstash object is being held captive.
|
163
|
+
#
|
164
|
+
# We're kinda reimplementing Ruby's method lookup logic here, but there's
|
165
|
+
# no other way to store our object *somewhere* in the object + class
|
166
|
+
# hierarchy and still be able to get at it from a module (class variables
|
167
|
+
# don't like being accessed from modules).
|
168
|
+
#
|
107
169
|
def loggerstash
|
108
170
|
([self] + self.class.ancestors).find { |m| m.instance_variable_defined?(:@loggerstash) }.instance_variable_get(:@loggerstash).tap do |ls|
|
109
171
|
if ls.nil?
|
172
|
+
#:nocov:
|
110
173
|
raise RuntimeError,
|
111
174
|
"Cannot find loggerstash instance. CAN'T HAPPEN."
|
175
|
+
#:nocov:
|
112
176
|
end
|
113
177
|
end
|
114
178
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: loggerstash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Palmer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-10-
|
11
|
+
date: 2018-10-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: logstash_writer
|
@@ -210,6 +210,7 @@ files:
|
|
210
210
|
- CONTRIBUTING.md
|
211
211
|
- LICENCE
|
212
212
|
- README.md
|
213
|
+
- lib/filtered_debug_logger.rb
|
213
214
|
- lib/loggerstash.rb
|
214
215
|
- loggerstash.gemspec
|
215
216
|
homepage: https://github.com/discourse/loggerstash
|