slack_log_device 5.0.0 → 5.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.mdown +18 -2
- data/VERSION +1 -1
- data/lib/slack_log_device.rb +5 -4
- data/lib/slack_log_device/formatter.rb +59 -12
- data/lib/slack_log_device/formatter/message.rb +22 -0
- 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: 3aa26183c60229ce1a1102f4392082150a585cfc57f637e2241596ce4bb4f289
|
4
|
+
data.tar.gz: 862b27ad34b7c77c5c8fd4f3d04a0228420462f3ebd2cf77593af8c8e044f673
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d6460466cd2b2e71b43a8db275105fd36d6d92e7dcac1b4a2f8935f27f6ea1eeeacdbafa9cb9827dd914f07369e499f924c79d60a9dca898813ec8461650b94
|
7
|
+
data.tar.gz: 95f9bf541bc5c76be65e876d6c7cb1dde41f66e381da3fcf6693e56e0100f9bb0d60fa07f64aa1940fce85efdd67b280f82fa465796c4cec21db68b3c1c4d815
|
data/README.mdown
CHANGED
@@ -34,8 +34,6 @@ by default).
|
|
34
34
|
- `channel`: The channel to post message on (webhook configured channel by
|
35
35
|
default). It can be a channel (if starting with `#`) or a specific user (if
|
36
36
|
starting with a `@`).
|
37
|
-
- `disable_default_metadata`: `true` to disable default metadata (User,
|
38
|
-
Machine and PID). This is `false` (enabled) by default.
|
39
37
|
- `flush_delay`: The delay in seconds to send buffered messages (1 by
|
40
38
|
default).
|
41
39
|
- `max_buffer_size`: The max messages count to flush them (10 messages by
|
@@ -72,6 +70,9 @@ By default, formatter adds those metadata:
|
|
72
70
|
- `User`: The current user (`ENV['USER']`).
|
73
71
|
- `Machine`: The machine name (`hostname`).
|
74
72
|
|
73
|
+
To disable default metadata (User, Machine and PID), set
|
74
|
+
`disable_default_metadata` formatter option to `true`.
|
75
|
+
|
75
76
|
You can also add custom metadata to message sent to slack, here is how to do
|
76
77
|
it:
|
77
78
|
|
@@ -107,6 +108,21 @@ If you need more, use `:extra_metadata` option. Note that blocks specified
|
|
107
108
|
with `:extra_metadata` option are invoked with options that may contains
|
108
109
|
`:request` option (if present).
|
109
110
|
|
111
|
+
### Icon emojis
|
112
|
+
|
113
|
+
By default, there is a different icon emoji for each logging severity level.
|
114
|
+
But, it can be configured:
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
logger.formatter = SlackLogDevice.formatter(max_backtrace_lines: 50, icon_emojis: { fatal: ':metal:', warn: ':skull:' })
|
118
|
+
```
|
119
|
+
|
120
|
+
Or for all severity levels:
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
logger.formatter = SlackLogDevice.formatter(max_backtrace_lines: 50, icon_emoji: ':metal:')
|
124
|
+
```
|
125
|
+
|
110
126
|
## Rails configuration
|
111
127
|
|
112
128
|
For a rails application, it is recommanded to use following configuration into
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
5.
|
1
|
+
5.1.0
|
data/lib/slack_log_device.rb
CHANGED
@@ -54,13 +54,15 @@ class SlackLogDevice
|
|
54
54
|
|
55
55
|
def flush
|
56
56
|
while !@buffer.empty? do
|
57
|
-
message =
|
57
|
+
message = nil
|
58
58
|
@mutex.synchronize do
|
59
59
|
message = @buffer.pop
|
60
60
|
end
|
61
|
-
|
62
|
-
|
61
|
+
text = message.to_s.strip
|
62
|
+
next if text.empty?
|
63
|
+
data = { 'text' => text }
|
63
64
|
data['channel'] = channel if channel.present?
|
65
|
+
data['icon_emoji'] = message.icon_emoji if message.respond_to?(:icon_emoji) && message.icon_emoji.present?
|
64
66
|
data['username'] = username if username.present?
|
65
67
|
begin
|
66
68
|
HTTParty.post(webhook_url, body: data.to_json, headers: { 'Content-Type' => 'application/json' }, timeout: timeout)
|
@@ -105,7 +107,6 @@ class SlackLogDevice
|
|
105
107
|
end
|
106
108
|
|
107
109
|
def write(message)
|
108
|
-
message = message.to_s.try(:strip)
|
109
110
|
return if message.blank?
|
110
111
|
@mutex.synchronize do
|
111
112
|
@buffer << message
|
@@ -2,15 +2,28 @@ class SlackLogDevice
|
|
2
2
|
|
3
3
|
class Formatter
|
4
4
|
|
5
|
+
DEFAULT_ICON_EMOJIS = {
|
6
|
+
'DEBUG' => ':bug:',
|
7
|
+
'INFO' => ':information_source:',
|
8
|
+
'WARN' => ':warning:',
|
9
|
+
'ERROR' => ':x:',
|
10
|
+
'FATAL' => ':fire:',
|
11
|
+
'UNKNOWN' => ':interrobang:',
|
12
|
+
}.freeze
|
5
13
|
MAX_MESSAGE_LENGTH = 4000
|
6
14
|
|
7
15
|
attr_reader :extra_metadata, :max_backtrace_lines
|
8
16
|
|
9
17
|
def initialize(options = {}, &block)
|
10
|
-
options.assert_valid_keys(:disable_default_metadata, :extra_metadata, :max_backtrace_lines)
|
18
|
+
options.assert_valid_keys(:disable_default_metadata, :extra_metadata, :icon_emoji, :icon_emojis, :max_backtrace_lines)
|
19
|
+
self.disable_default_metadata = options[:disable_default_metadata]
|
11
20
|
self.extra_metadata = options.key?(:extra_metadata) ? options[:extra_metadata] : {}
|
12
21
|
self.max_backtrace_lines = options.key?(:max_backtrace_lines) ? options[:max_backtrace_lines] : 10
|
13
|
-
|
22
|
+
|
23
|
+
@icon_emojis = DEFAULT_ICON_EMOJIS.dup
|
24
|
+
self.icon_emojis = options[:icon_emojis] if options.key?(:icon_emojis)
|
25
|
+
self.icon_emoji = options[:icon_emoji] if options.key?(:icon_emoji)
|
26
|
+
|
14
27
|
@message_converter = block_given? ? Proc.new(&block) : -> (message) { message }
|
15
28
|
end
|
16
29
|
|
@@ -29,11 +42,47 @@ class SlackLogDevice
|
|
29
42
|
text << " #{convert_message(message)}".rstrip
|
30
43
|
text = append_metadata(text, message)
|
31
44
|
end
|
32
|
-
truncate(text)
|
45
|
+
Message.new(truncate(text), icon_emoji: icon_emoji(severity))
|
46
|
+
end
|
47
|
+
|
48
|
+
def disable_default_metadata=(value)
|
49
|
+
@disable_default_metadata = value.present?
|
33
50
|
end
|
34
51
|
|
35
52
|
def disable_default_metadata?
|
36
|
-
@disable_default_metadata
|
53
|
+
@disable_default_metadata
|
54
|
+
end
|
55
|
+
|
56
|
+
def extra_metadata=(value)
|
57
|
+
@extra_metadata = (value.presence || {})
|
58
|
+
end
|
59
|
+
|
60
|
+
def icon_emoji(severity)
|
61
|
+
@icon_emojis[parse_severity(severity)]
|
62
|
+
end
|
63
|
+
|
64
|
+
def icon_emoji=(value)
|
65
|
+
value = value.to_s.strip.presence
|
66
|
+
@icon_emojis.keys.each do |severity|
|
67
|
+
@icon_emojis[severity] = value
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def icon_emojis
|
72
|
+
@icon_emojis.freeze
|
73
|
+
end
|
74
|
+
|
75
|
+
def icon_emojis=(values = {})
|
76
|
+
values.each do |severity, emoji|
|
77
|
+
@icon_emojis[parse_severity(severity)] = emoji.to_s.strip.presence
|
78
|
+
end
|
79
|
+
@icon_emojis
|
80
|
+
end
|
81
|
+
|
82
|
+
def max_backtrace_lines=(value)
|
83
|
+
length = Integer(value) rescue nil
|
84
|
+
raise ArgumentError.new("Invalid max backtrace lines: #{value.inspect}") if length.nil? || length < -1
|
85
|
+
@max_backtrace_lines = length
|
37
86
|
end
|
38
87
|
|
39
88
|
private
|
@@ -83,10 +132,6 @@ class SlackLogDevice
|
|
83
132
|
metadata
|
84
133
|
end
|
85
134
|
|
86
|
-
def extra_metadata=(value)
|
87
|
-
@extra_metadata = value.presence || {}
|
88
|
-
end
|
89
|
-
|
90
135
|
def format_backtrace(exception, size_available)
|
91
136
|
return nil if max_backtrace_lines == 0 || size_available < 7
|
92
137
|
backtrace = (exception.backtrace || []).select(&:present?).compact
|
@@ -114,10 +159,10 @@ class SlackLogDevice
|
|
114
159
|
truncate(text, size_available)
|
115
160
|
end
|
116
161
|
|
117
|
-
def
|
118
|
-
|
119
|
-
|
120
|
-
|
162
|
+
def parse_severity(value)
|
163
|
+
severity = value.to_s.strip.upcase
|
164
|
+
return severity if DEFAULT_ICON_EMOJIS.key?(severity)
|
165
|
+
raise("Invalid log severity: #{value.inspect}")
|
121
166
|
end
|
122
167
|
|
123
168
|
def truncate(message, max_length = MAX_MESSAGE_LENGTH)
|
@@ -130,3 +175,5 @@ class SlackLogDevice
|
|
130
175
|
end
|
131
176
|
|
132
177
|
end
|
178
|
+
|
179
|
+
require "#{__dir__}/formatter/message"
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class SlackLogDevice
|
2
|
+
|
3
|
+
class Formatter
|
4
|
+
|
5
|
+
class Message < String
|
6
|
+
|
7
|
+
attr_reader :icon_emoji
|
8
|
+
|
9
|
+
def initialize(text, icon_emoji: nil)
|
10
|
+
super(text)
|
11
|
+
self.icon_emoji = icon_emoji
|
12
|
+
end
|
13
|
+
|
14
|
+
def icon_emoji=(value)
|
15
|
+
@icon_emoji = value.presence.try(&:strip)
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slack_log_device
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 5.
|
4
|
+
version: 5.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexis Toulotte
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-02-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -122,6 +122,7 @@ files:
|
|
122
122
|
- lib/slack_log_device.rb
|
123
123
|
- lib/slack_log_device/debug_exceptions.rb
|
124
124
|
- lib/slack_log_device/formatter.rb
|
125
|
+
- lib/slack_log_device/formatter/message.rb
|
125
126
|
- lib/slack_log_device/set_request_in_thread.rb
|
126
127
|
- slack_log_device.gemspec
|
127
128
|
homepage: https://github.com/alexistoulotte/slack_log_device
|