从gradle启用log4j的调试符号(Enable debug symbols for log4j from gradle)

编程入门 行业动态 更新时间:2024-10-26 20:23:38
从gradle启用log4j的调试符号(Enable debug symbols for log4j from gradle)

我的log4j语句正在显示? s代表行号。

我试过的

网络上的其他答案说,如果您没有将调试信息编译到类中,就会发生这种情况。 例如,在Ant中,这将通过<javac debug="true" ... 。 只在gradle中搜索一种方法

http://gradle.org/docs/2.3-rc-2/dsl/org.gradle.api.tasks.compile.CompileOptions.html

debug - 判断是否在生成的类文件中包含调试信息。 默认为true

我通过在我的build.gradle中放入println compileGroovy.options.debug来验证这一点,并打印出true 。 我还找到了http://forums.gradle.org/gradle/topics/compile-groovy-with-debugging-information ,它建议使用tasks.withType(GroovyCompile) { options.debug = true } ,这也不起作用。 最后,我找到了compileGroovy.options.debugOptions.debugLevel ,它可以包含source , lines或vars 。 默认情况下为null,但将其设置为source,lines没有给出我的行信息。

再现

gradle init --type groovy-library

编辑src / main / groovy / Library.groovy以获得额外的行:

import groovy.util.logging.Log4j @Log4j class Library { boolean someLibraryMethod() { log.info "XXXXXXXXXXXXX" true } }

将compile 'log4j:log4j:1.2.17'到build.gradle

添加文件src / main / resources / log4j.xml

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %c:%L - %m%n" /> </layout> </appender> <root> <level value="INFO" /> <appender-ref ref="console" /> </root> </log4j:configuration>

最后运行gradle clean test -i并观察你看到的2015-03-30 08:47:31,996 Library:? - XXXXXXXXXXXXX 2015-03-30 08:47:31,996 Library:? - XXXXXXXXXXXXX请注意? 在消息中。

版本信息

$ gradle -version

------------------------------------------------------------
Gradle 2.3
------------------------------------------------------------

Build time:   2015-02-16 05:09:33 UTC
Build number: none
Revision:     586be72bf6e3df1ee7676d1f2a3afd9157341274

Groovy:       2.3.9
Ant:          Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM:          1.8.0_40 (Oracle Corporation 25.40-b25)
OS:           Windows 7 6.1 amd64

$ groovy -version
Groovy Version: 2.4.3 JVM: 1.8.0_40 Vendor: Oracle Corporation OS: Windows 7

$ java -version
java version "1.8.0_40"
Java(TM) SE Runtime Environment (build 1.8.0_40-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.40-b25, mixed mode)
 

我的问题当然是,如何显示行号而不是问号?

My log4j statements are showing ?s for line numbers.

What I've tried

Other answers around the web say this happens when you haven't compiled debug information into your classes. In Ant for example, this would be done with <javac debug="true" .... Searching for a way to do this in gradle yields only

http://gradle.org/docs/2.3-rc-2/dsl/org.gradle.api.tasks.compile.CompileOptions.html

debug - Tells whether to include debugging information in the generated class files. Defaults to true

I verified this by putting println compileGroovy.options.debug in my build.gradle and it prints true. I also found http://forums.gradle.org/gradle/topics/compile-groovy-with-debugging-information, which recommended using tasks.withType(GroovyCompile) { options.debug = true }, which also did not work. Finally, I found compileGroovy.options.debugOptions.debugLevel, which can have source, lines, or vars. This is null by default, but setting it to source,lines did not give my line information.

Reproducing

gradle init --type groovy-library

Edit src/main/groovy/Library.groovy to have the additional lines:

import groovy.util.logging.Log4j @Log4j class Library { boolean someLibraryMethod() { log.info "XXXXXXXXXXXXX" true } }

Add compile 'log4j:log4j:1.2.17' to build.gradle

Add the file src/main/resources/log4j.xml

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %c:%L - %m%n" /> </layout> </appender> <root> <level value="INFO" /> <appender-ref ref="console" /> </root> </log4j:configuration>

And finally run gradle clean test -i and observe that you see 2015-03-30 08:47:31,996 Library:? - XXXXXXXXXXXXX Note the ? in the message.

Version information

$ gradle -version

------------------------------------------------------------
Gradle 2.3
------------------------------------------------------------

Build time:   2015-02-16 05:09:33 UTC
Build number: none
Revision:     586be72bf6e3df1ee7676d1f2a3afd9157341274

Groovy:       2.3.9
Ant:          Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM:          1.8.0_40 (Oracle Corporation 25.40-b25)
OS:           Windows 7 6.1 amd64

$ groovy -version
Groovy Version: 2.4.3 JVM: 1.8.0_40 Vendor: Oracle Corporation OS: Windows 7

$ java -version
java version "1.8.0_40"
Java(TM) SE Runtime Environment (build 1.8.0_40-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.40-b25, mixed mode)
 

My question is, of course, how do I get line numbers to show instead of question marks?

最满意答案

Groovy包含类文件中的行号。 问题是groovy调用的方法与Java略有不同,这会混淆决定行号的Log4j代码。

特别是,如果你改变了这条线

log.info "XXXXXXXXXXXXX"

log.error "XXXXXXXXXXXXX", new Exception('test')

你可以看到当groovy调用方法时会调用一些反射方法。 我生成的堆栈跟踪中最顶层的方法是sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ,它是一个本机方法,没有行号。 这就是Log4j所关注的。 不幸的是,没有简单的方法来过滤Log4j使用的堆栈跟踪或告诉Log4j在堆栈中向下看。

一种可能的解决方案是打开静态编译。 如果将@groovy.transform.CompileStatic添加到Library类或方法中,您将获得正确的行号。

Groovy includes the line numbers in the class files. The problem is that groovy calls methods slightly differently than Java which confuses the Log4j code that determines the line number.

In particular, if you change the line

log.info "XXXXXXXXXXXXX"

to

log.error "XXXXXXXXXXXXX", new Exception('test')

you can see some reflection methods are called when groovy invokes methods. The topmost method in the stack trace I generated was sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method), which, being a native method, doesn't have a line number. This is what Log4j is looking at. Unfortunately, there's no easy was to filter the stacktrace Log4j uses or tell Log4j to look further down the stack.

One possible solution is to turn on static compilation. If you add @groovy.transform.CompileStatic to the Library class or method you'll get the correct line number.

更多推荐

本文发布于:2023-08-07 07:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1461594.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:符号   log4j   gradle   symbols   debug

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!