class GunnyLog
GunnyLog
logs messages to stdout, stderr, or a file. It defaults to stdout. GunnyLog
is a singleton and uses the instance method. For example you can use GunnyLog.instance
.msg('Error message') or you can use log = GunnyLog.instance
GunnyLog
logs messages to stdout, stderr, or a file. It defaults to stdout. GunnyLog
is a singleton and uses the instance method. For example you can use GunnyLog.instance
.msg('Error message') or you can use log = GunnyLog.instance
Constants
Attributes
Is file open flag
@return [bool] is logging enabled
File used for the log file
@return [int] logging level
@return [string] message location
Public Class Methods
Instance method for GunnyLog
@return [GunnyLog] instance
# File lib/GunnyLog.rb, line 204 def self.instance @@instance ||= new end
initialize
# File lib/GunnyLog.rb, line 212 def initialize #local_debug('initialize') @logging_enabled = true @logging_level = DEBUG #@logging_severity = GunnyLogSeverity::DEBUG #local_debug('initialize', sprintf('Log level = %d',@logging_severity) ) @logging_location = 'MainMethod' @file_open = false @logging_file = STDOUT end
Public Instance Methods
Close the logfile
# File lib/GunnyLog.rb, line 106 def close begin @logging_file.close @file_open = false @logging_file = STDOUT rescue SystemCallError => exc handle_exception(exc) end end
Write DEBUG
message to logfile @param loc [string] message logging location, optional @param msg [string] message format string
# File lib/GunnyLog.rb, line 163 def log_debug(loc = nil, msg) log(DEBUG, loc, msg) end
Write ERROR
message to logfile @param loc [string] message logging location, optional @param msg [string] message format string
# File lib/GunnyLog.rb, line 184 def log_error(loc = nil, msg) log(ERROR, loc, msg) end
Write FATAL
message to logfile @param loc [string] message logging location, optional @param msg [string] message format string
# File lib/GunnyLog.rb, line 191 def log_fatal(loc = nil, msg) log(FATAL, loc, msg) end
Write INFO
message to logfile @param loc [string] message logging location, optional @param msg [string] message format string
# File lib/GunnyLog.rb, line 170 def log_info(loc = nil, msg) log(INFO, loc, msg) end
Write UNKOWN message to logfile @param loc [string] message logging location, optional @param msg [string] message format string
# File lib/GunnyLog.rb, line 198 def log_unknown(loc = nil, msg) log(UNKNOWN, loc, msg) end
Write WARNING
message to logfile @param loc [string] message logging location, optional @param msg [string] message format string
# File lib/GunnyLog.rb, line 177 def log_warning(loc = nil, msg) log(WARNING, loc, msg) end
Write message to logfile @param loc [string] message logging location, optional @param msg [string] message string
# File lib/GunnyLog.rb, line 125 def message(loc = nil, msg) write_msg(@logging_file, loc, msg) end
Write exception to logfile @param exc [exception] exception to log
# File lib/GunnyLog.rb, line 156 def message_exception(loc = nil, exc) write_msg(@logging_file, loc, exc.message) end
Write formatted message with single arg or array of args @param loc [string] message logging location, optional @param msg [string] message format string @param args [arg or array of args]
# File lib/GunnyLog.rb, line 133 def message_formatted(loc = nil, msg, args) formatted = sprintf(msg, *args) message(loc, formatted) end
Write formatted message with variable number of args @param loc [string] message logging location @param msg [string] message format string @param args [variable number of args]
# File lib/GunnyLog.rb, line 142 def message_formatted_vars(loc, msg, *args) formatted = sprintf(msg, *args) message(loc, formatted) end
Write message to logfile @param msg [string] message string
# File lib/GunnyLog.rb, line 118 def msg(msg) message(nil, msg) end
Write exception to logfile @param exc [exception] exception to log
# File lib/GunnyLog.rb, line 149 def msg_exception(exc) #write_msg(@logging_file, @logging_location, exc.message) message_exception(nil, exc) end
Open the logfile @param filename [string] name of the logfile
# File lib/GunnyLog.rb, line 84 def open(filename = 'gunnylog.log') begin @logging_file = File.open(filename, 'a+') @file_open = true rescue SystemCallError => exc handle_exception(exc) end end
Open the logfile with path, name, and extension @param pathname [string] path of the logfile @param filename [string] name of the logfile @param extension [string] extension of the logfile
# File lib/GunnyLog.rb, line 97 def open_file(pathname = nil, filename = 'gunnylog', extension = 'log') if pathname == nil self.open(filename + '.' + extension) else self.open(pathname + '/' + filename + '.' + extension) end end
Set logging on and off @param flag [bool] switch for on or off
# File lib/GunnyLog.rb, line 50 def set_logging_enabled(flag) @logging_enabled = flag end
Set logging level @param level [int] logging level
# File lib/GunnyLog.rb, line 56 def set_logging_level(level) @logging_level = level end
Set message was logged from logging location @param loc [string] logging location name
# File lib/GunnyLog.rb, line 62 def set_logging_location(loc) @logging_location = loc end
Set output to STDERR
# File lib/GunnyLog.rb, line 75 def set_output_stderr if @file_open self.close end @logging_file = STDERR end
Set output to STDOUT, default
# File lib/GunnyLog.rb, line 67 def set_output_stdout if @file_open self.close end @logging_file = STDOUT end
Private Instance Methods
get the date time stamp
# File lib/GunnyLog.rb, line 248 def date_str d = DateTime.now d.strftime('%m/%d/%Y|%I:%M:%S%p') end
log exception and raise
# File lib/GunnyLog.rb, line 254 def handle_exception(exc) self.message_exception('***GunnyLog***', exc) raise GunnyLogException.new(exc.message) end
get string for level
# File lib/GunnyLog/severity.rb, line 25 def level_string(level) case level when 0 return 'DEBUG' when 1 return 'INFO' when 2 return 'WARNING' when 3 return 'ERROR' when 4 return 'FATAL' else return 'UNKNOWN' end end
used for debugging GunnyLog
# File lib/GunnyLog.rb, line 260 def local_debug(loc, msg = nil) if DEBUG_FLAG if msg == nil puts 'GunnyLog::' + loc else puts 'GunnyLog::' + loc + '|' + msg end end end
log with level
# File lib/GunnyLog.rb, line 236 def log(sev, loc, msg) if loc == nil loc = @logging_location else @logging_location = loc end if @logging_enabled and sev >= @logging_level @logging_file.puts "#{date_str}|#{$0}|#{level_string(sev)}|#{loc}|#{msg}" end end
write the msg
# File lib/GunnyLog.rb, line 224 def write_msg(output, loc, msg) if loc == nil loc = @logging_location else @logging_location = loc end if @logging_enabled output.puts "#{date_str}|#{$0}|#{loc}|#{msg}" end end