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 do |s, t, p, m|
  "%5s [%s] (%s) %s :: %s\n" % [s, t.strftime('%Y-%m-%d %H:%M:%S'), $$, p, m]
end
@@logfile =

Initial state

DEFAULT_LOGFILE

Class Method Summary collapse

Class Method Details

.debug(msg) ⇒ Object



91
92
93
# File 'lib/lyber_core/log.rb', line 91

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

.error(msg) ⇒ Object



79
80
81
# File 'lib/lyber_core/log.rb', line 79

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

.exception(exc) ⇒ Object



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

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

.exception_message(exc) ⇒ Object



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

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

.fatal(msg) ⇒ Object



75
76
77
# File 'lib/lyber_core/log.rb', line 75

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

.info(msg) ⇒ Object



87
88
89
# File 'lib/lyber_core/log.rb', line 87

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

.levelObject

Return the current log level



71
72
73
# File 'lib/lyber_core/log.rb', line 71

def Log.level
  @@log.level
end

.logfileObject

The current location of the logfile



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

def Log.logfile
  @@logfile
end

.restore_defaultsObject

Restore LyberCore::Log to its default state



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

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


57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lyber_core/log.rb', line 57

def Log.set_level(loglevel)
  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

.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.



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

def Log.set_logfile(new_logfile)
  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

.warn(msg) ⇒ Object



83
84
85
# File 'lib/lyber_core/log.rb', line 83

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