File: level.rb

package info (click to toggle)
ruby-logging 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 688 kB
  • sloc: ruby: 6,272; sh: 9; makefile: 2
file content (32 lines) | stat: -rw-r--r-- 1,043 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
require 'set'

module Logging
  module Filters

    # The `Level` filter class provides a simple level-based filtering mechanism
    # that allows events whose log level matches a preconfigured list of values.
    class Level < ::Logging::Filter

      # Creates a new level filter that will only allow the given _levels_ to
      # propagate through to the logging destination. The _levels_ should be
      # given in symbolic form.
      #
      # Examples
      #     Logging::Filters::Level.new(:debug, :info)
      #
      def initialize(*levels)
        super()
        levels  = levels.flatten.map {|level| ::Logging::level_num(level)}
        @levels = Set.new(levels)
      end

      # Returns the event if it should be forwarded to the logging appender.
      # Otherwise, `nil` is returned. The log event is allowed if the
      # `event.level` matches one of the levels provided to the filter when it
      # was constructred.
      def allow(event)
        @levels.include?(event.level) ? event : nil
      end
    end
  end
end