3

I have a JRuby project that requires a Java jar. The Java jar is instrumented for logging using log4j. I don't need to use log4j in my JRuby code, but I want to run the JRuby code so that when the instrumented Java classes are called they respect the log4j settings I've specified.

I'm using Trinidad and had hoped that just passing in the 'normal' log4j JVM arguments would do the trick. Here's the process information when I just run Trinidad:

/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/bin/java -Xmx500m -Xss2048k  -Djffi.boot.library.path=/Users/eebbesen/.rvm/rubies/jruby-1.7.5/lib/jni -Dfile.encoding=UTF-8 -Djava.net.preferIPv4Stack=true -Xbootclasspath/a:/Users/eebbesen/.rvm/rubies/jruby-1.7.5/lib/jruby.jar -classpath : -Djruby.home=/Users/eebbesen/.rvm/rubies/jruby-1.7.5 -Djruby.lib=/Users/eebbesen/.rvm/rubies/jruby-1.7.5/lib -Djruby.script=jruby -Djruby.shell=/bin/sh org.jruby.Main script/trinidad

and here's what I tried to get log4j working:

/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/bin/java -Xmx500m -Xss2048k -Djffi.boot.library.path=/Users/eebbesen/.rvm/rubies/jruby-1.7.5/lib/jni -Dfile.encoding=UTF-8 -Djava.net.preferIPv4Stack=true -Xbootclasspath/a:/Users/eebbesen/.rvm/rubies/jruby-1.7.5/lib/jruby.jar -classpath : -Djruby.home=/Users/eebbesen/.rvm/rubies/jruby-1.7.5 -Djruby.lib=/Users/eebbesen/.rvm/rubies/jruby-1.7.5/lib -Djruby.script=jruby -Dlog4j.debug=true -Dlog4j.configuration=file:log4j.properties -Djruby.shell=/bin/sh org.jruby.Main script/trinidad

so I'm just adding

-Dlog4j.debug=true -Dlog4j.configuration=file:log4j.properties

to what Trinidad is already doing.

What I'm not seeing is any of the log4j logging happening when I run Trinidad. However, when I use Rails console my logging statements are appearing.

The posts I've seen cover how to use log4j from within JRuby, but I would like to accomplish this via JVM arguments if possible.

4

1 回答 1

2

我通过设置 JRuby 风格的 JVM 选项来实现这一点:

export JRUBY_OPTS="-J-Dlog4j.debug=true -J-Dlog4j.configuration=file:/absolute/path/to/log4j.properties"
trinidad

这覆盖了我的 Java jar 中指定的 log4j.properties,我能够在 jar 文件中(并由类显式调用)操作 Java 类的日志级别。

于 2015-06-03T19:23:12.513 回答