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: nil, **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: nil, **options) ⇒ Filter
Create a new log filter.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/console/filter.rb', line 73 def initialize(output, verbose: true, level: nil, **) @output = output @verbose = verbose # Set the log level using the behaviour implemented in `level=`: if level self.level = level else @level = self.class::DEFAULT_LEVEL end @subjects = {} @options = end |
Instance Attribute Details
#level ⇒ Object
Returns the value of attribute level.
110 111 112 |
# File 'lib/console/filter.rb', line 110 def level @level end |
#options ⇒ Object
Returns the value of attribute options.
116 117 118 |
# File 'lib/console/filter.rb', line 116 def @options end |
#output ⇒ Object
Returns the value of attribute output.
104 105 106 |
# File 'lib/console/filter.rb', line 104 def output @output end |
#subjects ⇒ Object (readonly)
Returns the value of attribute subjects.
113 114 115 |
# File 'lib/console/filter.rb', line 113 def subjects @subjects end |
#The current log level.(currentloglevel.) ⇒ Object (readonly)
110 |
# File 'lib/console/filter.rb', line 110 attr :level |
#The log levels for specific subject (classes).(loglevels) ⇒ Object (readonly)
113 |
# File 'lib/console/filter.rb', line 113 attr :subjects |
#verbose ⇒ Object (readonly)
Returns the value of attribute verbose.
107 108 109 |
# File 'lib/console/filter.rb', line 107 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
116 |
# File 'lib/console/filter.rb', line 116 attr_accessor :options |
#all! ⇒ Object
Enable all logging.
143 144 145 |
# File 'lib/console/filter.rb', line 143 def all! @level = self.class::MINIMUM_LEVEL - 1 end |
#call(subject, *arguments, **options, &block) ⇒ Object
Log a message with the given severity.
214 215 216 217 218 219 220 221 222 223 |
# File 'lib/console/filter.rb', line 214 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.
199 200 201 202 203 204 205 |
# File 'lib/console/filter.rb', line 199 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.
191 192 193 194 |
# File 'lib/console/filter.rb', line 191 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.
183 184 185 186 |
# File 'lib/console/filter.rb', line 183 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.
168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/console/filter.rb', line 168 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.
153 154 155 156 157 158 159 |
# File 'lib/console/filter.rb', line 153 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.
138 139 140 |
# File 'lib/console/filter.rb', line 138 def off! @level = self.class::MAXIMUM_LEVEL + 1 end |
#The output destination.=(outputdestination. = (value)) ⇒ Object
104 |
# File 'lib/console/filter.rb', line 104 attr_accessor :output |
#verbose!(value = true) ⇒ Object
Set verbose output (enable by default with no arguments).
132 133 134 135 |
# File 'lib/console/filter.rb', line 132 def verbose!(value = true) @verbose = value @output.verbose!(value) end |
#Whether to enable verbose output.=(toenableverboseoutput. = (value)) ⇒ Object
107 |
# File 'lib/console/filter.rb', line 107 attr :verbose |
#with(level: @level, verbose: @verbose, **options) ⇒ Object
Create a new log filter with the given options, from an existing log filter.
95 96 97 98 99 100 101 |
# File 'lib/console/filter.rb', line 95 def with(level: @level, verbose: @verbose, **) dup.tap do |logger| logger.level = level logger.verbose! if verbose logger. = @options.merge() end end |