Class: Console::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/console/filter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output, verbose: true, level: self.class::DEFAULT_LEVEL, **options) ⇒ Filter

Returns a new instance of Filter.



55
56
57
58
59
60
61
62
63
# File 'lib/console/filter.rb', line 55

def initialize(output, verbose: true, level: self.class::DEFAULT_LEVEL, **options)
	@output = output
	@verbose = verbose
	@level = level
	
	@subjects = {}
	
	@options = options
end

Instance Attribute Details

#levelObject

Returns the value of attribute level.



75
76
77
# File 'lib/console/filter.rb', line 75

def level
  @level
end

#optionsObject

Returns the value of attribute options.



79
80
81
# File 'lib/console/filter.rb', line 79

def options
  @options
end

#outputObject

Returns the value of attribute output.



73
74
75
# File 'lib/console/filter.rb', line 73

def output
  @output
end

#subjectsObject (readonly)

Returns the value of attribute subjects.



77
78
79
# File 'lib/console/filter.rb', line 77

def subjects
  @subjects
end

#verboseObject (readonly)

Returns the value of attribute verbose.



74
75
76
# File 'lib/console/filter.rb', line 74

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
# 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, **options, &block|
				if self.enabled?(subject, level)
					@output.call(subject, *arguments, severity: name, **@options, **options, &block)
				end
			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



98
99
100
# File 'lib/console/filter.rb', line 98

def all!
	@level = self.class::MINIMUM_LEVEL - 1
end

#call(subject, *arguments, **options, &block) ⇒ Object



146
147
148
149
150
151
152
153
# File 'lib/console/filter.rb', line 146

def call(subject, *arguments, **options, &block)
	severity = options[:severity] || UNKNOWN
	level = self.class::LEVELS[severity]
	
	if self.enabled?(subject, level)
		@output.call(subject, *arguments, **options, &block)
	end
end

#clear(subject) ⇒ Object

Clear any specific filters for the given class.



138
139
140
141
142
143
144
# File 'lib/console/filter.rb', line 138

def clear(subject)
	unless subject.is_a?(Module)
		raise ArgumentError, "Expected a class, got #{subject.inspect}"
	end
	
	@subjects.delete(subject)
end

#disable(subject) ⇒ Object



131
132
133
134
# File 'lib/console/filter.rb', line 131

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.



126
127
128
129
# File 'lib/console/filter.rb', line 126

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.

Parameters:

  • subject (Object)

    the subject to check.

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
120
121
122
# File 'lib/console/filter.rb', line 112

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



102
103
104
105
106
107
108
# File 'lib/console/filter.rb', line 102

def filter(subject, level)
	unless subject.is_a?(Module)
		raise ArgumentError, "Expected a class, got #{subject.inspect}"
	end
	
	@subjects[subject] = level
end

#off!Object



94
95
96
# File 'lib/console/filter.rb', line 94

def off!
	@level = self.class::MAXIMUM_LEVEL + 1
end

#verbose!(value = true) ⇒ Object



89
90
91
92
# File 'lib/console/filter.rb', line 89

def verbose!(value = true)
	@verbose = value
	@output.verbose!(value)
end

#with(level: @level, verbose: @verbose, **options) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/console/filter.rb', line 65

def with(level: @level, verbose: @verbose, **options)
	dup.tap do |logger|
		logger.level = level
		logger.verbose! if verbose
		logger.options = @options.merge(options)
	end
end