Module: OverSIP::Logger

Overview

Logging client module. Any class desiring to log messages must include (or extend) this module. In order to identify itself in the logs, the class can define log_id() method or set @log_id attribute.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fg_system_msg2str(level_str, msg, log_id) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/oversip/logger.rb', line 65

def self.fg_system_msg2str(level_str, msg, log_id)
  case msg
  when ::String
    "#{level_str.upcase}: <#{log_id}> " << msg
  when ::Exception
    "#{level_str.upcase}: <#{log_id}> #{msg.message} (#{msg.class })\n#{(msg.backtrace || [])[0..3].join("\n")}"
  else
    "#{level_str.upcase}: <#{log_id}> " << msg.inspect
  end
end

.load_methodsObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/oversip/logger.rb', line 8

def self.load_methods
  ::Syslog.close  if ::Syslog.opened?

  syslog_options = ::Syslog::LOG_PID | ::Syslog::LOG_NDELAY
  syslog_facility = ::OverSIP::Syslog::SYSLOG_FACILITY_MAPPING[::OverSIP.configuration[:core][:syslog_facility]] rescue ::Syslog::LOG_DAEMON
  ::Syslog.open(::OverSIP.master_name, syslog_options, syslog_facility)

  begin
    @@threshold = ::OverSIP::Syslog::SYSLOG_SEVERITY_MAPPING[::OverSIP.configuration[:core][:syslog_level]]
  rescue
    @@threshold = 0  # debug.
  end

  $oversip_debug = ( @@threshold == 0 ? true : false )

  ::OverSIP::Syslog::SYSLOG_SEVERITY_MAPPING.each do |level_str, level_value|
    method_str = "
      def log_system_#{level_str}(msg)
    "

    method_str << "
      return false if @@threshold > #{level_value}

      ::OverSIP::Syslog.log #{level_value}, msg, log_id, false
    "

    if not ::OverSIP.daemonized?
      if %w{debug info notice}.include? level_str
        method_str << "
          puts ::OverSIP::Logger.fg_system_msg2str('#{level_str}', msg, log_id)
          "
      else
        method_str << "
          $stderr.puts ::OverSIP::Logger.fg_system_msg2str('#{level_str}', msg, log_id)
        "
      end
    end

    method_str << "end"

    self.module_eval method_str


    # User logs.
    method_str = "
      def log_#{level_str}(msg)
        return false if @@threshold > #{level_value}

        ::OverSIP::Syslog.log #{level_value}, msg, log_id, true
      end
    "

    self.module_eval method_str

  end  # .each
end

Instance Method Details

#log_idObject

Default logging identifier is the class name. If log_id() method is redefined by the class including this module, or it sets @log_id, then such a value takes preference.



78
79
80
# File 'lib/oversip/logger.rb', line 78

def log_id
  @log_id ||= (self.is_a?(::Module) ? self.name.split("::").last : self.class.name)
end