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.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/siba/siba_logger.rb', line 40

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



113
114
115
116
117
118
119
# File 'lib/siba/siba_logger.rb', line 113

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.



38
39
40
# File 'lib/siba/siba_logger.rb', line 38

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

.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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/siba/siba_logger.rb', line 77

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



106
107
108
109
110
111
# File 'lib/siba/siba_logger.rb', line 106

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)


121
122
123
124
125
126
127
# File 'lib/siba/siba_logger.rb', line 121

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

#to_sObject



73
74
75
# File 'lib/siba/siba_logger.rb', line 73

def to_s
  strlog.string
end

#warn(*args, &block) ⇒ Object



102
103
104
# File 'lib/siba/siba_logger.rb', line 102

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