Class: Clearsale::LoggerFormatterFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/clearsale/logger_formatter_filter.rb

Constant Summary collapse

OPEN_TAG =
lambda{ |tag_name| "<#{tag_name}>"   }
CLOSE_TAG =
lambda{ |tag_name| "<\/#{tag_name}>" }
TAG_REGEXP =
lambda{ |tag_name, content| "#{OPEN_TAG.call(tag_name)}#{content}#{CLOSE_TAG.call(tag_name)}" }

Instance Method Summary collapse

Constructor Details

#initialize(tags_to_filter = [], &block) ⇒ LoggerFormatterFilter

Returns a new instance of LoggerFormatterFilter.



15
16
17
18
# File 'lib/clearsale/logger_formatter_filter.rb', line 15

def initialize(tags_to_filter = [], &block)
  @tags_to_filter = tags_to_filter
  @block = block
end

Instance Method Details

#call(severity, datetime, progname, msg) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/clearsale/logger_formatter_filter.rb', line 20

def call(severity, datetime, progname, msg)
  if @block
    @block.call(severity, datetime, progname, filter(msg))
  else
    "#{severity} #{datetime} --#{progname}: #{filter(msg)}"
  end
end

#filter(msg) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/clearsale/logger_formatter_filter.rb', line 6

def filter(msg)
  return msg if !msg.respond_to?(:gsub) || @tags_to_filter.nil?

  @tags_to_filter.each do |tag|
    msg = msg.gsub(%r{#{TAG_REGEXP.call(tag,'.*')}}, TAG_REGEXP.call(tag,"[FILTERED]"))
  end
  msg
end