Class: RSMP::Logger
- Inherits:
-
Object
show all
- Includes:
- Colorization, Filtering
- Defined in:
- lib/rsmp/log/logger.rb,
lib/rsmp/log/filtering.rb,
lib/rsmp/log/colorization.rb
Defined Under Namespace
Modules: Colorization, Filtering
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#add_connection_parts(parts, item) ⇒ Object
-
#add_message_parts(parts, item) ⇒ Object
-
#add_metadata_parts(parts, item) ⇒ Object
-
#add_output_parts(parts, item) ⇒ Object
-
#apply_default_lengths(settings) ⇒ Object
-
#build_output(item) ⇒ Object
-
#build_part(parts, item, key, &block) ⇒ Object
-
#default_field_lengths ⇒ Object
-
#default_field_settings ⇒ Object
-
#default_logger_settings ⇒ Object
-
#default_output_settings ⇒ Object
-
#dump(archive, num: nil) ⇒ Object
-
#ignorable_messages ⇒ Object
-
#initialize(settings = {}) ⇒ Logger
constructor
A new instance of Logger.
-
#level_enabled?(item) ⇒ Boolean
-
#log(item, force: false) ⇒ Object
-
#mute(ip, port) ⇒ Object
-
#muted?(item) ⇒ Boolean
-
#output(level, str) ⇒ Object
-
#setup_output_destination ⇒ Object
-
#unmute(ip, port) ⇒ Object
-
#unmute_all ⇒ Object
#apply_hash_colors, #colorize, #colorize_with_map, #default_colors
Methods included from Filtering
#acknowledgement_enabled?, #ignorable?, #message_ignored?, #output?
Constructor Details
#initialize(settings = {}) ⇒ Logger
Returns a new instance of Logger.
83
84
85
86
87
88
89
|
# File 'lib/rsmp/log/logger.rb', line 83
def initialize(settings = {})
@ignorable = ignorable_messages
@settings = settings ? default_logger_settings.merge(settings) : default_logger_settings
@settings = apply_default_lengths(@settings)
@muted = {}
setup_output_destination
end
|
Instance Attribute Details
#settings ⇒ Object
Returns the value of attribute settings.
6
7
8
|
# File 'lib/rsmp/log/logger.rb', line 6
def settings
@settings
end
|
Class Method Details
.shorten_message_id(m_id, length = 4) ⇒ Object
140
141
142
143
144
145
146
|
# File 'lib/rsmp/log/logger.rb', line 140
def self.shorten_message_id(m_id, length = 4)
if m_id
m_id[0..(length - 1)].ljust(length)
else
' ' * length
end
end
|
Instance Method Details
#add_connection_parts(parts, item) ⇒ Object
178
179
180
181
182
183
|
# File 'lib/rsmp/log/logger.rb', line 178
def add_connection_parts(parts, item)
build_part(parts, item, :ip)
build_part(parts, item, :port)
build_part(parts, item, :site_id)
build_part(parts, item, :component)
end
|
#add_message_parts(parts, item) ⇒ Object
185
186
187
188
189
190
191
192
|
# File 'lib/rsmp/log/logger.rb', line 185
def add_message_parts(parts, item)
build_part(parts, item, :direction) { |part| { in: 'In', out: 'Out' }[part] }
build_part(parts, item, :level, &:capitalize)
build_part(parts, item, :id) { Logger.shorten_message_id(item[:message].m_id, 4) if item[:message] }
build_part(parts, item, :text)
build_part(parts, item, :json) { item[:message]&.json }
build_part(parts, item, :exception) { |e| [e.class, e.backtrace].flatten.join("\n") }
end
|
171
172
173
174
175
176
|
# File 'lib/rsmp/log/logger.rb', line 171
def add_metadata_parts(parts, item)
build_part(parts, item, :prefix) { @settings['prefix'] if @settings['prefix'] != false }
build_part(parts, item, :index)
build_part(parts, item, :author)
build_part(parts, item, :timestamp) { |part| Clock.to_s part }
end
|
#add_output_parts(parts, item) ⇒ Object
194
195
196
197
198
|
# File 'lib/rsmp/log/logger.rb', line 194
def add_output_parts(parts, item)
add_metadata_parts(parts, item)
add_connection_parts(parts, item)
add_message_parts(parts, item)
end
|
#apply_default_lengths(settings) ⇒ Object
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/rsmp/log/logger.rb', line 72
def apply_default_lengths(settings)
lengths = default_field_lengths
settings.to_h do |key, value|
if value == true && lengths[key]
[key, lengths[key]]
else
[key, value]
end
end
end
|
#build_output(item) ⇒ Object
200
201
202
203
204
|
# File 'lib/rsmp/log/logger.rb', line 200
def build_output(item)
parts = []
add_output_parts(parts, item)
parts.join(' ').chomp(@settings['tabs'].to_s).rstrip
end
|
#build_part(parts, item, key, &block) ⇒ Object
157
158
159
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/rsmp/log/logger.rb', line 157
def build_part(parts, item, key, &block)
skey = key.to_s
return unless @settings[skey]
part = item[key]
part = yield part if block
part = part.to_s
part = part.ljust @settings[skey] if @settings[skey].is_a?(Integer)
part = @settings['tabs'].ljust(part.length) if @settings['tabs'] && part !~ /\S/
parts << part
end
|
#default_field_lengths ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/rsmp/log/logger.rb', line 46
def default_field_lengths
{
'index' => 7,
'author' => 13,
'timestamp' => 24,
'ip' => 22,
'port' => 5,
'site_id' => 19,
'component' => 19,
'direction' => 3,
'level' => 7,
'id' => 4
}
end
|
#default_field_settings ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/rsmp/log/logger.rb', line 25
def default_field_settings
{
'prefix' => false,
'index' => false,
'author' => false,
'timestamp' => true,
'ip' => false,
'port' => false,
'site_id' => true,
'component' => true,
'direction' => false,
'level' => false,
'id' => true,
'text' => true
}
end
|
#default_logger_settings ⇒ Object
42
43
44
|
# File 'lib/rsmp/log/logger.rb', line 42
def default_logger_settings
default_output_settings.merge(default_field_settings)
end
|
#default_output_settings ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/rsmp/log/logger.rb', line 8
def default_output_settings
{
'active' => true,
'path' => nil,
'stream' => nil,
'color' => true,
'debug' => false,
'statistics' => false,
'hide_ip_and_port' => false,
'acknowledgements' => false,
'watchdogs' => false,
'alarms' => true,
'json' => false,
'tabs' => '-'
}
end
|
#dump(archive, num: nil) ⇒ Object
148
149
150
151
152
153
154
155
|
# File 'lib/rsmp/log/logger.rb', line 148
def dump(archive, num: nil)
num ||= archive.items.size
log = archive.items.last(num).map do |item|
str = build_output item
colorize item[:level], str
end
log.join("\n")
end
|
#ignorable_messages ⇒ Object
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/rsmp/log/logger.rb', line 61
def ignorable_messages
{
'versions' => ['Version'],
'statuses' => %w[StatusRequest StatusSubscribe StatusUnsubscribe StatusResponse StatusUpdate],
'commands' => %w[CommandRequest CommandResponse],
'watchdogs' => 'Watchdog',
'alarms' => ['Alarm'],
'aggregated_status' => %w[AggregatedStatus AggregatedStatusRequest]
}
end
|
#level_enabled?(item) ⇒ Boolean
117
118
119
120
121
122
123
124
|
# File 'lib/rsmp/log/logger.rb', line 117
def level_enabled?(item)
return false if @settings['info'] == false && item[:level] == :info
return false if @settings['debug'] != true && item[:level] == :debug
return false if @settings['statistics'] != true && item[:level] == :statistics
return false if @settings['test'] != true && item[:level] == :test
true
end
|
#log(item, force: false) ⇒ Object
134
135
136
137
138
|
# File 'lib/rsmp/log/logger.rb', line 134
def log(item, force: false)
return unless output?(item, force: force)
output item[:level], build_output(item)
end
|
#mute(ip, port) ⇒ Object
101
102
103
|
# File 'lib/rsmp/log/logger.rb', line 101
def mute(ip, port)
@muted["#{ip}:#{port}"] = true
end
|
#muted?(item) ⇒ Boolean
113
114
115
|
# File 'lib/rsmp/log/logger.rb', line 113
def muted?(item)
item[:ip] && item[:port] && @muted["#{item[:ip]}:#{item[:port]}"]
end
|
#output(level, str) ⇒ Object
126
127
128
129
130
131
132
|
# File 'lib/rsmp/log/logger.rb', line 126
def output(level, str)
return if str.empty? || /^\s+$/.match(str)
str = colorize level, str
@stream.puts str
@stream.flush
end
|
#setup_output_destination ⇒ Object
91
92
93
94
95
96
97
98
99
|
# File 'lib/rsmp/log/logger.rb', line 91
def setup_output_destination
@stream = if @settings['stream']
@settings['stream']
elsif @settings['path']
File.open(@settings['path'], 'a') else
$stdout
end
end
|
#unmute(ip, port) ⇒ Object
105
106
107
|
# File 'lib/rsmp/log/logger.rb', line 105
def unmute(ip, port)
@muted.delete "#{ip}:#{port}"
end
|
#unmute_all ⇒ Object
109
110
111
|
# File 'lib/rsmp/log/logger.rb', line 109
def unmute_all
@muted = {}
end
|