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.



46
47
48
49
50
51
52
53
54
# File 'lib/console/filter.rb', line 46

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.



66
67
68
# File 'lib/console/filter.rb', line 66

def level
  @level
end

#optionsObject

Returns the value of attribute options.



70
71
72
# File 'lib/console/filter.rb', line 70

def options
  @options
end

#outputObject

Returns the value of attribute output.



64
65
66
# File 'lib/console/filter.rb', line 64

def output
  @output
end

#subjectsObject (readonly)

Returns the value of attribute subjects.



68
69
70
# File 'lib/console/filter.rb', line 68

def subjects
  @subjects
end

#verboseObject (readonly)

Returns the value of attribute verbose.



65
66
67
# File 'lib/console/filter.rb', line 65

def verbose
  @verbose
end

Class Method Details

.[](**levels) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/console/filter.rb', line 15

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_method(name) do |subject = nil, *arguments, **options, &block|
				if self.enabled?(subject, level)
					self.call(subject, *arguments, severity: name, **options, **@options, &block)
				end
			end
			
			define_method("#{name}!") do
				@level = level
			end
			
			define_method("#{name}?") do
				@level <= level
			end
		end
	end
	
	return klass
end

Instance Method Details

#all!Object



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

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

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



137
138
139
# File 'lib/console/filter.rb', line 137

def call(*arguments, **options, &block)
	@output.call(*arguments, **options, &block)
end

#clear(subject) ⇒ Object

Clear any specific filters for the given class.



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

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

#disable(subject) ⇒ Object



122
123
124
125
# File 'lib/console/filter.rb', line 122

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.



117
118
119
120
# File 'lib/console/filter.rb', line 117

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)


103
104
105
106
107
108
109
110
111
112
113
# File 'lib/console/filter.rb', line 103

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



93
94
95
96
97
98
99
# File 'lib/console/filter.rb', line 93

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

#off!Object



85
86
87
# File 'lib/console/filter.rb', line 85

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

#verbose!(value = true) ⇒ Object



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

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

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



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

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