Class: Lumberjack::Formatter::RoundFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/lumberjack/formatter/round_formatter.rb

Overview

Round numeric values to a set number of decimal places. This is useful when logging floating point numbers to reduce noise and rounding errors in the logs.

The formatter only affects numeric values, leaving other object types unchanged. This makes it safe to use as a general-purpose formatter for attributes that might contain various data types.

Instance Method Summary collapse

Constructor Details

#initialize(precision = 3) ⇒ RoundFormatter

Returns a new instance of RoundFormatter.

Parameters:

  • precision (Integer) (defaults to: 3)

    The number of decimal places to round to (defaults to 3).



15
16
17
# File 'lib/lumberjack/formatter/round_formatter.rb', line 15

def initialize(precision = 3)
  @precision = precision
end

Instance Method Details

#call(obj) ⇒ Numeric, Object

Round a numeric value to the configured precision.

Parameters:

  • obj (Object)

    The object to format. Only numeric values are rounded.

Returns:

  • (Numeric, Object)

    The rounded number if the object is numeric, otherwise returns the object unchanged.



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

def call(obj)
  if obj.is_a?(Numeric)
    obj.round(@precision)
  else
    obj
  end
end