class Jellog::Logger

The Jellog logger

Create instances of Jellog::Logger and use then to produce colourful and useful logs. See {file:README.md The Readme File} for more details.

Constants

LogOpts

@deprecated - no longer required

Attributes

log_level[R]

show current log level

logfilename[R]

get the name of the logfile as it was eventually set

Public Class Methods

disable_syslog() click to toggle source

disable logging to syslog. Only works if called before creating a logger @deprecated Use :disable_syslog parameter instead

# File lib/jellog.rb, line 59
def self.disable_syslog
  @@syslog = false
end
get_options(opts, delete=false) click to toggle source

extract options from a hash that relate to logging, deleting from the given hash and returning a hash containing just the logging options

@deprecated use jeckyl's intersection to get logopts and complement to remove them

# File lib/jellog.rb, line 68
def self.get_options(opts, delete=false)
  log_opts = Hash.new
  LogOpts.each_key do |key|
    if opts.has_key?(key) then
      log_opts[key] = opts[key]
      opts.delete(key) if delete
    end
  end
  return log_opts
end
new(appname, log_opts={}) click to toggle source

create a new logger that logs to a file in the given log directory

@param [String] appname name to describe application. This will be used to

name the log, and must therefore be compatible with filenaming conventions

@param [Hash] log_options various parameters as defined by the {Jellog::Config}

class.
# File lib/jellog.rb, line 86
def initialize(appname, log_opts={})
  @appname = appname
  #@log_opts = LogOpts.merge(log_opts)
  
  logdir = log_opts.delete(:log_dir)
  @log_level = log_opts.delete(:log_level)
  @mark = log_opts[:log_mark] || ' *** MARK ***'
  
  @logfilename = File.join(logdir, "#{@appname.downcase}.log")
  # make sure we can log somewhere!
  if  !(FileTest.directory?(logdir) && FileTest.writable?(logdir)) then
    @logfilename = File.join("/tmp", "#{@appname.downcase}.log")
  elsif (FileTest.exists?(@logfilename) && ! FileTest.writable?(@logfilename)) then
    stamp = Time.now.strftime("%Y%m%d%H%M%S")
    @logfilename = File.join(logdir, "#{@appname.downcase}_#{stamp}.log")
  end
  @logger = Jellog::Slogger.new(@logfilename, 
                              log_opts[:log_reset], 
                              log_opts[:log_rotation], 
                              log_opts[:log_length],
                              log_opts[:log_coloured],
                              log_opts[:log_date_time_format],
                              log_opts[:log_sync])
  if log_opts[:disable_syslog] then
    @syslog = false
  else
    @syslog = @@syslog
  end
end

Public Instance Methods

close() click to toggle source

close and flush the log

# File lib/jellog.rb, line 262
def close
  @logger.close
end
colours=(new_colours) click to toggle source

set the colours to use

@param [Hash<Symbol, Symbol>] new_colours hash to map logging methods

to colours

For example:

@logger.colours = {:system => :blue}

Note that :fatal and :mark messages are always displayed in bold as well.

# File lib/jellog.rb, line 128
def colours=(new_colours)
  @logger.colours = new_colours
end
debug(message) click to toggle source

logs to log file if log_level is :debug use it to log anything and everything, but probably not intended to survive into production unless the application is unstable

@param [String] message the message to log

# File lib/jellog.rb, line 195
def debug(message)
  return true unless @log_level == :debug
  @logger.debug(message)    
end
error(message) click to toggle source

output a standard error message to the log

@param [String] message the message to log @return [String] the message logged

# File lib/jellog.rb, line 213
def error(message)
  @logger.error(message)
  return message
end
exception(err) click to toggle source

output details of an exception to the log, i.e. the backtrace

@param [Exception] err to log and backtrace

# File lib/jellog.rb, line 221
def exception(err)
  @logger.error("#{err.class.to_s}: #{err.message}")
  err.backtrace.each do |trace|
    @logger.error(trace)
  end
end
fatal(message) click to toggle source

output a fatal error message this also logs to syslog cos its likely to be important unless the syslog has been disabled (see above)

Note that the syslog message given is prefixed with “Jellog Alert:” This is to make it easier to filter these messages, e.g. to create an email

@param [String] message the message to log @return [String] the message logged

# File lib/jellog.rb, line 237
def fatal(message)
  if @syslog then
    alert = "Jellog Alert: " + message
    Syslog.open(@appname)
    Syslog.info(alert)
    Syslog.close
  end
  @logger.fatal(message)
  return message
end
info(message) click to toggle source

just logs to log file - should be used for routine information that is always logged but does not need to clog up the system log

@param [String] message the message to log

# File lib/jellog.rb, line 177
def info(message)
  @logger.info(message)
end
log_level=(level) click to toggle source

set the level at which logging will work should be one of:

@param [Symbol] level to set logging to

Symbols can be one of:

  • :system - only system calls (or warn etc) will be logged

  • :verbose - add verbose messages

  • :debug - add debug messages as well

If anything else is passed in then the default :system is used

# File lib/jellog.rb, line 144
def log_level=(level)
  case level
  when :system, :verbose, :debug
    @log_level = level
  else
    @log_level = :system
  end
end
mark() click to toggle source

mark the log with a clear divider

# File lib/jellog.rb, line 257
def mark
  @logger.mark(@mark)
end
puts(message) click to toggle source

puts allows jellog to be used instead of a normal file for situations where output could be to stdout or a log

@param [String] message the message to log

# File lib/jellog.rb, line 252
def puts(message)
  self.verbose(message)
end
system(message) click to toggle source

create a system message

logs to syslog unless Jellog.disable_syslog was called before the logger was created logs to logfile

should be used for important info messages, such as logging successful startup etc

@param [String] message the message to log

# File lib/jellog.rb, line 164
def system(message)
  if @syslog then
    Syslog.open(@appname)
    Syslog.info(message)
    Syslog.close
  end
  @logger.system(message)
end
verbose(message) click to toggle source

logs to log file if log_level is :verbose or :debug use it to output more detail about the program without going crazy

@param [String] message the message to log

# File lib/jellog.rb, line 185
def verbose(message)
  return true unless @log_level == :verbose || @log_level == :debug
  @logger.info(message)
end
warn(message) click to toggle source

output a standard warning message to the log

@param [String] message the message to log @return [String] the message logged

# File lib/jellog.rb, line 204
def warn(message)
  @logger.warn(message)
  return message
end