Class: CanvasOss::Event::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/canvas_oss/event.rb

Overview

Class that log message using syslog

Examples:

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

Constant Summary collapse

FACILITIES =

Table of Syslog facilities constants

[Syslog::LOG_LOCAL0, Syslog::LOG_LOCAL1, Syslog::LOG_LOCAL2,
Syslog::LOG_LOCAL3, Syslog::LOG_LOCAL4, Syslog::LOG_LOCAL5,
Syslog::LOG_LOCAL6, Syslog::LOG_LOCAL7].freeze

Instance Method Summary collapse

Constructor Details

#initialize(program_name, facility) ⇒ Logger

Initialize Syslog::Logger class with a private call to #connect.

Examples:

example_log = CanvasOss::Event::Logger.new('canvas_web', 3) # 3 for Syslog::LOG_LOCAL3

Parameters:

  • program_name (String)

    This parameter is the name of the programname in syslog

  • facility (Integer)

    Number of the Syslog facility local, where the integer is the postion of the facility in FACILITIES array


37
38
39
# File 'lib/canvas_oss/event.rb', line 37

def initialize(program_name, facility)
  connect(program_name, facility)
end

Instance Method Details

#closeObject

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


71
72
73
74
75
76
77
78
# 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) ⇒ Object

Send logs thrue Syslog connection

Examples:

example_log.log('warn', 'This is a warning log text')
example_log.log('info', 'This is a info log text')

Parameters:

  • severity (String)

    Severity of the log (unknown, error, warn, info, debug)

  • message (String)

    Content of the log

Raises:

  • (ArgumentError)

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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