Class: Lumberjack::TagFormatter

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeTagFormatter

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

#clearObject

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(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(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_defaultObject

Remove the default formatter.



28
29
30
31
# File 'lib/lumberjack/tag_formatter.rb', line 28

def remove_default
  @default_formatter = nil
  self
end