Spring - 异常不记录到文件
Spring - Exceptions do not log to a file
我目前正在使用 SLF4J API 进行日志记录。
每当在运行时抛出异常时,完整的错误堆栈跟踪不会记录到文件中,它只会打印到控制台。我正在使用日食。
这是我的 logback.xml 代码(目前位于 WEB-INF 下的 类 文件夹中)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>
<!-- Specify here the path of the folder you want to save your logs -->
<property name="LOGFILE_PATH" value="C:/Logs" />
<!-- All logging will be redirected/ printed to console. -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{yyyy-MM-dd hh:mm:ss a} [%thread] %-5level %logger{50} - %rEx %msg%n </Pattern>
</layout>
</appender>
<!-- Send log to file -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOGFILE_PATH}/spring-mybatis-log.log</File>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{yyyy-MM-dd hh:mm:ss a} [%thread] %-5level %logger - %rEx %msg%n</pattern>
</layout>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOGFILE_PATH}/spring-mybatis-log-%d{yyyy-MM-dd}-%i.txt
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>2MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
上面的文件有什么missing/wrong吗??
是否可以记录(到文件)所有将打印到控制台的文本?
spring(或项目本身)如何读取logback.xml文件?如果我重命名它并将它放在另一个文件夹中怎么办?
如何创建一个 root 包含所有 levels(INFO、DEBUG、ERROR、WARN 等。 .) ?
您的配置看起来不错,但您可以尝试使用您的根级别 INFO 而不是 DEBUG 并且如果您有 [=17 这样的框架=]、hibernate 等。Logback 允许您对它们使用另一个级别,例如:
<logger name="org.hibernate" level="OFF" />
<logger name="org.springframework" level="INFO" />
<logger name="org.springframework.web.servlet.mvc" level="INFO" />
回答您的问题:
- 虽然我实际上没有尝试 运行,但您发布的文件在我看来没有明显错误。
- 就像你做的那样很好,有两个附加程序,一个去文件,另一个去控制台。
- Logback 默认在类路径中查找 logback.xml 文件。参考
<configuration>
元素中的 configuration page of the manual for the details. The way it gets there depends on your build system. When using Maven, I'd suggest putting it in src/main/resources
. But if it ends up in WEB-INF/classes
when deployed in your web app, that should work. If no matter what you put in your logback.xml file you are only getting console output (try adding a syntax error or renaming the file to test), that's what I'd look at first, to ensure that Logback is picking up the file right. It will default to showing everything just on the console if it can't find the file, though I think it shows a warning at the beginning that it's doing so. If it is picking up the file, you can try putting debug="true"
,看看是否有错误导致它无法按照您期望的方式使用 appender。
- 指定 "DEBUG" 级别的日志记录,正如您所做的那样,也将获得所有更高级别。
如果问题是 Spring 的日志记录没有到达您想要的位置,而 您的 应用程序的日志记录工作正常,您可能需要重定向 Spring(使用 Apache Commons Logging)改为使用 SLF4J。为此,删除 commons-logging 依赖项并添加 jcl-over-slf4j 库。这将模拟 commons-logging 调用并让它们指向 SLF4J。有关详细信息,请参阅 this blog post 的 "Using SLF4J" 部分。
我目前正在使用 SLF4J API 进行日志记录。
每当在运行时抛出异常时,完整的错误堆栈跟踪不会记录到文件中,它只会打印到控制台。我正在使用日食。
这是我的 logback.xml 代码(目前位于 WEB-INF 下的 类 文件夹中)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>
<!-- Specify here the path of the folder you want to save your logs -->
<property name="LOGFILE_PATH" value="C:/Logs" />
<!-- All logging will be redirected/ printed to console. -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{yyyy-MM-dd hh:mm:ss a} [%thread] %-5level %logger{50} - %rEx %msg%n </Pattern>
</layout>
</appender>
<!-- Send log to file -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOGFILE_PATH}/spring-mybatis-log.log</File>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{yyyy-MM-dd hh:mm:ss a} [%thread] %-5level %logger - %rEx %msg%n</pattern>
</layout>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOGFILE_PATH}/spring-mybatis-log-%d{yyyy-MM-dd}-%i.txt
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>2MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
上面的文件有什么missing/wrong吗??
是否可以记录(到文件)所有将打印到控制台的文本?
spring(或项目本身)如何读取logback.xml文件?如果我重命名它并将它放在另一个文件夹中怎么办?
如何创建一个 root 包含所有 levels(INFO、DEBUG、ERROR、WARN 等。 .) ?
您的配置看起来不错,但您可以尝试使用您的根级别 INFO 而不是 DEBUG 并且如果您有 [=17 这样的框架=]、hibernate 等。Logback 允许您对它们使用另一个级别,例如:
<logger name="org.hibernate" level="OFF" />
<logger name="org.springframework" level="INFO" />
<logger name="org.springframework.web.servlet.mvc" level="INFO" />
回答您的问题:
- 虽然我实际上没有尝试 运行,但您发布的文件在我看来没有明显错误。
- 就像你做的那样很好,有两个附加程序,一个去文件,另一个去控制台。
- Logback 默认在类路径中查找 logback.xml 文件。参考
<configuration>
元素中的 configuration page of the manual for the details. The way it gets there depends on your build system. When using Maven, I'd suggest putting it insrc/main/resources
. But if it ends up inWEB-INF/classes
when deployed in your web app, that should work. If no matter what you put in your logback.xml file you are only getting console output (try adding a syntax error or renaming the file to test), that's what I'd look at first, to ensure that Logback is picking up the file right. It will default to showing everything just on the console if it can't find the file, though I think it shows a warning at the beginning that it's doing so. If it is picking up the file, you can try puttingdebug="true"
,看看是否有错误导致它无法按照您期望的方式使用 appender。 - 指定 "DEBUG" 级别的日志记录,正如您所做的那样,也将获得所有更高级别。
如果问题是 Spring 的日志记录没有到达您想要的位置,而 您的 应用程序的日志记录工作正常,您可能需要重定向 Spring(使用 Apache Commons Logging)改为使用 SLF4J。为此,删除 commons-logging 依赖项并添加 jcl-over-slf4j 库。这将模拟 commons-logging 调用并让它们指向 SLF4J。有关详细信息,请参阅 this blog post 的 "Using SLF4J" 部分。