Class: Betterlog::Log::Severity

Inherits:
Object
  • Object
show all
Includes:
Comparable, Logger::Severity
Defined in:
lib/betterlog/log/severity.rb

Overview

A severity level representation for logging events.

This class provides a structured way to handle logging severity levels, ensuring consistent formatting and comparison of severity values. It integrates with Ruby’s standard Logger::Severity constants while providing additional functionality for normalization, caching, and comparison operations.

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Betterlog::Log::Severity

Initializes a new Severity instance with the given name.

This constructor creates a severity level object by converting the input name to a standardized symbol format and attempting to map it to a corresponding logger severity constant. If the constant is not found, it defaults to the UNKNOWN severity level.

Parameters:

  • name (String, Symbol, Betterlog::Log::Severity)

    the severity name to initialize with, which will be converted to uppercase and normalized to a symbol for lookup



60
61
62
63
64
65
66
67
68
69
# File 'lib/betterlog/log/severity.rb', line 60

def initialize(name)
  name = name.to_sym if self.class === name
  @name = name.to_s.downcase.to_sym
  begin
    @level = self.class.const_get(@name.to_s.upcase)
  rescue NameError
    @name  = :unknown
    @level = UNKNOWN
  end
end

Class Method Details

.allArray<Betterlog::Log::Severity>

Returns an array of all Severity constants as initialized objects.

This method retrieves all available severity constants defined in the class and creates a new instance of Severity for each one. The results are cached in an instance variable to avoid re-computation on subsequent calls.

Returns:



80
81
82
# File 'lib/betterlog/log/severity.rb', line 80

def self.all
  @all_constants ||= constants.map { |c| new(c) }
end

.new(name) ⇒ Betterlog::Log::Severity

Creates a new Severity instance with the given name, normalizing it to uppercase symbols for consistent lookup.

This method converts the input name to a standardized format by converting it to a symbol, uppercasing its string representation, and then attempting to retrieve or create a corresponding Severity constant. It ensures that only one instance exists per unique severity name by using a shared cache.

name to initialize with

Parameters:

Returns:



41
42
43
44
45
46
# File 'lib/betterlog/log/severity.rb', line 41

def new(name)
  name = name.to_sym if self.class === name
  name = name.to_s.upcase.to_sym
  self.shared ||= {}
  shared[name] ||= super(name).freeze
end

Instance Method Details

#<=>(other) ⇒ Integer

Compares this severity instance with another value for ordering.

This method enables sorting and comparison of severity levels by converting both the current instance and the provided value to their integer representations and performing a standard numeric comparison.

Parameters:

  • other (Object)

    the value to compare against, which will be converted to a Severity instance for comparison

Returns:

  • (Integer)

    -1 if this instance is less than the other, 0 if they are equal, 1 if this instance is greater than the other



138
139
140
# File 'lib/betterlog/log/severity.rb', line 138

def <=>(other)
  to_i <=> self.class.new(other).to_i
end

#as_jsonHash

Converts the log event to a JSON-compatible hash representation.

This method provides a way to serialize the log event data into a format suitable for JSON generation, ensuring that the event’s data can be easily converted to a JSON string while maintaining its structure and content.

Returns:

  • (Hash)

    a hash representation of the log event data



124
125
126
# File 'lib/betterlog/log/severity.rb', line 124

def as_json(*)
  to_s
end

#eql?(other) ⇒ TrueClass, FalseClass Also known as: ==

Checks equality between this severity instance and another object based on their symbol representations.

This method compares the symbol representation of the current severity instance with that of another object to determine if they are equivalent.

symbol representations, false otherwise

Parameters:

  • other (Object)

    the object to compare against

Returns:

  • (TrueClass, FalseClass)

    true if both objects have equal



152
153
154
# File 'lib/betterlog/log/severity.rb', line 152

def eql?(other)
  to_sym == other.to_sym
end

#hashInteger

Returns the hash value corresponding to the severity name symbol.

This method provides a hash representation of the internal symbol identifier for the severity level, which is used for consistent identification and comparison operations within the logging system.

Returns:

  • (Integer)

    the hash value of the severity name symbol



165
166
167
# File 'lib/betterlog/log/severity.rb', line 165

def hash
  @name.hash
end

#to_iInteger

Converts the severity level to its integer representation.

This method returns the underlying integer value that represents the severity level, which corresponds to standard logging severity constants.

Returns:

  • (Integer)

    the integer value of the severity level



91
92
93
# File 'lib/betterlog/log/severity.rb', line 91

def to_i
  @level
end

#to_sString

Converts the severity name to an uppercase string representation.

This method returns a string version of the severity name, converted to uppercase for consistent formatting and display purposes.

Returns:

  • (String)

    the uppercase string representation of the severity name



101
102
103
# File 'lib/betterlog/log/severity.rb', line 101

def to_s
  @name.to_s.upcase
end

#to_symSymbol

Converts the severity name to a symbol representation.

This method returns the internal symbol representation of the severity name, which is used for consistent identification and comparison of severity levels.

Returns:

  • (Symbol)

    the symbol representation of the severity name



112
113
114
# File 'lib/betterlog/log/severity.rb', line 112

def to_sym
  @name
end