Class: LogStash::Inputs::Log4j

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/inputs/log4j.rb

Overview

Read events over a TCP socket from a Log4j SocketAppender.

Can either accept connections from clients or connect to a server, depending on ‘mode`. Depending on which `mode` is configured, you need a matching SocketAppender or a SocketHubAppender on the remote side.

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Log4j

Returns a new instance of Log4j.



39
40
41
# File 'lib/logstash/inputs/log4j.rb', line 39

def initialize(*args)
  super(*args)
end

Instance Method Details

#registerObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/logstash/inputs/log4j.rb', line 44

def register
#    LogStash::Environment.load_elasticsearch_jars!
  require "java"
  require "jruby/serialization"

  begin
    Java::OrgApacheLog4jSpi.const_get("LoggingEvent")
  rescue
    raise(LogStash::PluginLoadingError, "Log4j java library not loaded")
  end

  if server?
    @logger.info("Starting Log4j input listener", :address => "#{@host}:#{@port}")
    @server_socket = TCPServer.new(@host, @port)
  end
  @logger.info("Log4j input")
end

#run(output_queue) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/logstash/inputs/log4j.rb', line 118

def run(output_queue)
  if server?
    loop do
      # Start a new thread for each connection.
      Thread.start(@server_socket.accept) do |s|
        # TODO(sissel): put this block in its own method.

        # monkeypatch a 'peer' method onto the socket.
        s.instance_eval { class << self; include ::LogStash::Util::SocketPeer end }
        @logger.debug("Accepted connection", :client => s.peer,
                      :server => "#{@host}:#{@port}")
        handle_socket(s, output_queue)
      end # Thread.start
    end # loop
  else
    loop do
      client_socket = TCPSocket.new(@host, @port)
      client_socket.instance_eval { class << self; include ::LogStash::Util::SocketPeer end }
      @logger.debug("Opened connection", :client => "#{client_socket.peer}")
      handle_socket(client_socket, output_queue)
    end # loop
  end
end