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_ig() method or set @log_id attribute.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.closeObject



150
151
152
153
# File 'lib/oversip/logger.rb', line 150

def self.close
  @@logger_mq.close rescue nil
  @@logger_mq.unlink rescue nil
end

.fg_system_msg2str(level, msg, log_id) ⇒ Object



139
140
141
142
143
144
145
146
147
148
# File 'lib/oversip/logger.rb', line 139

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

.init_logger_mq(group = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/oversip/logger.rb', line 8

def self.init_logger_mq group=nil
  @@logger_mq = ::OverSIP::PosixMQ.create_queue({
    :name    => ::OverSIP.syslogger_mq_name,
    :mode    => :write,
    :maxmsg  => 1000,
    :msgsize => 2000,
    :group => group
  })
end

.load_methodsObject



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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/oversip/logger.rb', line 19

def self.load_methods
  # When not yet daemonized, also log to syslog.
  if not ::OverSIP.daemonized?
    ::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)
  end

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

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

  @@congested = false

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

    if ::OverSIP.syslogger_ready?
      method_str << "
        return false if @@threshold > #{level_value} || @@congested
        begin
          unless @@logger_mq.trysend ::OverSIP::Logger.syslog_system_msg2str(#{level_value}, msg, log_id), 0
            @@congested = true
            ::EM.add_timer(1) do
              @@logger_mq.trysend ::OverSIP::Logger.syslog_system_msg2str(4, \"logger message queue was full, some logs have been lost\", log_id), 1
              @@congested = false
            end
          end
        rescue ::Errno::EMSGSIZE
          @@logger_mq.trysend ::OverSIP::Logger.syslog_system_msg2str(4, \"too long message could not be logged\", log_id), 1 rescue nil
        rescue ::Exception => e
          @@logger_mq.trysend ::OverSIP::Logger.syslog_system_msg2str(4, \"unexpected logging error (\#{e.class}: \#{e.message})\", log_id), 1 rescue nil
        end
      "
    end

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

    method_str << "end"

    self.module_eval method_str


    if ::OverSIP.syslogger_ready?
      # User logs.
      method_str = "
        def log_#{level}(msg)
          return false if @@threshold > #{level_value} || @@congested
          begin
            unless @@logger_mq.trysend ::OverSIP::Logger.syslog_user_msg2str(#{level_value}, msg, log_id), 0
              @@congested = true
              ::EM.add_timer(1) do
                @@logger_mq.trysend ::OverSIP::Logger.syslog_user_msg2str(4, \"logger message queue was full, some logs have been lost\", log_id), 1
                @@congested = false
              end
            end
          rescue ::Errno::EMSGSIZE
            @@logger_mq.trysend ::OverSIP::Logger.syslog_user_msg2str(4, \"too long message could not be logged\", log_id), 1 rescue nil
          rescue ::Exception => e
            @@logger_mq.trysend ::OverSIP::Logger.syslog_user_msg2str(4, \"unexpected logging error (\#{e.class}: \#{e.message})\", log_id), 1 rescue nil
          end
        end
      "

      self.module_eval method_str
    end

  end  # .each
end

.syslog_system_msg2str(level_value, msg, log_id) ⇒ Object

Generate nice log messages. It accepst three parameters:

  • level_value: Integer representing the log level.

  • msg: the String or Exception to be logged.

  • log_id: a String helping to identify the generator of this log message.



117
118
119
120
121
122
123
124
125
126
# File 'lib/oversip/logger.rb', line 117

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

.syslog_user_msg2str(level_value, msg, log_id) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/oversip/logger.rb', line 128

def self.syslog_user_msg2str(level_value, msg, log_id)
  case msg
  when ::String
    level_value.to_s << "<" << log_id << "> [user] " << msg
  when ::Exception
    "#{level_value}<#{log_id}> [user] #{msg.message} (#{msg.class })\n#{(msg.backtrace || [])[0..3].join("\n")}"
  else
    level_value.to_s << "<" << log_id << "> [user] " << msg.inspect
  end
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.



157
158
159
# File 'lib/oversip/logger.rb', line 157

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