class Lumberjack::TagFormatter
Class for formatting tags. You can register a default formatter and tag name specific formatters. Formatters can be either `Lumberjack::Formatter` objects or any object that responds to `call`.
tag_formatter = Lumberjack::TagFormatter.new
.default(Lumberjack::Formatter.new
) tag_formatter.add([“password”, “email”]) { |value| “***” } tag_formatter.add(“finished_at”, Lumberjack::Formatter::DateTimeFormatter.new
(“%Y-%m-%dT%H:%m:%S%z”))
Public Class Methods
# File lib/lumberjack/tag_formatter.rb, line 12 def initialize @formatters = {} @default_formatter = nil end
Public Instance Methods
Add a formatter for specific tag names. This can either be a Lumberjack::Formatter
or an object that responds to `call` or a block. The default formatter will not be applied.
# File lib/lumberjack/tag_formatter.rb, line 35 def add(names, formatter = nil, &block) formatter ||= block formatter = dereference_formatter(formatter) if formatter.nil? remove(key) else Array(names).each do |name| @formatters[name.to_s] = formatter end end self end
Remove all formatters.
# File lib/lumberjack/tag_formatter.rb, line 57 def clear @default_formatter = nil @formatters.clear self end
Add a default formatter applied to all tag values. This can either be a Lumberjack::Formatter
or an object that responds to `call` or a block.
# File lib/lumberjack/tag_formatter.rb, line 19 def default(formatter = nil, &block) formatter ||= block formatter = dereference_formatter(formatter) @default_formatter = formatter self end
Format a hash of tags using the formatters
# File lib/lumberjack/tag_formatter.rb, line 64 def format(tags) return nil if tags.nil? if @default_formatter.nil? && (@formatters.empty? || (@formatters.keys & tags.keys).empty?) tags else formatted = {} tags.each do |name, value| formatter = (@formatters[name.to_s] || @default_formatter) if formatter.is_a?(Lumberjack::Formatter) value = formatter.format(value) elsif formatter.respond_to?(:call) value = formatter.call(value) end formatted[name.to_s] = value end formatted end end
Remove formatters for specific tag names. The default formatter will still be applied.
# File lib/lumberjack/tag_formatter.rb, line 49 def remove(names) Array(names).each do |name| @formatters.delete(name.to_s) end self end
Remove the default formatter.
# File lib/lumberjack/tag_formatter.rb, line 27 def remove_default @default_formatter = nil self end
Private Instance Methods
# File lib/lumberjack/tag_formatter.rb, line 85 def dereference_formatter(formatter) if formatter.is_a?(TaggedLoggerSupport::Formatter) formatter.__formatter elsif formatter.is_a?(Symbol) formatter_class_name = "#{formatter.to_s.gsub(/(^|_)([a-z])/) { |m| $~[2].upcase }}Formatter" Formatter.const_get(formatter_class_name).new else formatter end end