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

DEBUG

Log level DEBUG

DEBUG_FLAG

local debug messages

ERROR

Log level ERROR

FATAL

Log level FATAL

INFO

Log level INFO

UNKNOWN

Log level UNKNOWN

WARNING

Log level WARNING

Attributes

file_open[RW]

Is file open flag

logging_enabled[R]

@return [bool] is logging enabled

logging_file[RW]

File used for the log file

logging_level[R]

@return [int] logging level

logging_location[R]

@return [string] message location

Public Class Methods

instance() click to toggle source

Instance method for GunnyLog @return [GunnyLog] instance

# File lib/GunnyLog.rb, line 204
def self.instance
  @@instance ||= new
end
new() click to toggle source

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() click to toggle source

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
log_debug(loc = nil, msg) click to toggle source

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
log_error(loc = nil, msg) click to toggle source

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
log_fatal(loc = nil, msg) click to toggle source

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
log_info(loc = nil, msg) click to toggle source

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
log_unknown(loc = nil, msg) click to toggle source

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
log_warning(loc = nil, msg) click to toggle source

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
message(loc = nil, msg) click to toggle source

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
message_exception(loc = nil, exc) click to toggle source

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
message_formatted(loc = nil, msg, args) click to toggle source

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
message_formatted_vars(loc, msg, *args) click to toggle source

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
msg(msg) click to toggle source

Write message to logfile @param msg [string] message string

# File lib/GunnyLog.rb, line 118
def msg(msg)
  message(nil, msg)
end
msg_exception(exc) click to toggle source

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(filename = 'gunnylog.log') click to toggle source

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_file(pathname = nil, filename = 'gunnylog', extension = 'log') click to toggle source

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_enabled(flag) click to toggle source

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(level) click to toggle source

Set logging level @param level [int] logging level

# File lib/GunnyLog.rb, line 56
def set_logging_level(level)
  @logging_level = level
end
set_logging_location(loc) click to toggle source

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_stderr() click to toggle source

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_stdout() click to toggle source

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

date_str() click to toggle source

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
handle_exception(exc) click to toggle source

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
level_string(level) click to toggle source

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
local_debug(loc, msg = nil) click to toggle source

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(sev, loc, msg) click to toggle source

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_msg(output, loc, msg) click to toggle source

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