Log4j2 and JRuby classes

I am trying to use log4j2 on my Ruby classes but can’t figure out how to
get the logger instance for a ruby class. Here’s some sample code:

java_import org.apache.logging.log4j.LogManager
class TestClass
@log = LogManager.getLogger(TestClass)

 def logme
     @log.info('Test the logging')
 end

end

x = TestClass.new
x.logme

The problem is that @log is always nil. How do I get a logger for a
Ruby class?

Thanks,

– Greg


– Greg Allen
Calet, LLC
[email protected]

(replies inline)

On Sat, 15 Nov 2014, Greg Allen wrote:

end
There are two things that are happening here, because of there the
logManager.getLogger line is occurring, it’s creating the @log
instance
variable on the class object. Whereas the logme method is an
instance
method which is attempting to access the instance variable on the
initialized
TestClass object.

If you change this to:

class TestClass
def initialize
@log = LogManager.getLogger(TestClass)
end

def logme
  @log.info('Test the logging')
end

end

Then you will access the right @log in the right scope

  • R. Tyler C.