Class: Siba::SibaLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/siba/siba_logger.rb

Constant Summary collapse

LogLevels =
%w(debug info warn error fatal unknown)

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, path_to_log_file, show_start_message = true) ⇒ SibaLogger

Returns a new instance of SibaLogger.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/siba/siba_logger.rb', line 61

def initialize(name, path_to_log_file, show_start_message = true)
  @show_finish_message = true
  @name = name
  SibaLogger.messages = []
  @loggers = []

  @strlog = StringIO.new
  @loggers << Logger.new(@strlog)

  unless SibaLogger.quiet
    @stdout_log = Logger.new(STDOUT)
    @loggers << stdout_log
  end

  unless path_to_log_file.nil? || SibaLogger.no_log
    @file = File.open(path_to_log_file, "a:utf-8")
    @file_log = Logger.new(file)
    @loggers << file_log
  end

  @loggers.each do |logger|
    logger.formatter = method(:formatter)
  end

  file_log.info "
||----------NEW LOG----------||
|| #{Time.now} ||
||---------------------------||
" unless file_log.nil?

  info "#{name} started" if show_start_message
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/siba/siba_logger.rb', line 134

def method_missing(meth, *args, &block)
  if SibaLogger::log_level? meth.to_s
    log(meth.to_s, *args, &block)
  else
    super
  end
end

Class Attribute Details

.messagesObject

Returns the value of attribute messages.



9
10
11
# File 'lib/siba/siba_logger.rb', line 9

def messages
  @messages
end

.no_logObject

Returns the value of attribute no_log.



9
10
11
# File 'lib/siba/siba_logger.rb', line 9

def no_log
  @no_log
end

.quietObject

Returns the value of attribute quiet.



9
10
11
# File 'lib/siba/siba_logger.rb', line 9

def quiet
  @quiet
end

.verboseObject

Returns the value of attribute verbose.



9
10
11
# File 'lib/siba/siba_logger.rb', line 9

def verbose
  @verbose
end

Instance Attribute Details

#show_finish_messageObject

Returns the value of attribute show_finish_message.



59
60
61
# File 'lib/siba/siba_logger.rb', line 59

def show_finish_message
  @show_finish_message
end

Class Method Details

.check_log_level(level) ⇒ Object



15
16
17
# File 'lib/siba/siba_logger.rb', line 15

def check_log_level(level)
  raise "Unsupported log level '#{level}'" unless SibaLogger::log_level? level
end

.count(severity = nil, exact_level = true) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/siba/siba_logger.rb', line 24

def count(severity=nil, exact_level=true)
  return 0 if SibaLogger.messages.nil?
  return SibaLogger.messages.size if severity.nil?
  severity_i = SibaLogger.level_to_i severity
  SibaLogger.messages.count do |i|
    if exact_level
      i.level == severity_i
    else
      i.level >= severity_i
    end
  end
end

.count_messages(msg, severity = nil, exact_level = true) ⇒ Object

returns the number of log messages that contain given msg text



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/siba/siba_logger.rb', line 38

def count_messages(msg, severity=nil, exact_level=true)
  return 0 if SibaLogger.messages.nil?
  severity_i = SibaLogger.level_to_i severity unless severity.nil?
  SibaLogger.messages.count do |i|
    match_level = true
    if severity_i
      if exact_level
        match_level = i.level == severity_i
      else
        match_level = i.level >= severity_i
      end
    end
    if match_level
      !((i.msg =~ /#{msg}/).nil?)
    else
      false
    end
  end
end

.level_to_i(level) ⇒ Object



19
20
21
22
# File 'lib/siba/siba_logger.rb', line 19

def level_to_i(level)
  check_log_level level
  SibaLogger::LogLevels.index level
end

.log_level?(level) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/siba/siba_logger.rb', line 11

def log_level?(level)
  SibaLogger::LogLevels.include? level
end

Instance Method Details

#closeObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/siba/siba_logger.rb', line 98

def close
  if show_finish_message
    if SibaLogger.count('fatal') > 0
      info "#{name} failed"
    elsif SibaLogger.count('warn', false) == 0
      info "#{name} finished successfully"
    else
      info "#{name} completed with some issues"
    end
  end

  unless file.nil?
    file.close
  end

  unless file_log.nil?
    file_log.close
  end
  @loggers = []
  @strlog = nil
  @file_log = nil
  @file = nil
  @stdout_log = nil
end

#log_exception(exception, log_only_backtrace = false) ⇒ Object



127
128
129
130
131
132
# File 'lib/siba/siba_logger.rb', line 127

def log_exception(exception, log_only_backtrace=false)
  log('debug',exception.message) unless log_only_backtrace
  unless exception.backtrace.nil?
    log('debug',"\n--- stack trace ---\n#{exception.backtrace.join("\n")}\n--- stack trace ---")
  end
end

#respond_to?(meth) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
145
146
147
148
# File 'lib/siba/siba_logger.rb', line 142

def respond_to?(meth)
  if SibaLogger::log_level? meth.to_s
    true
  else
    super
  end
end

#to_sObject



94
95
96
# File 'lib/siba/siba_logger.rb', line 94

def to_s
  strlog.string
end

#warn(*args, &block) ⇒ Object



123
124
125
# File 'lib/siba/siba_logger.rb', line 123

def warn(*args, &block)
  log('warn', *args, &block)
end