Class: Syslogger::SimpleFormatter

Inherits:
Logger::Formatter
  • Object
show all
Defined in:
lib/syslogger.rb

Overview

Instance Method Summary collapse

Instance Method Details

#call(_severity, _timestamp, _progname, msg) ⇒ Object

This method is invoked when a log event occurs.



171
172
173
# File 'lib/syslogger.rb', line 171

def call(_severity, _timestamp, _progname, msg)
  "#{tags_text}#{msg}"
end

#clear_tags!Object



192
193
194
# File 'lib/syslogger.rb', line 192

def clear_tags!
  current_tags.clear
end

#current_tagsObject



198
199
200
201
202
# File 'lib/syslogger.rb', line 198

def current_tags
  # We use our object ID here to avoid conflicting with other instances
  thread_key = @thread_key ||= "syslogger_tagged_logging_tags:#{object_id}".freeze
  Thread.current[thread_key] ||= []
end

#pop_tags(size = 1) ⇒ Object



188
189
190
# File 'lib/syslogger.rb', line 188

def pop_tags(size = 1)
  current_tags.pop size
end

#push_tags(*tags) ⇒ Object



182
183
184
185
186
# File 'lib/syslogger.rb', line 182

def push_tags(*tags)
  tags.flatten.reject { |i| i.respond_to?(:empty?) ? i.empty? : !i }.tap do |new_tags|
    current_tags.concat(new_tags).uniq!
  end
end

#tagged(*tags) ⇒ Object



175
176
177
178
179
180
# File 'lib/syslogger.rb', line 175

def tagged(*tags)
  new_tags = push_tags(*tags)
  yield self
ensure
  pop_tags(new_tags.size)
end

#tags_textObject



204
205
206
207
# File 'lib/syslogger.rb', line 204

def tags_text
  tags = current_tags
  tags.collect { |tag| "[#{tag}] " }.join if tags.any?
end