slack_log_device 2.1.3 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.mdown +9 -0
- data/VERSION +1 -1
- data/lib/slack_log_device.rb +16 -1
- data/spec/slack_log_device_spec.rb +52 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 825e9f635d6d804f3b3221de1bc7c6f2e8f75702
|
|
4
|
+
data.tar.gz: 4ec127d12b83a824df28e0ed50c979f5dc71049c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2d42f60c686262a582f5b346f0fa46df4129beaa6cf391ee6fe7e76c776e6f854f4b37c6ac52eee7d79b82656dda1947fa5b63e061c70a68086457207a6ff31e
|
|
7
|
+
data.tar.gz: 9746e95b665bc59d0dd0db446bc794163e99d755d6274e213b26b73eb8d84bdcf3edc88522e3a95bb5c4f6d4d5ff8c84756804e7155f0e3db3bb4561bfcffe7e
|
data/README.mdown
CHANGED
|
@@ -40,6 +40,15 @@ starting with a `@`).
|
|
|
40
40
|
- `username`: The username to post message as (nil by default).
|
|
41
41
|
- `webhook_url`: The URL of the webhook (mandatory).
|
|
42
42
|
|
|
43
|
+
## Formatter
|
|
44
|
+
|
|
45
|
+
`slack_log_device` provides a log formatter to have a pretty output for slack.
|
|
46
|
+
It can be configured like this:
|
|
47
|
+
|
|
48
|
+
```ruby
|
|
49
|
+
logger.formatter = SlackLogDevice::FORMATTER
|
|
50
|
+
```
|
|
51
|
+
|
|
43
52
|
## Rails configuration
|
|
44
53
|
|
|
45
54
|
For a rails application, it is recommanded to use following configuration into
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.
|
|
1
|
+
2.2.0
|
data/lib/slack_log_device.rb
CHANGED
|
@@ -6,6 +6,21 @@ require 'logger'
|
|
|
6
6
|
|
|
7
7
|
class SlackLogDevice
|
|
8
8
|
|
|
9
|
+
FORMATTER = -> (severity, datetime, progname, message) do
|
|
10
|
+
text = "*`#{severity}`*"
|
|
11
|
+
text << " (*#{progname}*)" if progname.present?
|
|
12
|
+
text << ': '
|
|
13
|
+
if message.is_a?(Exception)
|
|
14
|
+
text << "A `#{message.class}` occurred: #{message.message}\n\n```"
|
|
15
|
+
backtrace = message.backtrace.join("\n")
|
|
16
|
+
backtrace = backtrace[0, MAX_MESSAGE_LENGTH - 7 - text.size] << "\n..." if backtrace.size > MAX_MESSAGE_LENGTH - 3
|
|
17
|
+
text << backtrace << '```'
|
|
18
|
+
else
|
|
19
|
+
text << message
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
MAX_MESSAGE_LENGTH = 4000
|
|
23
|
+
|
|
9
24
|
attr_reader :channel, :flush_delay, :max_buffer_size, :timeout, :username, :webhook_url
|
|
10
25
|
|
|
11
26
|
def initialize(options = {})
|
|
@@ -59,7 +74,7 @@ class SlackLogDevice
|
|
|
59
74
|
end
|
|
60
75
|
|
|
61
76
|
def flush?
|
|
62
|
-
auto_flush? || flush_delay.zero? || @buffer.join("\n").
|
|
77
|
+
auto_flush? || flush_delay.zero? || @buffer.join("\n").bytesize > max_buffer_size
|
|
63
78
|
end
|
|
64
79
|
|
|
65
80
|
def flush_delay=(value)
|
|
@@ -13,6 +13,50 @@ describe SlackLogDevice do
|
|
|
13
13
|
expect(device).not_to be_a(Logger::LogDevice)
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
+
describe '::FORMATTER' do
|
|
17
|
+
|
|
18
|
+
let(:formatter) { SlackLogDevice::FORMATTER }
|
|
19
|
+
|
|
20
|
+
it 'returns a proc' do
|
|
21
|
+
expect(formatter).to be_a(Proc)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
describe '#call' do
|
|
25
|
+
|
|
26
|
+
it 'returns a formatted message' do
|
|
27
|
+
expect(formatter.call('DEBUG', Time.now, ' ', 'Hello World')).to eq('*`DEBUG`*: Hello World')
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it 'includes progname if given' do
|
|
31
|
+
expect(formatter.call('DEBUG', Time.now, 'My App', 'Hello World')).to eq('*`DEBUG`* (*My App*): Hello World')
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it 'formats exception' do
|
|
35
|
+
exception = RuntimeError.new('BAM!')
|
|
36
|
+
exception.set_backtrace(['foo', 'bar'])
|
|
37
|
+
expect(formatter.call('DEBUG', Time.now, nil, exception)).to eq("*`DEBUG`*: A `RuntimeError` occurred: BAM!\n\n```foo\nbar```")
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it 'message with trace never exceed 4000 chars' do
|
|
41
|
+
exception = RuntimeError.new('BAM!')
|
|
42
|
+
exception.set_backtrace(['a' * 4500])
|
|
43
|
+
message = formatter.call('DEBUG', Time.now, 'My App', exception)
|
|
44
|
+
expect(message.size).to eq(4000)
|
|
45
|
+
expect(message).to end_with("aaaaaa\n...```")
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
it 'can be exactly 4000 chars (without three dots)' do
|
|
49
|
+
exception = RuntimeError.new('BAM!')
|
|
50
|
+
exception.set_backtrace(['a' * 3950])
|
|
51
|
+
message = formatter.call('DEBUG', Time.now, nil, exception)
|
|
52
|
+
expect(message).to end_with('a```')
|
|
53
|
+
expect(message.size).to eq(4000)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
end
|
|
59
|
+
|
|
16
60
|
describe '#auto_flush?' do
|
|
17
61
|
|
|
18
62
|
it 'is false by default' do
|
|
@@ -159,6 +203,14 @@ describe SlackLogDevice do
|
|
|
159
203
|
}.to change { device.flush? }.from(false).to(true)
|
|
160
204
|
end
|
|
161
205
|
|
|
206
|
+
it 'use byte size to compare max_buffer_size' do
|
|
207
|
+
options[:max_buffer_size] = 10
|
|
208
|
+
device.write('0123456é')
|
|
209
|
+
expect {
|
|
210
|
+
device.instance_variable_get(:@buffer).push('a')
|
|
211
|
+
}.to change { device.flush? }.from(false).to(true)
|
|
212
|
+
end
|
|
213
|
+
|
|
162
214
|
it 'is true if auto_flush option is present' do
|
|
163
215
|
expect {
|
|
164
216
|
device.auto_flush = true
|