class RFlow::Logger

The customized logger for RFlow applications that flows to the configured log file.

Constants

DATE_METHOD

@!visibility private

LOG_PATTERN_FORMAT

@!visibility private

LOG_PATTERN_FORMATTER

@!visibility private

Attributes

context_width[RW]

For the current logging context, how wide the field is where we're going to write the context/process name. @return [Integer]

internal_logger[RW]
log_file_path[RW]
log_level[RW]
log_name[RW]

Public Class Methods

new(config, include_stdout = false) click to toggle source
# File lib/rflow/logger.rb, line 33
def initialize(config, include_stdout = false)
  reconfigure(config, include_stdout)
end

Public Instance Methods

add_logging_context(context) click to toggle source

Add more logging context to the stack. @return [void]

# File lib/rflow/logger.rb, line 119
def add_logging_context(context)
  Log4r::NDC.push context
end
apply_logging_context(context) click to toggle source

Replace the current logging context. @return [void]

# File lib/rflow/logger.rb, line 107
def apply_logging_context(context)
  Log4r::NDC.inherit(context)
end
clear_logging_context() click to toggle source

Clear the current logging context. @return [void]

# File lib/rflow/logger.rb, line 113
def clear_logging_context
  Log4r::NDC.clear
end
clone_logging_context() click to toggle source

Clone the logging context so changes to it will not affect the exiting logging context. @return [void]

# File lib/rflow/logger.rb, line 101
def clone_logging_context
  Log4r::NDC.clone_stack
end
close() click to toggle source

Close the logger. @return [void]

# File lib/rflow/logger.rb, line 66
def close
  Outputter['rflow.log_file'].close
end
dump_threads() click to toggle source

Send a complete thread dump of the current process out to the logger. @return [void]

# File lib/rflow/logger.rb, line 89
def dump_threads
  Thread.list.each do |t|
    info "Thread #{t.inspect}:"
    t.backtrace.each {|b| info "  #{b}" }
    info '---'
  end
  info 'Thread dump complete.'
end
level=(level) click to toggle source

Update the log level. @return [void]

# File lib/rflow/logger.rb, line 72
def level=(level)
  internal_logger.level = LNAMES.index(level.to_s) || level
end
reconfigure(config, include_stdout = false) click to toggle source

Reconfigure the log file. @return [void]

# File lib/rflow/logger.rb, line 39
def reconfigure(config, include_stdout = false)
  @log_file_path = config['rflow.log_file_path']
  @log_level = config['rflow.log_level'] || 'WARN'
  @log_name = if config['rflow.application_name']; config['rflow.application_name']
              elsif log_file_path; File.basename(log_file_path)
              else ''; end

  establish_internal_logger
  hook_up_logfile
  hook_up_stdout if include_stdout
  register_logging_context

  internal_logger
end
reopen() click to toggle source

Reopen the logs at their configured filesystem locations. Presumably the previous log files have been renamed by now. @return [void]

# File lib/rflow/logger.rb, line 57
def reopen
  # TODO: Make this less of a hack, although Log4r doesn't support
  # it, so it might be permanent
  log_file = Outputter['rflow.log_file'].instance_variable_get(:@out)
  File.open(log_file.path, 'a') { |tmp_log_file| log_file.reopen(tmp_log_file) }
end
toggle_log_level() click to toggle source

Toggle the log level between DEBUG and whatever the default is. The previous level is saved to be toggled back the next time this method is called. @return [void]

# File lib/rflow/logger.rb, line 79
def toggle_log_level
  original_log_level = LNAMES[internal_logger.level]
  new_log_level = (original_log_level == 'DEBUG' ? log_level : 'DEBUG')

  internal_logger.warn "Changing log level from #{original_log_level} to #{new_log_level}"
  internal_logger.level = LNAMES.index new_log_level
end

Private Instance Methods

establish_internal_logger() click to toggle source
# File lib/rflow/logger.rb, line 124
def establish_internal_logger
  @internal_logger = Log4r::Logger.new(log_name).tap do |logger|
    logger.level = LNAMES.index log_level
    logger.trace = true
  end
end
hook_up_logfile() click to toggle source
# File lib/rflow/logger.rb, line 131
def hook_up_logfile
  if log_file_path
    begin
      internal_logger.add FileOutputter.new('rflow.log_file', :filename => log_file_path, :formatter => LOG_PATTERN_FORMATTER)
    rescue Exception => e
      # at least output the failure to stderr
      internal_logger.add StderrOutputter.new('rflow_stderr', :formatter => LOG_PATTERN_FORMATTER)
      raise ArgumentError, "Log file '#{File.expand_path log_file_path}' problem: #{e.message}"
    end
  end
end
hook_up_stdout() click to toggle source
# File lib/rflow/logger.rb, line 143
def hook_up_stdout
  internal_logger.add StdoutOutputter.new('rflow_stdout', :formatter => LOG_PATTERN_FORMATTER)
end
register_logging_context() click to toggle source
# File lib/rflow/logger.rb, line 147
def register_logging_context
  Log4r::NDC.clear
  Log4r::NDC.push(log_name)
end