Class: Console::Filter
- Inherits:
-
Object
- Object
- Console::Filter
- Defined in:
- lib/console/filter.rb
Overview
A log filter which can be used to filter log messages based on severity, subject, and other criteria.
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.
- #The current log level.(currentloglevel.) ⇒ Object readonly
- #The log levels for specific subject (classes).(loglevels) ⇒ Object readonly
-
#verbose ⇒ Object
readonly
Returns the value of attribute verbose.
Class Method Summary collapse
-
.[](**levels) ⇒ Object
Create a new log filter with specific log levels.
-
.define_immutable_method(name, &block) ⇒ Object
Define a method.
Instance Method Summary collapse
- #Additional options.=(options. = (value)) ⇒ Object
-
#all! ⇒ Object
Enable all logging.
-
#call(subject, *arguments, **options, &block) ⇒ Object
Log a message with the given severity.
-
#clear(subject) ⇒ Object
Clear any specific filters for the given class.
-
#disable(subject) ⇒ Object
Disable logging for the given class.
-
#enable(subject, level = self.class::MINIMUM_LEVEL) ⇒ Object
Enable specific log level for the given class.
-
#enabled?(subject, level = self.class::MINIMUM_LEVEL) ⇒ Boolean
Whether logging is enabled for the given subject and log level.
-
#filter(subject, level) ⇒ Object
Filter log messages based on the subject and log level.
-
#initialize(output, verbose: true, level: self.class::DEFAULT_LEVEL, **options) ⇒ Filter
constructor
Create a new log filter.
-
#off! ⇒ Object
Disable all logging.
- #The output destination.=(outputdestination. = (value)) ⇒ Object
-
#verbose!(value = true) ⇒ Object
Set verbose output (enable by default with no arguments).
- #Whether to enable verbose output.=(toenableverboseoutput. = (value)) ⇒ Object
-
#with(level: @level, verbose: @verbose, **options) ⇒ Object
Create a new log filter with the given options, from an existing log filter.
Constructor Details
#initialize(output, verbose: true, level: self.class::DEFAULT_LEVEL, **options) ⇒ Filter
Create a new log filter.
73 74 75 76 77 78 79 80 81 |
# File 'lib/console/filter.rb', line 73 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.
104 105 106 |
# File 'lib/console/filter.rb', line 104 def level @level end |
#options ⇒ Object
Returns the value of attribute options.
110 111 112 |
# File 'lib/console/filter.rb', line 110 def @options end |
#output ⇒ Object
Returns the value of attribute output.
98 99 100 |
# File 'lib/console/filter.rb', line 98 def output @output end |
#subjects ⇒ Object (readonly)
Returns the value of attribute subjects.
107 108 109 |
# File 'lib/console/filter.rb', line 107 def subjects @subjects end |
#The current log level.(currentloglevel.) ⇒ Object (readonly)
104 |
# File 'lib/console/filter.rb', line 104 attr :level |
#The log levels for specific subject (classes).(loglevels) ⇒ Object (readonly)
107 |
# File 'lib/console/filter.rb', line 107 attr :subjects |
#verbose ⇒ Object (readonly)
Returns the value of attribute verbose.
101 102 103 |
# File 'lib/console/filter.rb', line 101 def verbose @verbose end |
Class Method Details
.[](**levels) ⇒ Object
Create a new log filter with specific log levels.
“‘ruby class MyLogger < Console::Filter[debug: 0, okay: 1, bad: 2, terrible: 3] “`
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/console/filter.rb', line 34 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
Define a method.
16 17 18 19 |
# File 'lib/console/filter.rb', line 16 def self.define_immutable_method(name, &block) block = Ractor.make_shareable(block) self.define_method(name, &block) end |
Instance Method Details
#Additional options.=(options. = (value)) ⇒ Object
110 |
# File 'lib/console/filter.rb', line 110 attr_accessor :options |
#all! ⇒ Object
Enable all logging.
137 138 139 |
# File 'lib/console/filter.rb', line 137 def all! @level = self.class::MINIMUM_LEVEL - 1 end |
#call(subject, *arguments, **options, &block) ⇒ Object
Log a message with the given severity.
208 209 210 211 212 213 214 215 216 217 |
# File 'lib/console/filter.rb', line 208 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.
193 194 195 196 197 198 199 |
# File 'lib/console/filter.rb', line 193 def clear(subject) unless subject.is_a?(Module) raise ArgumentError, "Expected a class, got #{subject.inspect}" end @subjects.delete(subject) end |
#disable(subject) ⇒ Object
Disable logging for the given class.
185 186 187 188 |
# File 'lib/console/filter.rb', line 185 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.
177 178 179 180 |
# File 'lib/console/filter.rb', line 177 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
Whether logging is enabled for the given subject and log level.
You can enable and disable logging for classes. This function checks if logging for a given subject is enabled.
162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/console/filter.rb', line 162 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
Filter log messages based on the subject and log level.
You must provide the subject’s class, not an instance of the class.
147 148 149 150 151 152 153 |
# File 'lib/console/filter.rb', line 147 def filter(subject, level) unless subject.is_a?(Module) raise ArgumentError, "Expected a class, got #{subject.inspect}" end @subjects[subject] = level end |
#off! ⇒ Object
Disable all logging.
132 133 134 |
# File 'lib/console/filter.rb', line 132 def off! @level = self.class::MAXIMUM_LEVEL + 1 end |
#The output destination.=(outputdestination. = (value)) ⇒ Object
98 |
# File 'lib/console/filter.rb', line 98 attr_accessor :output |
#verbose!(value = true) ⇒ Object
Set verbose output (enable by default with no arguments).
126 127 128 129 |
# File 'lib/console/filter.rb', line 126 def verbose!(value = true) @verbose = value @output.verbose!(value) end |
#Whether to enable verbose output.=(toenableverboseoutput. = (value)) ⇒ Object
101 |
# File 'lib/console/filter.rb', line 101 attr :verbose |
#with(level: @level, verbose: @verbose, **options) ⇒ Object
Create a new log filter with the given options, from an existing log filter.
89 90 91 92 93 94 95 |
# File 'lib/console/filter.rb', line 89 def with(level: @level, verbose: @verbose, **) dup.tap do |logger| logger.level = level logger.verbose! if verbose logger. = @options.merge() end end |