Class: GenericMonitorLog

Inherits:
Object
  • Object
show all
Defined in:
lib/gml/generic_monitor_log.rb

Overview

This class provides a generic and simple way to log ruby code.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GenericMonitorLog

The class constructor.

Raises:

  • (StandardError)


13
14
15
16
17
# File 'lib/gml/generic_monitor_log.rb', line 13

def initialize(options = {})
  raise StandardError.new("'options' must be a Hash or nil.") if !options.is_a?(Hash)

  @options = DEFAULT_OPTIONS.merge(options)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/gml/generic_monitor_log.rb', line 8

def options
  @options
end

Instance Method Details

#entry(log_level = Gml::LOG_LEVEL_INFO, log_entry = "", caller_depth = 1) ⇒ Object

The main method that logs the messages.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gml/generic_monitor_log.rb', line 22

def entry(log_level = Gml::LOG_LEVEL_INFO, log_entry = "", caller_depth = 1)
  if ((log_level <= @options[:log_level]) && (@options[:console] || @options[:output]))
    at = caller_locations()[caller_depth]
    out =   "[#{Time.now.strftime(@options[:time_format])}] #{Gml::LOG_STRINGS[log_level]} "
    out <<  "[#{File.basename(at.absolute_path)}->#{at.label}: #{at.lineno}]"
    out <<  " - " if log_entry.size > 0
    out <<  log_entry

    @options[:console].puts(out) if @options[:console]

    if (@options[:output])
      if (@options[:output].is_a?(IO))
        @options[:output].puts(out)

      else
        out << "\n"
        File.write(@options[:output], out, (File.exist?(@options[:output]) ? File.size(@options[:output]) : 0))
      end

    end

  end
end

#log_debug(log_entry = "") ⇒ Object

Log a debug message. This is a convenience method.



72
73
74
# File 'lib/gml/generic_monitor_log.rb', line 72

def log_debug(log_entry = "")
  entry(Gml::LOG_LEVEL_DEBUG, log_entry, 1)
end

#log_info(log_entry = "") ⇒ Object Also known as: log

Log an info message. This is a convenience method.



56
57
58
# File 'lib/gml/generic_monitor_log.rb', line 56

def log_info(log_entry = "")
  entry(Gml::LOG_LEVEL_INFO, log_entry, 1)
end

#log_panic(log_entry = "") ⇒ Object

Log a panic message. This is a convenience method.



49
50
51
# File 'lib/gml/generic_monitor_log.rb', line 49

def log_panic(log_entry = "")
  entry(Gml::LOG_LEVEL_PANIC, log_entry, 1)
end

#log_trace(log_entry = "") ⇒ Object

Log a trace message. This is a convenience method.



79
80
81
# File 'lib/gml/generic_monitor_log.rb', line 79

def log_trace(log_entry = "")
  entry(Gml::LOG_LEVEL_TRACE, log_entry, 1)
end

#log_verbose(log_entry = "") ⇒ Object

Log a verbose message. This is a convenience method.



65
66
67
# File 'lib/gml/generic_monitor_log.rb', line 65

def log_verbose(log_entry = "")
  entry(Gml::LOG_LEVEL_VERBOSE, log_entry, 1)
end