Class: Lumberjack::TagFormatter
- Inherits:
-
Object
- Object
- Lumberjack::TagFormatter
- Defined in:
- lib/lumberjack/tag_formatter.rb
Overview
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”))
Instance Method Summary collapse
-
#add(names, formatter = nil, &block) ⇒ Object
Add a formatter for specific tag names.
-
#clear ⇒ Object
Remove all formatters.
-
#default(formatter = nil, &block) ⇒ Object
Add a default formatter applied to all tag values.
-
#format(tags) ⇒ Object
Format a hash of tags using the formatters.
-
#initialize ⇒ TagFormatter
constructor
A new instance of TagFormatter.
-
#remove(names) ⇒ Object
Remove formatters for specific tag names.
-
#remove_default ⇒ Object
Remove the default formatter.
Constructor Details
#initialize ⇒ TagFormatter
Returns a new instance of TagFormatter.
13 14 15 16 |
# File 'lib/lumberjack/tag_formatter.rb', line 13 def initialize @formatters = {} @default_formatter = nil end |
Instance Method Details
#add(names, formatter = nil, &block) ⇒ Object
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.
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/lumberjack/tag_formatter.rb', line 36 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 |
#clear ⇒ Object
Remove all formatters.
58 59 60 61 62 |
# File 'lib/lumberjack/tag_formatter.rb', line 58 def clear @default_formatter = nil @formatters.clear self end |
#default(formatter = nil, &block) ⇒ Object
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.
20 21 22 23 24 25 |
# File 'lib/lumberjack/tag_formatter.rb', line 20 def default(formatter = nil, &block) formatter ||= block formatter = dereference_formatter(formatter) @default_formatter = formatter self end |
#format(tags) ⇒ Object
Format a hash of tags using the formatters
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/lumberjack/tag_formatter.rb', line 65 def format() return nil if .nil? if @default_formatter.nil? && (@formatters.empty? || (@formatters.keys & .keys).empty?) else formatted = {} .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(names) ⇒ Object
Remove formatters for specific tag names. The default formatter will still be applied.
50 51 52 53 54 55 |
# File 'lib/lumberjack/tag_formatter.rb', line 50 def remove(names) Array(names).each do |name| @formatters.delete(name.to_s) end self end |
#remove_default ⇒ Object
Remove the default formatter.
28 29 30 31 |
# File 'lib/lumberjack/tag_formatter.rb', line 28 def remove_default @default_formatter = nil self end |