lib/rflow/logger.rb in rflow-1.0.0a1 vs lib/rflow/logger.rb in rflow-1.0.0a2
- old
+ new
@@ -7,42 +7,34 @@
LOG_PATTERN_FORMAT = '%l [%d] %x (%p) - %M'
DATE_METHOD = 'xmlschema(6)'
LOG_PATTERN_FORMATTER = PatternFormatter.new :pattern => LOG_PATTERN_FORMAT, :date_method => DATE_METHOD
- attr_accessor :config, :internal_logger
- attr_accessor :log_file_path, :log_level
+ private
+ attr_accessor :internal_logger
+ attr_accessor :log_file_path, :log_level, :log_name
+ public
+
+ # make sure Log4r is initialized; ignored if custom levels are already set
+ Log4r.define_levels(*Log4rConfig::LogLevels)
+
# Delegate log methods to internal logger
- def_delegators :@internal_logger, :fatal, :error, :warn, :info, :debug
+ def_delegators :@internal_logger,
+ *Log4r::LNAMES.map(&:downcase).map(&:to_sym),
+ *Log4r::LNAMES.map(&:downcase).map {|n| "#{n}?".to_sym }
- def initialize(config, include_stdout=nil)
+ def initialize(config, include_stdout = false)
@log_file_path = config['rflow.log_file_path']
@log_level = config['rflow.log_level']
+ @log_name = (config['rflow.application_name'] || File.basename(log_file_path))
- log_name = (config['rflow.application_name'] rescue File.basename(log_file_path))
- @internal_logger = Log4r::Logger.new(log_name)
+ establish_internal_logger
+ hook_up_logfile
+ hook_up_stdout if include_stdout
+ register_logging_context
- internal_logger.level = LNAMES.index log_level
- internal_logger.trace = true
-
- begin
- internal_logger.add FileOutputter.new('rflow.log_file', :filename => log_file_path, :formatter => LOG_PATTERN_FORMATTER)
- rescue Exception => e
- error_message = "Log file '#{File.expand_path log_file_path}' problem: #{e.message}\b#{e.backtrace.join("\n")}"
- RFlow.logger.error error_message
- raise ArgumentError, error_message
- end
-
- if include_stdout
- internal_logger.add StdoutOutputter.new('rflow_stdout', :formatter => LOG_PATTERN_FORMATTER)
- end
-
- # RFlow.logger.info "Transitioning to running log file #{log_file_path} at level #{log_level}"
- Log4r::NDC.clear
- Log4r::NDC.push(log_name)
-
internal_logger
end
def reopen
# TODO: Make this less of a hack, although Log4r doesn't support
@@ -61,7 +53,31 @@
internal_logger.warn "Changing log level from #{original_log_level} to #{new_log_level}"
internal_logger.level = LNAMES.index new_log_level
end
+ private
+ 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
+
+ def hook_up_logfile
+ begin
+ internal_logger.add FileOutputter.new('rflow.log_file', :filename => log_file_path, :formatter => LOG_PATTERN_FORMATTER)
+ rescue Exception => e
+ raise ArgumentError, "Log file '#{File.expand_path log_file_path}' problem: #{e.message}\b#{e.backtrace.join("\n")}"
+ end
+ end
+
+ def hook_up_stdout
+ internal_logger.add StdoutOutputter.new('rflow_stdout', :formatter => LOG_PATTERN_FORMATTER)
+ end
+
+ def register_logging_context
+ Log4r::NDC.clear
+ Log4r::NDC.push(log_name)
+ end
end
end