Class: Console::Filter
- Inherits:
-
Object
- Object
- Console::Filter
- Defined in:
- lib/console/filter.rb
Instance Attribute Summary collapse
-
#level ⇒ Object
Returns the value of attribute level.
-
#options ⇒ Object
Returns the value of attribute options.
-
#output ⇒ Object
Returns the value of attribute output.
-
#subjects ⇒ Object
readonly
Returns the value of attribute subjects.
-
#verbose ⇒ Object
readonly
Returns the value of attribute verbose.
Class Method Summary collapse
Instance Method Summary collapse
- #all! ⇒ Object
- #call(subject, *arguments, **options, &block) ⇒ Object
-
#clear(subject) ⇒ Object
Clear any specific filters for the given class.
- #disable(subject) ⇒ Object
-
#enable(subject, level = self.class::MINIMUM_LEVEL) ⇒ Object
Enable specific log level for the given class.
-
#enabled?(subject, level = self.class::MINIMUM_LEVEL) ⇒ Boolean
You can enable and disable logging for classes.
- #filter(subject, level) ⇒ Object
-
#initialize(output, verbose: true, level: self.class::DEFAULT_LEVEL, **options) ⇒ Filter
constructor
A new instance of Filter.
- #off! ⇒ Object
- #verbose!(value = true) ⇒ Object
- #with(level: @level, verbose: @verbose, **options) ⇒ Object
Constructor Details
#initialize(output, verbose: true, level: self.class::DEFAULT_LEVEL, **options) ⇒ Filter
Returns a new instance of Filter.
57 58 59 60 61 62 63 64 65 |
# File 'lib/console/filter.rb', line 57 def initialize(output, verbose: true, level: self.class::DEFAULT_LEVEL, **) @output = output @verbose = verbose @level = level @subjects = {} @options = end |
Instance Attribute Details
#level ⇒ Object
Returns the value of attribute level.
77 78 79 |
# File 'lib/console/filter.rb', line 77 def level @level end |
#options ⇒ Object
Returns the value of attribute options.
81 82 83 |
# File 'lib/console/filter.rb', line 81 def @options end |
#output ⇒ Object
Returns the value of attribute output.
75 76 77 |
# File 'lib/console/filter.rb', line 75 def output @output end |
#subjects ⇒ Object (readonly)
Returns the value of attribute subjects.
79 80 81 |
# File 'lib/console/filter.rb', line 79 def subjects @subjects end |
#verbose ⇒ Object (readonly)
Returns the value of attribute verbose.
76 77 78 |
# File 'lib/console/filter.rb', line 76 def verbose @verbose end |
Class Method Details
.[](**levels) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/console/filter.rb', line 24 def self.[] **levels klass = Class.new(self) minimum_level, maximum_level = levels.values.minmax klass.instance_exec do const_set(:LEVELS, levels.freeze) const_set(:MINIMUM_LEVEL, minimum_level) const_set(:MAXIMUM_LEVEL, maximum_level) levels.each do |name, level| const_set(name.to_s.upcase, level) define_immutable_method(name) do |subject = nil, *arguments, **, &block| if self.enabled?(subject, level) @output.call(subject, *arguments, severity: name, **@options, **, &block) end return nil end define_immutable_method("#{name}!") do @level = level end define_immutable_method("#{name}?") do @level <= level end end end return klass end |
.define_immutable_method(name, &block) ⇒ Object
14 15 16 17 |
# File 'lib/console/filter.rb', line 14 def self.define_immutable_method(name, &block) block = Ractor.make_shareable(block) self.define_method(name, &block) end |
Instance Method Details
#all! ⇒ Object
100 101 102 |
# File 'lib/console/filter.rb', line 100 def all! @level = self.class::MINIMUM_LEVEL - 1 end |
#call(subject, *arguments, **options, &block) ⇒ Object
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/console/filter.rb', line 148 def call(subject, *arguments, **, &block) severity = [:severity] || UNKNOWN level = self.class::LEVELS[severity] if self.enabled?(subject, level) @output.call(subject, *arguments, **, &block) end return nil end |
#clear(subject) ⇒ Object
Clear any specific filters for the given class.
140 141 142 143 144 145 146 |
# File 'lib/console/filter.rb', line 140 def clear(subject) unless subject.is_a?(Module) raise ArgumentError, "Expected a class, got #{subject.inspect}" end @subjects.delete(subject) end |
#disable(subject) ⇒ Object
133 134 135 136 |
# File 'lib/console/filter.rb', line 133 def disable(subject) # Set the filter level of the logging for a given subject which filters all log messages: filter(subject, self.class::MAXIMUM_LEVEL + 1) end |
#enable(subject, level = self.class::MINIMUM_LEVEL) ⇒ Object
Enable specific log level for the given class.
128 129 130 131 |
# File 'lib/console/filter.rb', line 128 def enable(subject, level = self.class::MINIMUM_LEVEL) # Set the filter level of logging for a given subject which passes all log messages: filter(subject, level) end |
#enabled?(subject, level = self.class::MINIMUM_LEVEL) ⇒ Boolean
You can enable and disable logging for classes. This function checks if logging for a given subject is enabled.
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/console/filter.rb', line 114 def enabled?(subject, level = self.class::MINIMUM_LEVEL) subject = subject.class unless subject.is_a?(Module) if specific_level = @subjects[subject] return level >= specific_level end if level >= @level return true end end |
#filter(subject, level) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/console/filter.rb', line 104 def filter(subject, level) unless subject.is_a?(Module) raise ArgumentError, "Expected a class, got #{subject.inspect}" end @subjects[subject] = level end |
#off! ⇒ Object
96 97 98 |
# File 'lib/console/filter.rb', line 96 def off! @level = self.class::MAXIMUM_LEVEL + 1 end |
#verbose!(value = true) ⇒ Object
91 92 93 94 |
# File 'lib/console/filter.rb', line 91 def verbose!(value = true) @verbose = value @output.verbose!(value) end |
#with(level: @level, verbose: @verbose, **options) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/console/filter.rb', line 67 def with(level: @level, verbose: @verbose, **) dup.tap do |logger| logger.level = level logger.verbose! if verbose logger. = @options.merge() end end |