Class: Betterlog::Log::Severity
- Inherits:
-
Object
- Object
- Betterlog::Log::Severity
- 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.
Class Method Summary collapse
-
.all ⇒ Array<Betterlog::Log::Severity>
Returns an array of all Severity constants as initialized objects.
-
.new(name) ⇒ Betterlog::Log::Severity
Creates a new Severity instance with the given name, normalizing it to uppercase symbols for consistent lookup.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Compares this severity instance with another value for ordering.
-
#as_json ⇒ Hash
Converts the log event to a JSON-compatible hash representation.
-
#eql?(other) ⇒ TrueClass, FalseClass
(also: #==)
Checks equality between this severity instance and another object based on their symbol representations.
-
#hash ⇒ Integer
Returns the hash value corresponding to the severity name symbol.
-
#initialize(name) ⇒ Betterlog::Log::Severity
constructor
Initializes a new Severity instance with the given name.
-
#to_i ⇒ Integer
Converts the severity level to its integer representation.
-
#to_s ⇒ String
Converts the severity name to an uppercase string representation.
-
#to_sym ⇒ Symbol
Converts the severity name to a symbol representation.
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.
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
.all ⇒ Array<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.
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
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.
138 139 140 |
# File 'lib/betterlog/log/severity.rb', line 138 def <=>(other) to_i <=> self.class.new(other).to_i end |
#as_json ⇒ Hash
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.
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
152 153 154 |
# File 'lib/betterlog/log/severity.rb', line 152 def eql?(other) to_sym == other.to_sym end |
#hash ⇒ Integer
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.
165 166 167 |
# File 'lib/betterlog/log/severity.rb', line 165 def hash @name.hash end |
#to_i ⇒ Integer
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.
91 92 93 |
# File 'lib/betterlog/log/severity.rb', line 91 def to_i @level end |
#to_s ⇒ String
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.
101 102 103 |
# File 'lib/betterlog/log/severity.rb', line 101 def to_s @name.to_s.upcase end |
#to_sym ⇒ Symbol
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.
112 113 114 |
# File 'lib/betterlog/log/severity.rb', line 112 def to_sym @name end |