Class: RrxLogging::Formatter
- Inherits:
-
Object
- Object
- RrxLogging::Formatter
- Defined in:
- lib/rrx_logging/formatter.rb
Constant Summary collapse
- MAX_MESSAGE_LENGTH =
2000
Instance Attribute Summary collapse
-
#default_tags ⇒ Object
readonly
Returns the value of attribute default_tags.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
Instance Method Summary collapse
- #call(severity, timestamp, progname, msg) ⇒ Object
- #clear_tags! ⇒ Object
- #current_tags ⇒ Hash
- #current_tags=(val) ⇒ Object
-
#initialize(mode = :json, tags: nil) ⇒ Formatter
constructor
A new instance of Formatter.
- #thread_key ⇒ String
- #with_tags(**tags) ⇒ Object
Constructor Details
#initialize(mode = :json, tags: nil) ⇒ Formatter
Returns a new instance of Formatter.
7 8 9 10 11 |
# File 'lib/rrx_logging/formatter.rb', line 7 def initialize(mode = :json, tags: nil) @mode = mode @default_tags = || {} super() end |
Instance Attribute Details
#default_tags ⇒ Object (readonly)
Returns the value of attribute default_tags.
5 6 7 |
# File 'lib/rrx_logging/formatter.rb', line 5 def @default_tags end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
5 6 7 |
# File 'lib/rrx_logging/formatter.rb', line 5 def mode @mode end |
Instance Method Details
#call(severity, timestamp, progname, msg) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rrx_logging/formatter.rb', line 17 def call(severity, , progname, msg) msg = msg.to_s.strip if @mode == :json data = { time: , level: severity, log: msg } data[:name] = progname if progname data[:log] = msg.truncate(MAX_MESSAGE_LENGTH) if msg.length > MAX_MESSAGE_LENGTH data.merge!() "#{data.to_json}\n" else = .map { |k, v| "[#{k}=#{v}]" } "%s %s %s %s%s%s\n" % [ .strftime('%Y-%m-%d'), .strftime('%H:%M:%S.%L'), severity, progname ? "#{progname}: " : '', msg, .empty? ? '' : ' %s' % .join('') ] end end |
#clear_tags! ⇒ Object
51 52 53 |
# File 'lib/rrx_logging/formatter.rb', line 51 def .clear end |
#current_tags ⇒ Hash
56 57 58 |
# File 'lib/rrx_logging/formatter.rb', line 56 def Thread.current[thread_key] ||= @default_tags end |
#current_tags=(val) ⇒ Object
61 62 63 |
# File 'lib/rrx_logging/formatter.rb', line 61 def (val) Thread.current[thread_key] = val end |
#thread_key ⇒ String
66 67 68 69 |
# File 'lib/rrx_logging/formatter.rb', line 66 def thread_key # We use our object ID here to avoid conflicting with other instances @thread_key ||= "activesupport_tagged_logging_tags:#{object_id}" end |
#with_tags(**tags) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/rrx_logging/formatter.rb', line 43 def (**) = self. = .merge() yield self ensure self. = end |