Class: LyberCore::Log

Inherits:
Object
  • Object
show all
Defined in:
lib/lyber_core/log.rb

Constant Summary collapse

DEFAULT_LOGFILE =

Default values

"/tmp/lybercore_log.log"
DEFAULT_LOG_LEVEL =

TODO change to STDOUT?

Logger::INFO
DEFAULT_FORMATTER =
proc{|s,t,p,m|"%5s [%s] (%s) %s :: %s\n" % [s,
t.strftime("%Y-%m-%d %H:%M:%S"), $$, p, m]}
@@logfile =

Initial state

DEFAULT_LOGFILE

Class Method Summary collapse

Class Method Details

.debug(msg) ⇒ Object



97
98
99
# File 'lib/lyber_core/log.rb', line 97

def Log.debug(msg)
  @@log.add(Logger::DEBUG) { msg }
end

.error(msg) ⇒ Object



85
86
87
# File 'lib/lyber_core/log.rb', line 85

def Log.error(msg)
  @@log.add(Logger::ERROR) { msg }
end

.exception(e) ⇒ Object



101
102
103
104
# File 'lib/lyber_core/log.rb', line 101

def Log.exception(e)
  msg = Log.exception_message(e)
  Log.error(msg)
end

.exception_message(e) ⇒ Object



106
107
108
109
# File 'lib/lyber_core/log.rb', line 106

def Log.exception_message(e)
  msg = e.inspect.split($/).join('; ') + "\n"
  msg << e.backtrace.join("\n") if(e.backtrace)
end

.fatal(msg) ⇒ Object



81
82
83
# File 'lib/lyber_core/log.rb', line 81

def Log.fatal(msg)
  @@log.add(Logger::FATAL) { msg }
end

.info(msg) ⇒ Object



93
94
95
# File 'lib/lyber_core/log.rb', line 93

def Log.info(msg)
  @@log.add(Logger::INFO) { msg }
end

.levelObject

Return the current log level



77
78
79
# File 'lib/lyber_core/log.rb', line 77

def Log.level
  @@log.level
end

.logfileObject

The current location of the logfile



29
30
31
# File 'lib/lyber_core/log.rb', line 29

def Log.logfile
  return @@logfile
end

.restore_defaultsObject

Restore LyberCore::Log to its default state



22
23
24
25
26
# File 'lib/lyber_core/log.rb', line 22

def Log.restore_defaults
  @@log.level = DEFAULT_LOG_LEVEL
  Log.set_logfile(DEFAULT_LOGFILE)
  @@log.formatter = DEFAULT_FORMATTER
end

.set_level(loglevel) ⇒ Object

Set the log level. See ruby-doc.org/core/classes/Logger.html for more info. Possible values are:

Logger::FATAL (4):  an unhandleable error that results in a program crash
Logger::ERROR (3):  a handleable error condition
Logger::WARN (2): a warning
Logger::INFO (1): generic (useful) information about system operation
Logger::DEBUG (0):  low-level information for developers


61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/lyber_core/log.rb', line 61

def Log.set_level(loglevel)
 begin
    if [0,1,2,3,4].include? loglevel
      @@log.level = loglevel
      @@log.debug "Setting LyberCore::Log.level to #{loglevel}"
    else
      @@log.warn "I received an invalid option for log level. I expected a number between 0 and 4 but I got #{loglevel}"
      @@log.warn "I'm setting the loglevel to 0 (debug) because you seem to be having trouble."
      @@log.level = 0
    end
  rescue Exception => e
    raise e, "Couldn't set log level because\n#{e.message}: #{e.backtrace.join(%{\n})}"
  end
end

.set_logfile(new_logfile) ⇒ Object

Accepts a filename as an argument, and checks to see whether that file can be opened for writing. If it can be opened, it closes the existing Logger object and re-opens it with the new logfile location. It raises an exception if it cannot write to the specified logfile.



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/lyber_core/log.rb', line 39

def Log.set_logfile(new_logfile)
  begin
    current_log_level = @@log.level
    current_formatter = @@log.formatter
    @@log = Logger.new(new_logfile)
    @@logfile = new_logfile
    @@log.level = current_log_level
    @@log.formatter = current_formatter
  rescue Exception => e
    raise e, "Couldn't initialize logfile #{new_logfile} because\n#{e.message}: #{e.backtrace.join(%{\n})}}"
  end

end

.warn(msg) ⇒ Object



89
90
91
# File 'lib/lyber_core/log.rb', line 89

def Log.warn(msg)
  @@log.add(Logger::WARN) { msg }
end