Class: Log4r::XMPPOutputter

Inherits:
Outputter
  • Object
show all
Defined in:
lib/log4r-xmpp.rb,
lib/log4r-xmpp/xmppoutputter.rb

Constant Summary collapse

VERSION =
'0.1.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_name, hash = {}) ⇒ XMPPOutputter

XMPPOutputter is initialized the same as other Log4r Outputters with the addition of the following hash options:

:buffsize   => The number of logging events to buffer before sending (default 1)
:username   => Sending user's XMPP/Jabber account username
:password   => Sending user's XMPP/Jabber account password
:resource   => Optional sending user's XMPP/Jabber resource (default 'Log4r')
:recipients => Array of usernames to send Log4r log statements to

Example:

options => { :buffsize   => 10,
             :username   => '[email protected]',
             :password   => 'secret',
             :resource   => 'Log4r',
             :recipients => ['[email protected]', '[email protected]']
           }

outputter = Log4r::XMPPOutputter.new('xmpp', options)

mylog = Log4r::Logger.new 'mylog'
mylog.outputters = outputter

mylog.debug "This is a test message sent at level DEBUG"


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/log4r-xmpp/xmppoutputter.rb', line 53

def initialize(_name, hash={})

  super(_name, hash)

  @buff = []

  begin

    connect
    Logger.log_internal { "XMPPOutputter '#{@name}' connected as #{@username}"}

  rescue => e

    xmpp_error = "#{e.class}: #{e.message}"

    Logger.log_internal(-2) do

      "XMPPOutputter '#{@name}' failed to connect to XMPP server: #{xmpp_error}"

    end # Logger.log_internal

  end # begin
  
end

Instance Attribute Details

#passwordObject (readonly)

Returns the value of attribute password.



26
27
28
# File 'lib/log4r-xmpp/xmppoutputter.rb', line 26

def password
  @password
end

#recipientsObject (readonly)

Returns the value of attribute recipients.



26
27
28
# File 'lib/log4r-xmpp/xmppoutputter.rb', line 26

def recipients
  @recipients
end

#resourceObject (readonly)

Returns the value of attribute resource.



26
27
28
# File 'lib/log4r-xmpp/xmppoutputter.rb', line 26

def resource
  @resource
end

#usernameObject (readonly)

Returns the value of attribute username.



26
27
28
# File 'lib/log4r-xmpp/xmppoutputter.rb', line 26

def username
  @username
end

Instance Method Details

#connectObject

Connects to the Jabber/XMPP server as a client. This is done automatically during initialization.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/log4r-xmpp/xmppoutputter.rb', line 81

def connect

  Jabber::logger = Log4r::Logger['xmpp4r'].nil? ? Log4r::Logger.new('xmpp4r')\
                                                : Log4r::Logger['xmpp4r']

  # Enable XMPP4r library to log DEBUG events to the internal logger
  #
  Jabber::warnings = true

  jid = Jabber::JID::new("#{@username}/#{@resource}")

  @client = Jabber::Client::new(jid)
  @client.connect
  @client.auth(@password)

end

#flushObject

Call to force an outputter to write any buffered log events.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/log4r-xmpp/xmppoutputter.rb', line 100

def flush

  synch do

    messages = []

    @buff.each{ |event| messages.push( format(event) ) }

    write(messages)

  end # synch

  @buff.clear

  Logger.log_internal { "Flushed XMPPOutputter '#{@name}'" }

end