JRuby - 使用 SLF4J
如何在 JRuby 中获得与以下 Java 代码(初始化 SLF4J 记录器)等效的内容?
private final static Logger logger = LoggerFactory.getLogger(Manager.class);
假设我的(工作)示例脚本如下所示:
def test(some_input)
logger = org.slf4j.LoggerFactory.getLogger("SCRIPT");
logger.error("Error...")
end
因为我是 JRuby 新手,所以我只能让它在方法内部工作,而不是在 getLogger
中使用 .class
代码>.
所以问题是:
- 如何在 JRuby 代码中使用
.class
作为参数来调用getLogger
? - 如何将 Logger 对象作为 static 放置在某处?我希望每次调用该方法时都避免使用
getLogger
。
感谢您的帮助!
How do I get the equivalent of the following Java code (initializing a SLF4J logger) working in JRuby?
private final static Logger logger = LoggerFactory.getLogger(Manager.class);
Let's say that my (working) example script looks like this:
def test(some_input)
logger = org.slf4j.LoggerFactory.getLogger("SCRIPT");
logger.error("Error...")
end
Because I'm a JRuby newbie I only got it working inside the method, and not with a .class
in the getLogger
.
So the questions are:
- How do I call
getLogger
using a.class
as an argument in the JRuby code? - How do I put the Logger object somewhere as a static ? I would like to avoid the
getLogger
every time I call the method.
Thank you for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
java_class
来获取“正确”的类,该类是(包装的)java.lang.Class
,而不是像那样的 ruby 类java.util.Date.class
例如:至于“静态”存储它,您可以使用类变量,但请记住它们在继承层次结构中共享,例如:
you'll need to use
java_class
to get the "correct" class that is a (wrapped)java.lang.Class
and not a ruby class as is withjava.util.Date.class
e.g. :as for storing it "statically" you could use class variables, but keep in mind that they are shared across the inheritance hierarchy, example :