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.



12
13
14
15
16
# File 'lib/lumberjack/tag_formatter.rb', line 12

def initialize
  @formatters = {}
  @class_formatters = {}
  @default_formatter = nil
end

Instance Method Details

#add(names_or_classes, formatter = nil, &block) ⇒ Lumberjack::TagFormatter

Add a formatter for specific tag names or object classes. This can either be a Lumberjack::Formatter or an object that responds to ‘call` or a block. The formatter will be applied if it matches either a tag name or if the tag value is an instance of a registered class. Tag name formatters will take precedence over class formatters. The default formatter will not be applied to a value if a tag formatter is applied to it.

Name formatters can be applied to nested hashes using dot syntax. For example, if you add a formatter for “foo.bar”, it will be applied to the value of the “bar” key in the “foo” tag if that value is a hash.

Class formatters will be applied recursively to nested hashes and arrays.

Examples:

tag_formatter.add("password", &:redact)

Parameters:

  • names_or_classes (String, Module, Array<String, Module>)

    The tag names or object classes to apply the formatter to.

  • formatter (Lumberjack::Formatter, #call, nil) (defaults to: nil)

    The formatter to use. If this is nil, then the block will be used as the formatter.

Returns:



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/lumberjack/tag_formatter.rb', line 58

def add(names_or_classes, formatter = nil, &block)
  formatter ||= block
  formatter = dereference_formatter(formatter)
  if formatter.nil?
    remove(key)
  else
    Array(names_or_classes).each do |key|
      if key.is_a?(Module)
        @class_formatters[key] = formatter
      else
        @formatters[key.to_s] = formatter
      end
    end
  end
  self
end

#clearLumberjack::TagFormatter

Remove all formatters.

Returns:



93
94
95
96
97
# File 'lib/lumberjack/tag_formatter.rb', line 93

def clear
  @default_formatter = nil
  @formatters.clear
  self
end

#default(formatter = nil, &block) ⇒ Lumberjack::TagFormatter

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.

Parameters:

  • formatter (Lumberjack::Formatter, #call, nil) (defaults to: nil)

    The formatter to use. If this is nil, then the block will be used as the formatter.

Returns:



24
25
26
27
28
29
# File 'lib/lumberjack/tag_formatter.rb', line 24

def default(formatter = nil, &block)
  formatter ||= block
  formatter = dereference_formatter(formatter)
  @default_formatter = formatter
  self
end

#format(tags) ⇒ Hash

Format a hash of tags using the formatters

Parameters:

  • tags (Hash)

    The tags to format.

Returns:

  • (Hash)

    The formatted tags.



103
104
105
106
107
108
109
110
# File 'lib/lumberjack/tag_formatter.rb', line 103

def format(tags)
  return nil if tags.nil?
  if @default_formatter.nil? && @formatters.empty? && @class_formatters.empty?
    return tags
  end

  formatted_tags(tags)
end

#remove(names_or_classes) ⇒ Lumberjack::TagFormatter

Remove formatters for specific tag names. The default formatter will still be applied.

Parameters:

  • names_or_classes (String, Module, Array<String, Module>)

    The tag names or classes to remove the formatter from.

Returns:



79
80
81
82
83
84
85
86
87
88
# File 'lib/lumberjack/tag_formatter.rb', line 79

def remove(names_or_classes)
  Array(names_or_classes).each do |key|
    if key.is_a?(Module)
      @class_formatters.delete(key)
    else
      @formatters.delete(key.to_s)
    end
  end
  self
end

#remove_defaultLumberjack::TagFormatter

Remove the default formatter.

Returns:



34
35
36
37
# File 'lib/lumberjack/tag_formatter.rb', line 34

def remove_default
  @default_formatter = nil
  self
end