Sha256: 235b5449d0b8173d6e060d9048181527579d4a865616c52f70536f227eb7b727

Contents?: true

Size: 1.3 KB

Versions: 12

Compression:

Stored size: 1.3 KB

Contents

module SemanticLogger
  module Levels
    # Logging levels in order of most detailed to most severe
    LEVELS = %i[trace debug info warn error fatal].freeze

    # Internal method to return the log level as an internal index
    # Also supports mapping the ::Logger levels to SemanticLogger levels
    def self.index(level)
      return if level.nil?

      index =
        if level.is_a?(Symbol)
          LEVELS.index(level)
        elsif level.is_a?(String)
          level = level.downcase.to_sym
          LEVELS.index(level)
        elsif level.is_a?(Integer) && defined?(::Logger::Severity)
          # Mapping of Rails and Ruby Logger levels to SemanticLogger levels
          @map_levels ||=
            begin
              levels = []
              ::Logger::Severity.constants.each do |constant|
                levels[::Logger::Severity.const_get(constant)] =
                  LEVELS.find_index(constant.downcase.to_sym) || LEVELS.find_index(:error)
              end
              levels
            end
          @map_levels[level]
        end
      raise "Invalid level:#{level.inspect} being requested. Must be one of #{LEVELS.inspect}" unless index

      index
    end

    # Returns the symbolic level for the supplied level index
    def self.level(level_index)
      LEVELS[level_index]
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
semantic_logger-4.12.0 lib/semantic_logger/levels.rb
semantic_logger-4.11.0 lib/semantic_logger/levels.rb
semantic_logger-4.10.0 lib/semantic_logger/levels.rb
semantic_logger-4.9.0 lib/semantic_logger/levels.rb
semantic_logger-4.8.2 lib/semantic_logger/levels.rb
semantic_logger-4.8.1 lib/semantic_logger/levels.rb
semantic_logger-4.8.0 lib/semantic_logger/levels.rb
semantic_logger-4.7.4 lib/semantic_logger/levels.rb
semantic_logger-4.7.3 lib/semantic_logger/levels.rb
semantic_logger-4.7.2 lib/semantic_logger/levels.rb
semantic_logger-4.7.1 lib/semantic_logger/levels.rb
semantic_logger-4.7.0 lib/semantic_logger/levels.rb