Class: BrpmLogger

Inherits:
LoggerBase show all
Defined in:
lib/logging/brpm_logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from LoggerBase

#log_error, #message_box

Constructor Details

#initializeBrpmLogger

Returns a new instance of BrpmLogger.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/logging/brpm_logger.rb', line 7

def initialize
  @step_number = BrpmAuto.params.step_number
  @step_name = BrpmAuto.params.step_name

  @request_log_file_path = "#{BrpmAuto.params.automation_results_dir}/#{BrpmAuto.params.request_id}.log"

  @step_run_log_file_path = "#{BrpmAuto.params.automation_results_dir}/#{BrpmAuto.params.request_id}_#{BrpmAuto.params.step_id}_#{BrpmAuto.params.run_key}.log"
  #@step_run_log_file_path = BrpmAuto.params.output_file

  print "Logging to #{@step_run_log_file_path} and #{@request_log_file_path}\n" unless BrpmAuto.params.also_log_to_console
end

Instance Attribute Details

#request_log_file_pathObject (readonly)

Returns the value of attribute request_log_file_path.



4
5
6
# File 'lib/logging/brpm_logger.rb', line 4

def request_log_file_path
  @request_log_file_path
end

#step_run_log_file_pathObject (readonly)

Returns the value of attribute step_run_log_file_path.



5
6
7
# File 'lib/logging/brpm_logger.rb', line 5

def step_run_log_file_path
  @step_run_log_file_path
end

Instance Method Details

#log(message, with_prefix = true) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/logging/brpm_logger.rb', line 19

def log(message, with_prefix = true)
  message = message.to_s # in case booleans or whatever are passed
  timestamp = "#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}"

  if with_prefix
    prefix = ""
  else
    prefix = "#{timestamp}|#{'%2.2s' % @step_number}|#{'%-20.20s' % @step_name}|"
    message.gsub!("\n", "\n" + (" " * prefix.length))
  end

  log_message = "#{prefix}#{message}\n"

  print(log_message) if BrpmAuto.params.also_log_to_console

  File.open(@request_log_file_path, "a") do |log_file|
    log_file.print(log_message)
  end

  File.open(@step_run_log_file_path, "a") do |log_file|
    log_file.print(log_message)
  end
end