Class: Loggability::Override

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/loggability/override.rb

Overview

A class which encapsulates the logic and data of temporarily overriding one or more aspects of logging for the execution of one or more blocks of code.

It’s not meant to be used directly, but via one of the override aggregate methods on Loggability:

  • Loggability.with_level

  • Loggability.outputting_to

  • Loggability.formatted_with

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}) ⇒ Override

Create a new Override with the specified settings that will be applied during a call to #call, and then reverted when #call returns. Valid settings are:

:level

Set the level of all Loggers to the value.

:logdev

Set the destination log device of all Loggers to the value.

:log_hosts

Set the affected log hosts to the value.

:formatter

Set the formatter for all Loggers to the value (a Loggability::Formatter).



68
69
70
71
72
73
74
# File 'lib/loggability/override.rb', line 68

def initialize( settings={} )
	super()

	@log_hosts = settings.delete( :log_hosts )
	@settings = settings
	@overridden_settings = nil
end

Instance Attribute Details

#log_hostsObject

The log hosts the Override will be applied to during an overridden #call. If this is nil, all log hosts will be affected.



103
104
105
# File 'lib/loggability/override.rb', line 103

def log_hosts
  @log_hosts
end

#overridden_settingsObject

The original settings preserved by the Override during a call to #call, keyed by the logger they belong to.



98
99
100
# File 'lib/loggability/override.rb', line 98

def overridden_settings
  @overridden_settings
end

#settingsObject (readonly)

The Override’s settings Hash (the settings that will be applied during an overridden #call).



93
94
95
# File 'lib/loggability/override.rb', line 93

def settings
  @settings
end

Class Method Details

.for_logger(*log_hosts, &block) ⇒ Object

Set up an Override that will only apply to the specified log_hosts. If called with a block, call #call immediately on the Override with the block and return the result of that instead.



50
51
52
# File 'lib/loggability/override.rb', line 50

def self::for_logger( *log_hosts, &block )
	return self.new( log_hosts: log_hosts, &block )
end

.formatted_with(new_formatter, &block) ⇒ Object

Set up an Override with its logging formatter set to formatter. If called with a block, call #call immediately on the Override with the block and return the result of that instead.



42
43
44
# File 'lib/loggability/override.rb', line 42

def self::formatted_with( new_formatter, &block )
	return self.new( formatter: new_formatter, &block )
end

.outputting_to(new_destination, &block) ⇒ Object

Set up an Override with its logging output set to new_destination. If called with a block, call #call immediately on the Override with the block and return the result of that instead.



34
35
36
# File 'lib/loggability/override.rb', line 34

def self::outputting_to( new_destination, &block )
	return self.new( logdev: new_destination, &block )
end

.with_level(new_level, &block) ⇒ Object

Set up an Override with its logging level set to newlevel. If called with a block, call #call immediately on the Override with the block and return the result of that instead.



26
27
28
# File 'lib/loggability/override.rb', line 26

def self::with_level( new_level, &block )
	return self.new( level: new_level, &block )
end

Instance Method Details

#callObject

Call the provided block with configured overrides applied, and then restore the previous settings before control is returned.



108
109
110
111
112
113
# File 'lib/loggability/override.rb', line 108

def call
	self.apply_overrides
	yield
ensure
	self.restore_overridden_settings
end

#for_logger(*hosts, &block) ⇒ Object Also known as: for_loggers

Return a clone of the receiving Override with its log hosts set to hosts (instead of affecting all registered log hosts)

Raises:

  • (ArgumentError)


143
144
145
146
# File 'lib/loggability/override.rb', line 143

def for_logger( *hosts, &block )
	raise ArgumentError, "no log hosts given" if hosts.empty?
	return self.clone_with( log_hosts: hosts, &block )
end

#formatted_with(new_formatter, &block) ⇒ Object

Return a clone of the receiving Override with its logging formatter set to formatter.



136
137
138
# File 'lib/loggability/override.rb', line 136

def formatted_with( new_formatter, &block )
	return self.clone_with( formatter: new_formatter, &block )
end

#initialize_copy(original) ⇒ Object

Copy constructor – make copies of the internal data structures when duplicated.



79
80
81
82
83
# File 'lib/loggability/override.rb', line 79

def initialize_copy( original )
	@log_hosts = original.log_hosts.dup if original.log_hosts
	@settings = original.settings.dup
	@overridden_settings = nil
end

#inspectObject

Return the object as a human-readable string suitable for debugging.



151
152
153
154
155
156
157
158
159
160
# File 'lib/loggability/override.rb', line 151

def inspect
	return "#<%p:%#016x formatter: %s, level: %s, output: %s, affecting %s log hosts>" % [
		self.class,
		self.object_id * 2,
		self.settings[:formatter] || '-',
		self.settings[:level] || '-',
		self.settings[:logdev] ? self.settings[:logdev].class : '-',
		self.log_hosts ? self.log_hosts.length.to_s : "all",
	]
end

#outputting_to(new_destination, &block) ⇒ Object

Return a clone of the receiving Override with its logging output set to new_destination.



129
130
131
# File 'lib/loggability/override.rb', line 129

def outputting_to( new_destination, &block )
	return self.clone_with( logdev: new_destination, &block )
end

#with_level(new_level, &block) ⇒ Object

Return a clone of the receiving Override with its logging level set to newlevel.



122
123
124
# File 'lib/loggability/override.rb', line 122

def with_level( new_level, &block )
	return self.clone_with( level: new_level, &block )
end