Class: Spectre::Logging::File

Inherits:
Object
  • Object
show all
Defined in:
lib/spectre/logging/file.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ File

Returns a new instance of File.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/spectre/logging/file.rb', line 4

def initialize config
  raise 'No log format section in config for console logger' unless config.key? 'log_format' and config['log_format'].key? 'file'

  @config = config['log_format']['file']
  @fmt_start_group = @config['start_group']
  @fmt_end_group = @config['end_group']
  @fmt_sep = @config['separator']

  @file_log = ::Logger.new(config['log_file'], progname: 'spectre')
  @file_log.level = config['debug'] ? 'DEBUG' : 'INFO'
end

Instance Method Details

#end_context(context) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/spectre/logging/file.rb', line 32

def end_context context
  if context and context.__desc
    @file_log.debug("context '#{context.__desc}' finished")
  else
    @file_log.debug("main context finished of #{context.__subject.desc}")
  end
end

#end_group(desc) ⇒ Object



63
64
65
66
# File 'lib/spectre/logging/file.rb', line 63

def end_group desc
  desc = @fmt_end_group.gsub('<desc>', desc) if @fmt_end_group
  @file_log.info(desc)
end

#end_spec(spec, data = nil) ⇒ Object



46
47
48
49
50
51
# File 'lib/spectre/logging/file.rb', line 46

def end_spec spec, data=nil
  log_msg = "running spec [#{spec.name}] '#{spec.desc}'"
  log_msg += " with data #{data}" if data
  log_msg += " finished"
  @file_log.debug(log_msg)
end

#end_subject(subject) ⇒ Object



20
21
22
# File 'lib/spectre/logging/file.rb', line 20

def end_subject subject
  @file_log.debug("subject '#{subject.desc}' finished")
end

#log_debug(message) ⇒ Object



76
77
78
# File 'lib/spectre/logging/file.rb', line 76

def log_debug message
  @file_log.debug("#{Status::DEBUG} #{message}")
end

#log_error(spec, exception) ⇒ Object



80
81
82
83
# File 'lib/spectre/logging/file.rb', line 80

def log_error spec, exception
  file, line = exception.backtrace[0].match(/(.*\.rb):(\d+)/).captures
  @file_log.error("An unexpected error occurred at '#{file}:#{line}' while running spec '#{spec.name}': [#{exception.class}] #{exception.message}\n#{exception.backtrace.join "\n"}")
end

#log_info(message) ⇒ Object



72
73
74
# File 'lib/spectre/logging/file.rb', line 72

def log_info message
  @file_log.info("#{Status::INFO} #{message}")
end

#log_process(desc) ⇒ Object



68
69
70
# File 'lib/spectre/logging/file.rb', line 68

def log_process desc
  @file_log.debug(desc)
end

#log_separator(desc) ⇒ Object



53
54
55
56
# File 'lib/spectre/logging/file.rb', line 53

def log_separator desc
  desc = @fmt_sep.gsub('<desc>', desc) if @fmt_sep
  @file_log.info(desc)
end

#log_skipped(spec, message = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/spectre/logging/file.rb', line 85

def log_skipped spec, message=nil
  txt = "spec '#{spec.desc}' skipped"

  unless message.nil?
    txt += ': ' + message
  end

  @file_log.warn(txt)
end

#log_status(desc, status, annotation = nil) ⇒ Object



95
96
97
98
99
# File 'lib/spectre/logging/file.rb', line 95

def log_status desc, status, annotation=nil
  msg = "expected #{desc}...#{status.upcase}"
  msg += " - #{annotation}" if annotation
  @file_log.debug(msg)
end

#start_context(context) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/spectre/logging/file.rb', line 24

def start_context context
  if context and context.__desc
    @file_log.debug("start running context '#{context.__desc}'")
  else
    @file_log.debug("start running main context of #{context.__subject.desc}")
  end
end

#start_group(desc) ⇒ Object



58
59
60
61
# File 'lib/spectre/logging/file.rb', line 58

def start_group desc
  desc = @fmt_start_group.gsub('<desc>', desc) if @fmt_start_group
  @file_log.info(desc)
end

#start_spec(spec, data = nil) ⇒ Object



40
41
42
43
44
# File 'lib/spectre/logging/file.rb', line 40

def start_spec spec, data=nil
  log_msg = "start running spec [#{spec.name}] '#{spec.desc}'"
  log_msg += " with data #{data}" if data
  @file_log.debug(log_msg)
end

#start_subject(subject) ⇒ Object



16
17
18
# File 'lib/spectre/logging/file.rb', line 16

def start_subject subject
  @file_log.debug("start running subject '#{subject.desc}'")
end