JRuby - Using SLF4J

How to get the equivalent of the following Java code (initialization of the SLF4J logger) running in JRuby?

private final static Logger logger = LoggerFactory.getLogger(Manager.class); 

Say my (working) example script looks like this:

 def test(some_input) logger = org.slf4j.LoggerFactory.getLogger("SCRIPT"); logger.error("Error...") end 

Since I'm new to JRuby, I got it inside the method, not with .class in getLogger .

So the questions are:

  • How do I call getLogger using .class as an argument in JRuby code?
  • How to place a Logger object somewhere as static? I would like to avoid getLogger every time I call a method.

Thank you for your help!

+4
source share
1 answer

you need to use java_class to get the โ€œcorrectโ€ class, which is the (wrapped) java.lang.Class , and not the ruby โ€‹โ€‹class, as it is with java.util.Date.class , for example.

 Java::OrgSlf4j::LoggerFactory.getLogger java.util.Date.java_class 

since you can use class variables to save โ€œstaticallyโ€, but keep in mind that they are shared between the inheritance hierarchy, for example:

 @@logger = Java::OrgSlf4j::LoggerFactory.getLogger java.util.Date.java_class 
+2
source

All Articles