Class: RallyLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/vcseif/utils/rally_logger.rb

Constant Summary collapse

VCSEIF_MAX_LOGFILES =
10
VCSEIF_MAX_LOGFILE_SIZE =

5 MB

5*1024*1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logfile_name = nil, max_logfiles = VCSEIF_MAX_LOGFILES, max_file_size = VCSEIF_MAX_LOGFILE_SIZE) ⇒ RallyLogger

Returns a new instance of RallyLogger.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/vcseif/utils/rally_logger.rb', line 23

def initialize(logfile_name=nil, max_logfiles=VCSEIF_MAX_LOGFILES, max_file_size=VCSEIF_MAX_LOGFILE_SIZE)
  if logfile_name.nil?
    @logger = Logger.new(STDOUT)
  else
    @logger = Logger.new(logfile_name, max_logfiles, max_file_size)
  end 

  @cache_errors    = false
  @cache_warnings  = false
  @errors_cache    = []
  @warnings_cache  = []

  @logger.formatter = CustomLogFormat.new  # Install custom formatter
  @logger.level = Logger::DEBUG
end

Instance Attribute Details

#cache_errors(cache_on) ⇒ Object

def exception(ex)

  if (ex.kind_of? RecoverableException)
    warn("Message " + ex.message)
  else
    error("Message " + ex.message)
    error( "Stack Trace")
    ex.backtrace.each {|trace| write(trace) }
  end
end


120
121
122
# File 'lib/vcseif/utils/rally_logger.rb', line 120

def cache_errors
  @cache_errors
end

#cache_warnings(cache_on) ⇒ Object

Returns the value of attribute cache_warnings.



17
18
19
# File 'lib/vcseif/utils/rally_logger.rb', line 17

def cache_warnings
  @cache_warnings
end

#errors_cacheObject

Returns the value of attribute errors_cache.



17
18
19
# File 'lib/vcseif/utils/rally_logger.rb', line 17

def errors_cache
  @errors_cache
end

#loggerObject (readonly)

Returns the value of attribute logger.



18
19
20
# File 'lib/vcseif/utils/rally_logger.rb', line 18

def logger
  @logger
end

#warnings_cacheObject

Returns the value of attribute warnings_cache.



17
18
19
# File 'lib/vcseif/utils/rally_logger.rb', line 17

def warnings_cache
  @warnings_cache
end

Instance Method Details

#clear_caches_and_flagsObject



136
137
138
139
140
141
# File 'lib/vcseif/utils/rally_logger.rb', line 136

def clear_caches_and_flags
  @warnings_cache  = []
  @errors_cache    = []
  @cache_errors    = false
  @cache_warnings  = false
end

#debug(text, exception = nil) ⇒ Object



90
91
92
93
# File 'lib/vcseif/utils/rally_logger.rb', line 90

def debug(text, exception=nil)
  context = !exception.nil? ? exception_context(exception) : nominal_context()
  @logger.debug("#{context} - " + text)
end

#entry(text) ⇒ Object



104
105
106
107
108
# File 'lib/vcseif/utils/rally_logger.rb', line 104

def entry(text)
  timestamp = Time.now.utc.strftime("%Y-%m-%d %H:%M:%S Z")
  twig = "[%s] %s" % [timestamp, text]
  write(twig)
end

#error(text, exception = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/vcseif/utils/rally_logger.rb', line 62

def error(text, exception=nil)
  context = !exception.nil? ? exception_context(exception) : nominal_context()
  message = "#{context} - " + text
  @logger.error(message)
  if @cache_errors
    datetime = Time.now.utc.strftime("%Y-%m-%d %H:%M:%S Z")
    info = {:time => datetime, :message => message}
    @errors_cache.push(info)
  end
end

#fatal(text, exception = nil) ⇒ Object



57
58
59
60
# File 'lib/vcseif/utils/rally_logger.rb', line 57

def fatal(text, exception=nil)
  context = !exception.nil? ? exception_context(exception) : nominal_context()
  @logger.fatal("#{context} - " + text)
end

#info(text, exception = nil) ⇒ Object



86
87
88
# File 'lib/vcseif/utils/rally_logger.rb', line 86

def info(text, exception=nil)
  @logger.info("#{nominal_context()} - " + text)
end

#set_level(level) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vcseif/utils/rally_logger.rb', line 40

def set_level(level)
  case level
    when 'Fatal'
      @logger.level = Logger::FATAL
    when 'Error'
      @logger.level = Logger::ERROR
    when 'Warn'
      @logger.level = Logger::WARN
    when 'Warning'
      @logger.level = Logger::WARN
    when 'Info'
      @logger.level = Logger::INFO
    when 'Debug'
      @logger.level = Logger::DEBUG
  end
end

#unknown(text, exception = nil) ⇒ Object



95
96
97
98
# File 'lib/vcseif/utils/rally_logger.rb', line 95

def unknown(text, exception=nil)
  context = !exception.nil? ? exception_context(exception) : nominal_context()
  @logger.unknown("#{context} - " + text)
end

#warning(text, exception = nil) ⇒ Object Also known as: warn



73
74
75
76
77
78
79
80
81
82
# File 'lib/vcseif/utils/rally_logger.rb', line 73

def warning(text, exception=nil)
  context = !exception.nil? ? exception_context(exception) : nominal_context()
  message = "#{context} - " + text
  @logger.warn(message)
  if @cache_warnings
    datetime = Time.now.utc.strftime("%Y-%m-%d %H:%M:%S Z")
    info = {:time => datetime, :message => message}
    @warnings_cache.push(info)
  end
end

#write(text) ⇒ Object



100
101
102
# File 'lib/vcseif/utils/rally_logger.rb', line 100

def write(text)
  @logger << (text + "\n")
end