Class: CUKES::CreateLog

Inherits:
Object
  • Object
show all
Includes:
DateTimeLibrary, FileLibrary
Defined in:
lib/friendly/cukes/framework/library/generic/create_log.rb

Instance Method Summary collapse

Methods included from DateTimeLibrary

#format_nonoseconds_to_time, #get_current_datetime, #get_datetime_diff, #get_formatted_datetime

Methods included from FileLibrary

#close_file, #create_html_report, delete_object, #execute_command, #format_file_path, #get_feature_module_name, #get_files_absolute_path, #is_file_exists, #open_excel_file, #open_yml_file, #rename_file_type, #set_datafile_path, #set_scenario_based_datafile

Constructor Details

#initialize(str_logfile_name = "log_") ⇒ CreateLog

Description : initializes the Logger class and creates a new log file Author : Chandra sekaran Arguments :

str_logfile_name: name of the log file to be created


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/friendly/cukes/framework/library/generic/create_log.rb', line 19

def initialize(str_logfile_name = "log_")
  begin
    date_time_format = DATETIME_FORMAT || "%d_%m_%Y-%H_%M_%S"
  rescue Exception => ex
    date_time_format = "%d_%m_%Y-%H_%M_%S"   # when DATETIME_FORMAT is not defined
  end

  begin
    level = LOGGER_LEVEL || "DEBUG"
  rescue Exception => ex
    level = "DEBUG"    # when LOGGER_LEVEL is not defined
  end

  begin
    @str_logdir_name = $current_log_dir || "./test_result/test_report_#{Time.now.strftime(date_time_format)}"       # this line is enough, ex handlng not need
  rescue Exception => ex
    @str_logdir_name = "./test_result/test_report_#{Time.now.strftime(date_time_format)}"
  end

  create_directory(@str_logdir_name)       # creates a new directory
  @log_file = "#{@str_logdir_name}/#{str_logfile_name}.log"
  @log = Logger.new(@log_file)              # creates a new log file
  set_level(level)                          # sets the Logger::Level
  @log.datetime_format = date_time_format   # sets the datetime format
  @log.formatter = lambda do |severity, datetime, progname, message|
    "[#{Time.now.strftime(date_time_format)}] #{severity}: #{message}\n"   # customize the log content
  end
end

Instance Method Details

#create_directory(str_directory_path) ⇒ Object

Description : creates a new directory under the given path (newly added to override the method in file_library.rb) Author : Chandra sekaran Arguments :

str_directory_path : relative path of the directory to be created


130
131
132
133
134
135
# File 'lib/friendly/cukes/framework/library/generic/create_log.rb', line 130

def create_directory(str_directory_path)
  unless File.directory?(str_directory_path)
    FileUtils.mkdir_p(str_directory_path)
    puts "New directory created under : #{str_directory_path}"
  end
end

#debug(message) ⇒ Object

Description : logs a DEBUG message Author : Chandra sekaran Arguments :

message     : string message to be logged


53
54
55
# File 'lib/friendly/cukes/framework/library/generic/create_log.rb', line 53

def debug(message)
  @log.debug(message)
end

#error(message) ⇒ Object

Description : logs an ERROR message Author : Chandra sekaran Arguments :

message     : string message to be logged


80
81
82
83
84
# File 'lib/friendly/cukes/framework/library/generic/create_log.rb', line 80

def error(message)
  @log.error(message)
  #$world.puts(message)  if !$world.nil?
  #exit #raise #"EXCEPTION RAISED"#Cucumber.wants_to_quit = true     # exit(1)
end

#fatal(message) ⇒ Object

Description : logs a FATAL message Author : Chandra sekaran Arguments :

message     : string message to be logged


101
102
103
# File 'lib/friendly/cukes/framework/library/generic/create_log.rb', line 101

def fatal(message)
  @log.fatal(message)
end

#get_current_log_dirObject

Description : returns the relative path of currently created directory Author : Chandra sekaran



140
141
142
# File 'lib/friendly/cukes/framework/library/generic/create_log.rb', line 140

def get_current_log_dir
  @str_logdir_name
end

#get_current_log_fileObject

Description : returns the relative path of currently created file (logfile) Author : Chandra sekaran



147
148
149
# File 'lib/friendly/cukes/framework/library/generic/create_log.rb', line 147

def get_current_log_file
  @log_file
end

#info(message) ⇒ Object

Description : logs an INFO message Author : Chandra sekaran Arguments :

message     : string message to be logged


62
63
64
# File 'lib/friendly/cukes/framework/library/generic/create_log.rb', line 62

def info(message)
  @log.info(message)
end

#set_level(level) ⇒ Object

Description : sets the logger level Author : Chandra sekaran Arguments :

level       : logger level string


110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/friendly/cukes/framework/library/generic/create_log.rb', line 110

def set_level(level)
  case level.upcase
    when "DEBUG"
      @log.level = Logger::DEBUG
    when "INFO"
      @log.level = Logger::INFO
    when "WARN"
      @log.level = Logger::WARN
    when "ERROR"
      @log.level = Logger::ERROR
    when "FATAL"
      @log.level = Logger::FATAL
  end
end

#success(message) ⇒ Object

Description : logs a success message Author : Chandra sekaran Arguments :

message     : string message to be logged


91
92
93
94
# File 'lib/friendly/cukes/framework/library/generic/create_log.rb', line 91

def success(message)
  info(message)
  $world.puts(message)  if !$world.nil?
end

#warn(message) ⇒ Object

Description : logs a WARN message Author : Chandra sekaran Arguments :

message     : string message to be logged


71
72
73
# File 'lib/friendly/cukes/framework/library/generic/create_log.rb', line 71

def warn(message)
  @log.warn(message)
end