class Xyeger::Formatters::Base

Constants

UNCOLORIZE_REGEXP

Attributes

attributes[R]
colors[R]

Public Class Methods

new(attributes = {}) click to toggle source
# File lib/xyeger/formatters/base.rb, line 10
def initialize(attributes = {})
  @attributes = attributes
  @tags = []
  @colors = Array.new(9) { Paint.random } if attributes[:colored]
end

Public Instance Methods

call(severity, timestamp, context, message) click to toggle source
# File lib/xyeger/formatters/base.rb, line 16
def call(severity, timestamp, context, message)
  message, context = prepare(message, context)
  message = uncolorize(message) unless attributes[:colored]

  context = filter_context(context)

  result = {
    hostname: Xyeger.config.hostname,
    pid: $$,
    app: Xyeger.config.app,
    env: Xyeger.config.env,
    level: severity,
    time: timestamp,
    message: message,
    context: context
  }

  result[:tags] = current_tags if current_tags.any?
  colored(result) if attributes[:colored]

  result
end
uncolorize(message) click to toggle source
# File lib/xyeger/formatters/base.rb, line 67
def uncolorize(message)
  message.to_s.gsub(UNCOLORIZE_REGEXP, '')
end

Private Instance Methods

colored(result) click to toggle source
# File lib/xyeger/formatters/base.rb, line 55
        def colored(result)
  result.each_with_index do |(key, value), index|
    result[key] = Paint[value, colors[index]]
  end
end
filter_context(context) click to toggle source
# File lib/xyeger/formatters/base.rb, line 61
        def filter_context(context)
  return context unless Xyeger.config.filter && context.is_a?(Hash)

  Xyeger.config.filter.filter(context)
end
prepare(message, context) click to toggle source
# File lib/xyeger/formatters/base.rb, line 39
        def prepare(message, context)
  new_message = attributes[:message].call(message, context) if attributes[:message]
  new_context = attributes[:context].call(message, context) if attributes[:context]

  return [new_message, new_context] if attributes[:message] || attributes[:context]

  case message
  when LogrageRaw
    ['Lograge', message.data]
  when ::StandardError
    ['StandardError', { class: message.class.name, error: message.to_s }]
  else
    [message.to_s, context]
  end
end