Class: RSMP::Logger

Inherits:
Object
  • Object
show all
Includes:
Colorization, Filtering
Defined in:
lib/rsmp/log/logger.rb,
lib/rsmp/log/filtering.rb,
lib/rsmp/log/colorization.rb

Overview

Formats and outputs log messages according to configured settings.

Defined Under Namespace

Modules: Colorization, Filtering

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Colorization

#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.



84
85
86
87
88
89
90
# File 'lib/rsmp/log/logger.rb', line 84

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

#settingsObject

Returns the value of attribute settings.



7
8
9
# File 'lib/rsmp/log/logger.rb', line 7

def settings
  @settings
end

Class Method Details

.shorten_message_id(m_id, length = 4) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/rsmp/log/logger.rb', line 141

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



179
180
181
182
183
184
# File 'lib/rsmp/log/logger.rb', line 179

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



186
187
188
189
190
191
192
193
# File 'lib/rsmp/log/logger.rb', line 186

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

#add_metadata_parts(parts, item) ⇒ Object



172
173
174
175
176
177
# File 'lib/rsmp/log/logger.rb', line 172

def (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



195
196
197
198
199
# File 'lib/rsmp/log/logger.rb', line 195

def add_output_parts(parts, item)
  (parts, item)
  add_connection_parts(parts, item)
  add_message_parts(parts, item)
end

#apply_default_lengths(settings) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/rsmp/log/logger.rb', line 73

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



201
202
203
204
205
# File 'lib/rsmp/log/logger.rb', line 201

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



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/rsmp/log/logger.rb', line 158

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)

  # replace the first char with a dash if string is all whitespace
  part = @settings['tabs'].ljust(part.length) if @settings['tabs'] && part !~ /\S/
  parts << part
end

#default_field_lengthsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rsmp/log/logger.rb', line 47

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_settingsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rsmp/log/logger.rb', line 26

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_settingsObject



43
44
45
# File 'lib/rsmp/log/logger.rb', line 43

def default_logger_settings
  default_output_settings.merge(default_field_settings)
end

#default_output_settingsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rsmp/log/logger.rb', line 9

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



149
150
151
152
153
154
155
156
# File 'lib/rsmp/log/logger.rb', line 149

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_messagesObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/rsmp/log/logger.rb', line 62

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

Returns:

  • (Boolean)


118
119
120
121
122
123
124
125
# File 'lib/rsmp/log/logger.rb', line 118

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



135
136
137
138
139
# File 'lib/rsmp/log/logger.rb', line 135

def log(item, force: false)
  return unless output?(item, force: force)

  output item[:level], build_output(item)
end

#mute(ip, port) ⇒ Object



102
103
104
# File 'lib/rsmp/log/logger.rb', line 102

def mute(ip, port)
  @muted["#{ip}:#{port}"] = true
end

#muted?(item) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/rsmp/log/logger.rb', line 114

def muted?(item)
  item[:ip] && item[:port] && @muted["#{item[:ip]}:#{item[:port]}"]
end

#output(level, str) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/rsmp/log/logger.rb', line 127

def output(level, str)
  return if str.empty? || /^\s+$/.match(str)

  str = colorize level, str
  @stream.puts str
  @stream.flush
end

#setup_output_destinationObject



92
93
94
95
96
97
98
99
100
# File 'lib/rsmp/log/logger.rb', line 92

def setup_output_destination
  @stream = if @settings['stream']
              @settings['stream']
            elsif @settings['path']
              File.open(@settings['path'], 'a') # appending
            else
              $stdout
            end
end

#unmute(ip, port) ⇒ Object



106
107
108
# File 'lib/rsmp/log/logger.rb', line 106

def unmute(ip, port)
  @muted.delete "#{ip}:#{port}"
end

#unmute_allObject



110
111
112
# File 'lib/rsmp/log/logger.rb', line 110

def unmute_all
  @muted = {}
end