为什么没有Slf4j / Logback记录文件名和行号(Why doesn't Slf4j/Logback log filename and row number)

编程入门 行业动态 更新时间:2024-10-28 02:29:38
为什么没有Slf4j / Logback记录文件名和行号(Why doesn't Slf4j/Logback log filename and row number)

我的日志记录存在问题,其中slf4j没有记录消息/ stacktrace的文件名和行号。

码:

private static Logger log = LoggerFactory.getLogger(SomeService.class); @Override public void aService(ServiceAdmin sa) throws Exception { log.debug(LoggerFactory.class.toString()); log.debug(log.getClass().getName()); log.debug("Setup Example"); SomeService.setDefault(Example.getInstance()); log.debug("Example finished"); }

日志:

2015-04-20 14:47:26.573 DEBUG [main] null:-1 - class org.slf4j.LoggerFactory 2015-04-20 14:47:26.574 DEBUG [main] null:-1 - ch.qos.logback.classic.Logger 2015-04-20 14:47:26.574 DEBUG [main] null:-1 - Setup Example 2015-04-20 14:47:26.575 DEBUG [main] null:-1 - SomeService finished

这是我的logback.xml与该类相关的部分

<logger name="com.bredband.nexusgw.services"> <level value="debug" /> <appender-ref ref="nexusservice" /> </logger> <appender name="nexusservice" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>/var/log/nexus/nexusjgw/jgw/Service.log</File> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <FileNamePattern>/var/log/nexus/nexusjgw/jgw/Service.log.%i </FileNamePattern> <MinIndex>1</MinIndex> <MaxIndex>5</MaxIndex> </rollingPolicy> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <MaxFileSize>200MB</MaxFileSize> </triggeringPolicy> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>debug</level> </filter> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [%t] %F:%L - %m%n</Pattern> </layout> </appender>

有没有人见过这个,可以指出我正确的方向?

BR Stefan

- 更新 -

我将%F更改为%C,但这并没有改变日志中的任何内容。 然后我尝试在我的ant文件的javac头中设置debug =“on”,现在它记录了classname和行号。

2015-04-21 08:28:55.910 DEBUG [main] Service.java:20 - class org.slf4j.LoggerFactory 2015-04-21 08:28:55.911 DEBUG [main] Service.java:21 - ch.qos.logback.classic.Logger 2015-04-21 08:28:55.911 DEBUG [main] Service.java:22 - Setup Example 2015-04-21 08:28:55.912 DEBUG [main] Service.java:24 - Example finished

虽然这是有效的,但这不是我以前改变过的。 随着时间的推移,这个项目唯一改变的是增加了其他外部罐子。 我不确定如何在另一个外部jar中添加重复/更新/更旧版本的slf4j会影响日志记录,但是任何版本都应该查找logback文件?

- 更新 - 在检查了很多build.xml文件的历史签入后,我发现我们之前实际上有debug =“on”,实际上这个参数可能已经被引入了“off”。

感谢您的帮助。

BR Stefan

I'm having problems with my logging where slf4j doesn't log the filename and row number of the message/stacktrace.

Code:

private static Logger log = LoggerFactory.getLogger(SomeService.class); @Override public void aService(ServiceAdmin sa) throws Exception { log.debug(LoggerFactory.class.toString()); log.debug(log.getClass().getName()); log.debug("Setup Example"); SomeService.setDefault(Example.getInstance()); log.debug("Example finished"); }

Log:

2015-04-20 14:47:26.573 DEBUG [main] null:-1 - class org.slf4j.LoggerFactory 2015-04-20 14:47:26.574 DEBUG [main] null:-1 - ch.qos.logback.classic.Logger 2015-04-20 14:47:26.574 DEBUG [main] null:-1 - Setup Example 2015-04-20 14:47:26.575 DEBUG [main] null:-1 - SomeService finished

This is pars of my logback.xml that relates to this class

<logger name="com.bredband.nexusgw.services"> <level value="debug" /> <appender-ref ref="nexusservice" /> </logger> <appender name="nexusservice" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>/var/log/nexus/nexusjgw/jgw/Service.log</File> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <FileNamePattern>/var/log/nexus/nexusjgw/jgw/Service.log.%i </FileNamePattern> <MinIndex>1</MinIndex> <MaxIndex>5</MaxIndex> </rollingPolicy> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <MaxFileSize>200MB</MaxFileSize> </triggeringPolicy> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>debug</level> </filter> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [%t] %F:%L - %m%n</Pattern> </layout> </appender>

Has anyone seen this before and can point me in the right direction?

BR Stefan

-- Update --

I changed the %F to %C, but that didn't change anything in the logs. Then I tried to set debug = "on" in the javac header in my ant file and now it's logging classname and line number.

2015-04-21 08:28:55.910 DEBUG [main] Service.java:20 - class org.slf4j.LoggerFactory 2015-04-21 08:28:55.911 DEBUG [main] Service.java:21 - ch.qos.logback.classic.Logger 2015-04-21 08:28:55.911 DEBUG [main] Service.java:22 - Setup Example 2015-04-21 08:28:55.912 DEBUG [main] Service.java:24 - Example finished

Although this is working, it's not something I've changed before. The only thing that has changed in this project over time is the addition of other external jars. I'm not really sure how adding a duplicate/newer/older version of slf4j in another external jar would affect the logging though, any version should look for the logback file?

-- Update -- After checking alot of historical checkins of the build.xml file I found that we actually had debug = "on" earlier and the problem may actually have been introduced by this parameter beeing set to "off".

Thank you for your help.

BR Stefan

最满意答案

您应首先确保使用调试信息编译应用程序。 如果直接调用javac ,那么它是-g命令行参数。 但是,大多数构建工具(如maven或ant)都有一种特定的方法来提供此参数。

您可能还希望将%F替换为%C ,这应该在没有调试信息的情况下工作。

You should first make sure that you compile your application with debug information. If calling javac directly, then it is the -g command-line parameter. However most build tools (like maven or ant) have a specific way to provide this parameter.

You may also want to replace %F with %C, which should work without the debug information.

更多推荐

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

发布评论

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

>www.elefans.com

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