Class: Smplkit::LogLevel

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/smplkit/log_level.rb

Overview

Log severity levels used by the Smpl Logging service.

Acts as a string-valued enum: each constant equals its name when used in string contexts, and supports comparison via the ordinal. Members are declared in increasing severity order — matching the Ruby stdlib Logger convention (TRACE = least severe, SILENT = most severe).

Constant Summary collapse

NAMES =
%w[TRACE DEBUG INFO WARN ERROR FATAL SILENT].freeze
TRACE =
new("TRACE", 0)
DEBUG =
new("DEBUG", 1)
INFO =
new("INFO", 2)
WARN =
new("WARN", 3)
ERROR =
new("ERROR", 4)
FATAL =
new("FATAL", 5)
SILENT =
new("SILENT", 6)
ALL =
[TRACE, DEBUG, INFO, WARN, ERROR, FATAL, SILENT].freeze
BY_NAME =
ALL.to_h { |lvl| [lvl.name, lvl] }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, ordinal) ⇒ LogLevel

Returns a new instance of LogLevel.



19
20
21
22
23
# File 'lib/smplkit/log_level.rb', line 19

def initialize(name, ordinal)
  @name = name.freeze
  @ordinal = ordinal
  freeze
end

Instance Attribute Details

#nameString (readonly)

Returns Canonical level name (e.g. “INFO”).

Returns:

  • (String)

    Canonical level name (e.g. “INFO”).



14
15
16
# File 'lib/smplkit/log_level.rb', line 14

def name
  @name
end

#ordinalInteger (readonly)

Returns Severity ordinal — TRACE=0 (lowest) through SILENT=6 (highest).

Returns:

  • (Integer)

    Severity ordinal — TRACE=0 (lowest) through SILENT=6 (highest).



17
18
19
# File 'lib/smplkit/log_level.rb', line 17

def ordinal
  @ordinal
end

Class Method Details

.coerce(value) ⇒ Object



57
58
59
60
61
# File 'lib/smplkit/log_level.rb', line 57

def self.coerce(value)
  return value if value.is_a?(LogLevel)

  from_string(value)
end

.from_string(value) ⇒ Object

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
54
55
# File 'lib/smplkit/log_level.rb', line 47

def self.from_string(value)
  raise ArgumentError, "log level cannot be nil" if value.nil?

  key = value.to_s.upcase
  level = BY_NAME[key]
  raise ArgumentError, "unknown log level: #{value.inspect}" unless level

  level
end

Instance Method Details

#<=>(other) ⇒ Object



31
# File 'lib/smplkit/log_level.rb', line 31

def <=>(other) = other.is_a?(LogLevel) ? @ordinal <=> other.ordinal : nil

#==(other) ⇒ Object



28
# File 'lib/smplkit/log_level.rb', line 28

def ==(other) = other.is_a?(LogLevel) ? @ordinal == other.ordinal : @name == other

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


30
# File 'lib/smplkit/log_level.rb', line 30

def eql?(other) = self == other

#hashObject



29
# File 'lib/smplkit/log_level.rb', line 29

def hash = @ordinal.hash

#inspectObject



27
# File 'lib/smplkit/log_level.rb', line 27

def inspect = "#<Smplkit::LogLevel #{@name}>"

#to_sObject



25
# File 'lib/smplkit/log_level.rb', line 25

def to_s = @name

#to_strObject



26
# File 'lib/smplkit/log_level.rb', line 26

def to_str = @name