class CanvasOss::Event::Logger

Class that log message using syslog @example Create connection, send warning log, and close connection

example_log = CanvasOss::Event::Logger.new('canvas_web', 1)
example_log.log('warn', 'This is a warning log text')
example_log.close

@author Dylan TROLES

Constants

FACILITIES

Table of Syslog facilities constants

Public Class Methods

new(program_name, facility) click to toggle source

Initialize Syslog::Logger class with a private call to {#connect}. @param program_name [String] This parameter is the name of the programname in syslog @param facility [Integer] Number of the Syslog facility local, where the integer is the postion of the facility in {CanvasOss::Event::Logger::FACILITIES} array @example

example_log = CanvasOss::Event::Logger.new('canvas_web', 3) # 3 for Syslog::LOG_LOCAL3
# File lib/canvas_oss/event.rb, line 37
def initialize(program_name, facility)
  connect(program_name, facility)
end

Public Instance Methods

close() click to toggle source

Close connection if there is one opened. If not, raise error.

# File lib/canvas_oss/event.rb, line 71
def close
  begin
    Syslog::Logger.syslog.close
  rescue RuntimeError => exception
    raise "There is no Syslog connection to close : #{exception}"
  end
  return true
end
log(severity, message) click to toggle source

Send logs thrue Syslog connection @param severity [String] Severity of the log (unknown, error, warn, info, debug) @param message [String] Content of the log @example

example_log.log('warn', 'This is a warning log text')
example_log.log('info', 'This is a info log text')
# File lib/canvas_oss/event.rb, line 47
def log(severity, message)
  raise ArgumentError, 'Message must be a String' unless message.is_a?(String)
  begin
    case severity
    when 'alert'
      @log.unknown '[Alert] : ' + message
    when 'error'
      @log.error '[Error] : ' + message
    when 'warn'
      @log.warn '[Warning] : ' + message
    when 'info'
      @log.info '[Info] : ' + message
    when 'debug'
      @log.debug '[Debug] : ' + message
    else
      raise "You've provided a false severity name. Choose between : alert,
            error, warn, info, debug."
    end
  rescue RuntimeError => exception
    raise "#{exception}. Syslog opened status : #{Syslog::Logger.syslog.opened?} "
  end
end

Private Instance Methods

connect(program_name, facility) click to toggle source

Initialize Syslog::Logger class. Called by {#initialize}. @param program_name [String] This parameter is the name of the programname in syslog @param facility [Integer] Number of the Syslog facility local, where the integer is the postion of the facility in {CanvasOss::Event::Logger::FACILITIES} array @example

example_log = CanvasOss::Event::Logger.new('canvas_web', 3) # 3 for Syslog::LOG_LOCAL3
# File lib/canvas_oss/event.rb, line 87
def connect(program_name, facility)
  raise ArgumentError, "You need to provide a number between 0 and #{FACILITIES.length - 1}" unless facility.is_a?(Integer) && facility.between?(0, FACILITIES.length - 1)
  raise ArgumentError, 'Program name must be a String' unless program_name.is_a?(String)
  begin
    @log = Syslog::Logger.new(program_name, FACILITIES[facility])
  rescue StandardError => exception
    raise "Syslog connection has failed : #{exception}"
  end
end