Module: Lumberjack::FormatterRegistry

Defined in:
lib/lumberjack/formatter_registry.rb

Overview

The formatter registry is used for setting up names to represent Formatter classes. It is used in the constructor for Lumberjack::Logger and allows passing in a symbol to reference a formatter.

Formatters must respond to the call method.

Examples:


Lumberjack::FormatterRegistry.add(:upcase) { |value| value.to_s.upcase }
Lumberjack::FormatterRegistry.add(:currency, Lumberjack::Formatter::RoundFormatter, 2)

Lumberjack::EntryFormatter.build do |config|
  config.add_attribute :status, :upcase
  config.add_attribute :amount, :currency
end

Class Method Summary collapse

Class Method Details

.add(name, formatter = nil, &block) ⇒ void

This method returns an undefined value.

Register a formatter name. Formatter names can be used to associate a symbol with a formatter class. The symbol can then be passed to Logger as the formatter argument.

Registered formatters must take only one argument and that is the options hash for the formatter options.

Parameters:

  • name (Symbol)

    The name of the formatter

  • formatter (Class, #call) (defaults to: nil)

    The formatter or formatter class to register..

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
# File 'lib/lumberjack/formatter_registry.rb', line 33

def add(name, formatter = nil, &block)
  raise ArgumentError.new("name must be a symbol") unless name.is_a?(Symbol)
  raise ArgumentError.new("formatter or block must be provided") if formatter.nil? && block.nil?
  raise ArgumentError.new("cannot have both formatter and a block") if !formatter.nil? && !block.nil?

  formatter ||= block
  raise ArgumentError.new("formatter must be a class or respond to call") unless formatter.is_a?(Class) || formatter.respond_to?(:call)

  @lock.synchronize { @registry[name] = formatter }
end

.formatter(name, *args) ⇒ #call?

Retrieve the formatter registered with the given name or nil if the name is not defined.

Parameters:

  • name (Symbol)

    The name of the formatter

Returns:

  • (#call, nil)

    The registered formatter class or nil if not found



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/lumberjack/formatter_registry.rb', line 64

def formatter(name, *args)
  instance = @lock.synchronize { @registry[name] }

  if instance.nil?
    valid_names = registered_formatters.keys.map(&:inspect).join(", ")
    raise ArgumentError.new("#{name.inspect} is not registered as a formatter name; valid names are: #{valid_names}")
  end

  instance = instance.new(*args) if instance.is_a?(Class)

  instance
end

.registered?(name) ⇒ Boolean

Check if a formatter is registered.

Parameters:

  • name (Symbol)

    The name of the formatter

Returns:

  • (Boolean)

    True if the formatter is registered, false otherwise



56
57
58
# File 'lib/lumberjack/formatter_registry.rb', line 56

def registered?(name)
  @lock.synchronize { @registry.include?(name) }
end

.registered_formattersHash

Return the map of registered formatters.

Returns:

  • (Hash)


80
81
82
# File 'lib/lumberjack/formatter_registry.rb', line 80

def registered_formatters
  @lock.synchronize { @registry.dup }
end

.remove(name) ⇒ void

This method returns an undefined value.

Remove a formatter from the registry.

Parameters:

  • name (Symbol)

    The name of the formatter to remove



48
49
50
# File 'lib/lumberjack/formatter_registry.rb', line 48

def remove(name)
  @lock.synchronize { @registry.delete(name) }
end