Class: FluentLoggerRails::TaggedHashFormatter
- Inherits:
-
Logger::Formatter
- Object
- Logger::Formatter
- FluentLoggerRails::TaggedHashFormatter
- Defined in:
- lib/fluent_logger_rails/tagged_hash_formatter.rb
Instance Attribute Summary collapse
-
#parent_key ⇒ Object
Parent key - ensures that if the log is being merged into a higher level payload this key can be set in order to not conflict or be overridden.
Instance Method Summary collapse
- #add_tags(*tags) ⇒ Object
- #call(severity, timestamp, _progname, msg) ⇒ Object
- #clear_tags! ⇒ Object
- #compact_tags ⇒ Object
- #current_tags ⇒ Object
- #format_severity(severity) ⇒ Object
-
#initialize(merge_message_into_payload = false) ⇒ TaggedHashFormatter
constructor
A new instance of TaggedHashFormatter.
- #remove_tags(*tags) ⇒ Object
- #tagged(*tags) ⇒ Object
Constructor Details
#initialize(merge_message_into_payload = false) ⇒ TaggedHashFormatter
Returns a new instance of TaggedHashFormatter.
9 10 11 12 13 14 |
# File 'lib/fluent_logger_rails/tagged_hash_formatter.rb', line 9 def initialize( = false) super() @merge_message_into_payload = # Override the Logger::Formatter default to a format that does not have a trailing space @datetime_format = "%Y-%m-%dT%H:%M:%S.%6N" end |
Instance Attribute Details
#parent_key ⇒ Object
Parent key - ensures that if the log is being merged into a higher level payload this key can be set in order to not conflict or be overridden.
7 8 9 |
# File 'lib/fluent_logger_rails/tagged_hash_formatter.rb', line 7 def parent_key @parent_key end |
Instance Method Details
#add_tags(*tags) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/fluent_logger_rails/tagged_hash_formatter.rb', line 54 def (*) = .first if .length == 1 if .is_a? Array [:tags] ||= [] [:tags] += elsif .is_a? String [:tags] ||= [] [:tags] << else .merge! end end |
#call(severity, timestamp, _progname, msg) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/fluent_logger_rails/tagged_hash_formatter.rb', line 16 def call(severity, , _progname, msg) payload = if msg.is_a?(Hash) && @merge_message_into_payload msg else { message: msg.is_a?(String) ? msg.strip : msg, } end payload = payload.merge( { severity: format_severity(severity), timestamp: format_datetime(), }, ).merge(.deep_dup) @parent_key ? { @parent_key => payload } : payload end |
#clear_tags! ⇒ Object
82 83 84 |
# File 'lib/fluent_logger_rails/tagged_hash_formatter.rb', line 82 def .clear end |
#compact_tags ⇒ Object
92 93 94 |
# File 'lib/fluent_logger_rails/tagged_hash_formatter.rb', line 92 def .delete_if { |_k, v| v.blank? } end |
#current_tags ⇒ Object
86 87 88 89 90 |
# File 'lib/fluent_logger_rails/tagged_hash_formatter.rb', line 86 def # We use our object ID here to avoid conflicting with other instances thread_key = @thread_key ||= "fluent_logger_rails:#{object_id}" Thread.current[thread_key] ||= {} end |
#format_severity(severity) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/fluent_logger_rails/tagged_hash_formatter.rb', line 35 def format_severity(severity) if severity.blank? 'ANY' elsif severity.is_a? Integer ActiveSupport::Logger::SEV_LABEL[severity] else severity end end |
#remove_tags(*tags) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/fluent_logger_rails/tagged_hash_formatter.rb', line 68 def (*) = .first if .length == 1 if .is_a? Array [:tags] ||= [] .each { |tag| [:tags].delete(tag) } elsif .is_a? String [:tags] ||= [] [:tags].delete() else .each_key { |key| .delete(key) } end end |
#tagged(*tags) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/fluent_logger_rails/tagged_hash_formatter.rb', line 45 def tagged(*) = .flatten.compact if .is_a?(Array) (*) if .present? yield(self) ensure (*) if .present? end |