Class: JobManager::ApplicationIOLogger

Inherits:
ApplicationLogger show all
Defined in:
lib/jobmanager/applicationlogger.rb

Overview

This class provides a Logger interface and logs messages to an IO stream. In addition, it provides a string instance method which allows the caller to retrieve a concatenation of all the messages written to this logger (with additional formatting such that the string represents exactly what was written to the IO stream). This class is implemented using the Logger class itself. The user name, and job name are prepended to each message, which is then written via the Logger class (to a string stream) which is in turn written to the IO stream. This was done so that the exact string that was logged to the IO stream could be later returned via the string instance method. A number of the interface methods are implemented via the method_missing method.

Direct Known Subclasses

ApplicationFileLogger

Constant Summary collapse

LOGGER_LEVELS =
[ :debug,
  :info,
  :warn,
  :error,
  :fatal,
  :unknown 
]

Constants inherited from ApplicationLogger

JobManager::ApplicationLogger::LEVEL_LOGGER_MAP

Instance Attribute Summary

Attributes inherited from ApplicationLogger

#job_name, #user_name

Instance Method Summary collapse

Methods inherited from ApplicationLogger

#record_exception, #record_tagged_exception, #write

Constructor Details

#initialize(io_stream, user_name, job_name = nil) ⇒ ApplicationIOLogger

Description:

This method creates a new instance.



182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/jobmanager/applicationlogger.rb', line 182

def initialize(io_stream,
               user_name, 
               job_name = nil)

  super(job_name, user_name)
  
  @copy = StringIO.new
  @io_stream = io_stream
  
  @logger_stringio = StringIO.new
  @logger = Logger.new(@logger_stringio)
  @logger.formatter = Logger::Formatter.new
  @logger.datetime_format = '%FT%T '
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object (private)

Description:

This method is intended to handle all shortcut methods (debug, warn, info, etc.), as well as the level attribute methods.



254
255
256
257
258
259
260
261
262
263
264
# File 'lib/jobmanager/applicationlogger.rb', line 254

def method_missing(*args, &block)
  method = args.shift

  # if the method is a shortcut log method
  if (LOGGER_LEVELS.find() {|key| method == key})
    wrap_record_send(method, args[0] ? args[0] : yield)
  else
    # forward the method directly onto the Logger instance.
    @logger.send(method, *args, &block)
  end
end

Instance Method Details

#add(severity, message = nil, &block) ⇒ Object Also known as: log

Description:

This method is akin to the Logger::add interface method.



210
211
212
# File 'lib/jobmanager/applicationlogger.rb', line 210

def add(severity, message = nil, &block)
  wrap_record_send(LEVEL_LOGGER_MAP[severity], message || yield)
end

#closeObject

Description:

This method exists only to complete the Logger interface. It is a noop.



220
221
# File 'lib/jobmanager/applicationlogger.rb', line 220

def close
end

#stringObject

Returns:

A string containing all mesages that were logged through this interface.



202
203
204
# File 'lib/jobmanager/applicationlogger.rb', line 202

def string()
  return @copy.string()
end