Class: Pantheios::Services::StandardLogServiceAdapter

Inherits:
Object
  • Object
show all
Includes:
Xqsr3::Quality::ParameterChecking
Defined in:
lib/pantheios/services/standard_log_service_adapter.rb

Overview

An adapter class that fulfils the Pantheios LogService protocol in terms an instance of the standard Ruby logger

NOTE: The LogService protocol is implemented by a class that provides the instance methods severity_logged?(severity : Object) : boolean and log(severity : Object, t : Time, prefix : String|Array, msg : String)

Constant Summary collapse

STOCK_SEVS_EXT2NUM =
::Pantheios::ApplicationLayer::StockSeverityLevels::STOCK_SEVERITY_LEVEL_VALUES
SEV_LEVELS_INT2PAIR =
{

	::Logger::DEBUG => [ :debug0, STOCK_SEVS_EXT2NUM[:debug0] ],
	::Logger::WARN => [ :warning, STOCK_SEVS_EXT2NUM[:warning] ],
	::Logger::ERROR => [ :failure, STOCK_SEVS_EXT2NUM[:failure] ],
	::Logger::FATAL => [ :alert, STOCK_SEVS_EXT2NUM[:alert] ],
	::Logger::UNKNOWN => [ :violation, STOCK_SEVS_EXT2NUM[:violation] ],
}
SEV_LEVELS_EXT2INT =
{

	:trace => ::Logger::DEBUG,
	:benchmark => ::Logger::DEBUG,

	:debug0 => ::Logger::DEBUG,
	:debug1 => ::Logger::DEBUG,
	:debug2 => ::Logger::DEBUG,
	:debug3 => ::Logger::DEBUG,
	:debug4 => ::Logger::DEBUG,

	:notice => ::Logger::INFO,
	:informational => ::Logger::INFO, :info => ::Logger::INFO,

	:warning => ::Logger::WARN, :warn => ::Logger::WARN,

	:failure => ::Logger::ERROR, :error => ::Logger::ERROR,
	:critical => ::Logger::ERROR,

	:alert => ::Logger::UNKNOWN,
	:violation => ::Logger::UNKNOWN, :emergency => ::Logger::UNKNOWN,
}
SEV_LEVELS_EXT2NUM =
{

	STOCK_SEVS_EXT2NUM[:trace] => ::Logger::DEBUG,
	STOCK_SEVS_EXT2NUM[:benchmark] => ::Logger::DEBUG,

	STOCK_SEVS_EXT2NUM[:debug0] => ::Logger::DEBUG,
	STOCK_SEVS_EXT2NUM[:debug1] => ::Logger::DEBUG,
	STOCK_SEVS_EXT2NUM[:debug2] => ::Logger::DEBUG,
	STOCK_SEVS_EXT2NUM[:debug3] => ::Logger::DEBUG,
	STOCK_SEVS_EXT2NUM[:debug4] => ::Logger::DEBUG,

	STOCK_SEVS_EXT2NUM[:notice] => ::Logger::INFO,
	STOCK_SEVS_EXT2NUM[:informational] => ::Logger::INFO,

	STOCK_SEVS_EXT2NUM[:warning] => ::Logger::WARN,

	STOCK_SEVS_EXT2NUM[:failure] => ::Logger::ERROR,
	STOCK_SEVS_EXT2NUM[:critical] => ::Logger::ERROR,

	STOCK_SEVS_EXT2NUM[:alert] => ::Logger::UNKNOWN,
	STOCK_SEVS_EXT2NUM[:violation] => ::Logger::UNKNOWN,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, adapter_threshold = nil, **options) ⇒ StandardLogServiceAdapter

Returns a new instance of StandardLogServiceAdapter.



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/pantheios/services/standard_log_service_adapter.rb', line 131

def initialize logger, adapter_threshold = nil, **options

	check_parameter logger, 'logger', responds_to: [ :add, :level, :level= ]
	check_parameter adapter_threshold, 'adapter_threshold', types: [ ::Integer, ::Symbol ], allow_nil: true
	format = check_option options, :format, type: ::Symbol, values: [ :default, :simple, :standard ], allow_nil: true

	@logger				=	logger
	@format				=	format || :default
	@adapter_threshold	=	adapter_threshold
	@at_value			=	nil
	@closed				=	false
end

Instance Attribute Details

#adapter_thresholdObject

The threshold of the adapter, as expressed in a Pantheios severity level

NOTE: may be nil, in which case the decision to determine whether to log (in the form of the severity_logged? method) will be defered to the underlying logger.



150
151
152
# File 'lib/pantheios/services/standard_log_service_adapter.rb', line 150

def adapter_threshold
  @adapter_threshold
end

Instance Method Details

#closeObject



170
171
172
173
174
175
176
177
# File 'lib/pantheios/services/standard_log_service_adapter.rb', line 170

def close

	raise "already closed" if @closed

	@logger.close

	@closed = true
end

#flushObject



179
180
181
182
# File 'lib/pantheios/services/standard_log_service_adapter.rb', line 179

def flush

	@logger.flush if @logger.respond_to? :flush
end

#log(severity, t, prefix, msg) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/pantheios/services/standard_log_service_adapter.rb', line 220

def log severity, t, prefix, msg

	sev_ext		=	STOCK_SEVS_EXT2NUM[severity]
	sev_ext		||=	severity if ::Integer === severity
	sev_int		=	SEV_LEVELS_EXT2NUM[sev_ext]
	sev_int		||=	::Logger::UNKNOWN

	case @format
	when :default

		prog_name	=	::Pantheios::Util::ProcessUtil.derive_process_name $0

		@logger.add sev_int, msg, prog_name
	when :simple

		@logger << msg + ?\n
	when :standard

		@logger << "#{prefix}#{msg}\n"
	end
end

#severity_logged?(severity) ⇒ Boolean

Returns:

  • (Boolean)


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/pantheios/services/standard_log_service_adapter.rb', line 184

def severity_logged? severity

	case severity
	when nil

		return true
	when adapter_threshold

		return true
	when ::Symbol

		sev	=	STOCK_SEVS_EXT2NUM[severity] || 0
	when ::Integer

		sev	=	severity
	else

		warn "severity - '#{severity}' - of invalid type (#{severity.class}) specified to severity_logged?"

		return true
	end


	unless adapter_threshold

		# ask the logger

		sym, val = SEV_LEVELS_INT2PAIR[@logger.level]

		return sev <= val
	end


	return sev <= @at_value
end