Class: RFlow::Logger

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Log4r
Defined in:
lib/rflow/logger.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, include_stdout = false) ⇒ Logger

Returns a new instance of Logger.



33
34
35
# File 'lib/rflow/logger.rb', line 33

def initialize(config, include_stdout = false)
  reconfigure(config, include_stdout)
end

Instance Attribute Details

#context_widthInteger

For the current logging context, how wide the field is where we’re going to write the context/process name.

Returns:

  • (Integer)


23
24
25
# File 'lib/rflow/logger.rb', line 23

def context_width
  @context_width
end

Instance Method Details

#add_logging_context(context) ⇒ void

This method returns an undefined value.

Add more logging context to the stack.



119
120
121
# File 'lib/rflow/logger.rb', line 119

def add_logging_context(context)
  Log4r::NDC.push context
end

#apply_logging_context(context) ⇒ void

This method returns an undefined value.

Replace the current logging context.



107
108
109
# File 'lib/rflow/logger.rb', line 107

def apply_logging_context(context)
  Log4r::NDC.inherit(context)
end

#clear_logging_contextvoid

This method returns an undefined value.

Clear the current logging context.



113
114
115
# File 'lib/rflow/logger.rb', line 113

def clear_logging_context
  Log4r::NDC.clear
end

#clone_logging_contextvoid

This method returns an undefined value.

Clone the logging context so changes to it will not affect the exiting logging context.



101
102
103
# File 'lib/rflow/logger.rb', line 101

def clone_logging_context
  Log4r::NDC.clone_stack
end

#closevoid

This method returns an undefined value.

Close the logger.



66
67
68
# File 'lib/rflow/logger.rb', line 66

def close
  Outputter['rflow.log_file'].close
end

#dump_threadsvoid

This method returns an undefined value.

Send a complete thread dump of the current process out to the logger.



89
90
91
92
93
94
95
96
# 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) ⇒ void

This method returns an undefined value.

Update the log level.



72
73
74
# 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) ⇒ void

This method returns an undefined value.

Reconfigure the log file.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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

#reopenvoid

This method returns an undefined value.

Reopen the logs at their configured filesystem locations. Presumably the previous log files have been renamed by now.



57
58
59
60
61
62
# 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_levelvoid

This method returns an undefined value.

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.



79
80
81
82
83
84
85
# 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