Spring boot 2 + log detail 日志不起作用,有Logback、Hibernate + Weblogic
Posted
技术标签:
【中文标题】Spring boot 2 + log detail 日志不起作用,有Logback、Hibernate + Weblogic【英文标题】:Spring boot 2 + log detail logs do not work, with Logback, Hibernate + Weblogic 【发布时间】:2020-06-18 05:09:03 【问题描述】:我使用 oracle 11.x、Spring boot 2.x、maven、weblogic - 作为外部服务器。
入口点@SpringBootConfiguration
@SpringBootApplication
public class WebSpringBootJarApplication
extends SpringBootServletInitializer
implements WebApplicationInitializer
private static final Logger LOGGER = LoggerFactory.getLogger( WebSpringBootJarApplication.class );
public static void main(String[] args)
SpringApplication.run(WebSpringBootJarApplication.class, args);
LOGGER.info("Start an application...");
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder)
LOGGER.info("There is building the web application!");
return builder.sources(WebSpringBootJarApplication.class);
application.properties
spring.main.banner-mode=off
####################################################
# Oracle 11x
####################################################
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
logging.config=classpath:logger/logback-spring.xml
logging.file.dir=logs
logging.file.name.var=log.log
logging.file.archive.format.name=program_.%ddd-MM-yyyy.log
logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true" scanPeriod="10 seconds" >
<include resource="logger/settingslogger/defaults-spring.xml"/>
<springProfile name="default">
<include resource="logger/consolelogger/console-appender-spring.xml"/>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
</springProfile>
<springProfile name="production">
<include resource="logger/productionappenderlogger/logback-appender-production-spring.xml"/>
<logger name="org.springframework.web" level="DEBUG">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="org.springframework.data" level="DEBUG">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="weblogic.war.spring.boot" level="ERROR">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="weblogic.war.spring.boot" level="INFO">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="weblogic.war.spring.boot" level="INFO">
<appender-ref ref="FILE-ROLLING"/>
</logger>
<logger name="weblogic.war.spring.boot" level="INFO">
<appender-ref ref="FILE-ROLLING"/>
</logger>
</springProfile>
</configuration>
当邮递员发送请求时,我有响应:
"时间戳": "2020-03-05T13:47:37.128+0000", “状态”:500, "error": "内部服务器错误", "message": "JTA 事务意外回滚(可能是由于超时);嵌套异常是 weblogic.transaction.RollbackException: setRollbackOnly called on transaction", “路径”:“/sat/api/保存/不同类型”
我看到的控制台是这样的:
05-03-2020 16:47:37.095 DEBUG 10500 [(自我调整)'] mmaRequestResponseBodyMethodProcessor:将“application/json;charset=UTF-8”读取到 [weblogic.war.spring.boot.service。 dto.differenttypes.DifferentTypesDtoEightProduce@f8bde55f 不同(截断)...] 05-03-2020 16:47:37.124 DEBUG 10500 [(self-tuning)'] osweb.servlet.DispatcherServlet:未能完成请求:org.springframework.transaction.UnexpectedRollbackException:JTA 事务意外回滚(可能是由于暂停);嵌套异常是 weblogic.transaction.RollbackException: setRollbackOnly called on transaction
更新
我尝试在 logback-spring.xml 中添加以下设置:
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true" scanPeriod="10 seconds">
<include resource="logger/default/defaults-spring.xml"/>
<springProfile name="default">
<include resource="logger/console/console-appender-spring.xml"/>
<root level="TRACE">
<appender-ref ref="CONSOLE"/>
</root>
</springProfile>
<springProfile name="dev">
<include resource="logger/console/console-appender-spring.xml"/>
<include resource="logger/production/logback-appender-production-spring.xml"/>
<!-- <root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>-->
<logger name="ch.qos.logback" level="DEBUG" additivity="true">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="org.springframework.web" level="DEBUG" additivity="true" >
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="org.springframework.data" level="TRACE" additivity="true">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="org.springframework.data.jpa" level="TRACE" additivity="true">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="org.hibernate" level="TRACE" additivity="true">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="org.hibernate.validator" level="TRACE" additivity="true">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="org.hibernate.sql" level="TRACE" additivity="true">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="org.hibernate.type.descriptor.sql" level="TRACE" additivity="true">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="TRACE" additivity="true">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="org.springframework.jdbc.core.StatementCreatorUtils" level="TRACE" additivity="true">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="org.springframework.jdbc.core.PreparedStatementCreator" level="TRACE" additivity="true">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="com.dao" level="INFO">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="com.service" level="INFO">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="com.web" level="INFO">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="ch.qos.logback" level="DEBUG">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="org.springframework.web" level="DEBUG" additivity="false">
<appender-ref ref="FILE-ROLLING"/>
</logger>
<logger name="org.springframework.data" level="DEBUG" additivity="false">
<appender-ref ref="FILE-ROLLING"/>
</logger>
<logger name="org.springframework.data.jpa" level="DEBUG" additivity="false" >
<appender-ref ref="FILE-ROLLING"/>
</logger>
<logger name="org.hibernate" level="DEBUG" additivity="false">
<appender-ref ref="FILE-ROLLING"/>
</logger>
<logger name="org.hibernate.validator" level="WARN">
<appender-ref ref="FILE-ROLLING"/>
</logger>
<logger name="org.hibernate.sql" level="DEBUG" additivity="false">
<appender-ref ref="FILE-ROLLING"/>
</logger>
<logger name="org.hibernate.type.descriptor.sql" level="TRACE" additivity="false">
<appender-ref ref="FILE-ROLLING"/>
</logger>
<logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="TRACE" additivity="false">
<appender-ref ref="FILE-ROLLING"/>
</logger>
<logger name="org.springframework.jdbc.core.StatementCreatorUtils" level="TRACE" additivity="false">
<appender-ref ref="FILE-ROLLING"/>
</logger>
<logger name="org.springframework.jdbc.core.PreparedStatementCreator" level="TRACE" additivity="false">
<appender-ref ref="FILE-ROLLING"/>
</logger>
<logger name="com.dao" level="INFO">
<appender-ref ref="FILE-ROLLING"/>
</logger>
<logger name="com.service" level="INFO">
<appender-ref ref="FILE-ROLLING"/>
</logger>
<logger name="com.web" level="INFO">
<appender-ref ref="FILE-ROLLING"/>
</logger>
<!-- http://logback.qos.ch/manual/configuration.html-->
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<resetJUL>true</resetJUL>
</contextListener>
</springProfile>
</configuration>
spring.profiles.active=dev
logging.config=classpath:logger/logback-spring.xml
logging.file.dir=logs_gov
logging.file.name.var=gov_new.log
logging.file.archive.format.name=gov_new.%ddd-MM-yyyy.log
weblogic.xml
<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app
xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
https://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd
http://xmlns.oracle.com/weblogic/weblogic-web-app
https://xmlns.oracle.com/weblogic/weblogic-web-app/1.9/weblogic-web-app.xsd">
<wls:context-root>sun</wls:context-root>
<wls:container-descriptor>
<wls:prefer-application-packages>
<wls:package-name>ch.qos.logback.*</wls:package-name>
<wls:package-name>org.slf4j.*</wls:package-name>
</wls:prefer-application-packages>
<!-- <wls:prefer-application-resources>
<wls:resource-name>ch/qos/logback/core/ConsoleAppender.class</wls:resource-name>
<wls:resource-name>ch/qos/logback/core/rolling/RollingFileAppender.class</wls:resource-name>
<wls:resource-name>ch/qos/logback/classic/encoder/PatternLayoutEncoder.class</wls:resource-name>
<wls:resource-name>ch/qos/logback/core/rolling/TimeBasedRollingPolicy.class</wls:resource-name>
</wls:prefer-application-resources>-->
</wls:container-descriptor>
</wls:weblogic-web-app>
发生事务回滚时,我没有在控制台中看到错误。
12-03-2020 14:21:25.756 INFO 7216 c.w.r.s.d.DifferentTypesSaveRest : 写入对象 : DifferentTypesDtoEightProducestatus=OPEN 不同类型DtoFouryear=2020, car=true com.service.dto.differenttypes.DifferentTypesDtoEightProduce@64e50443 DifferentTypesDtoTwoid=135, name='fan5' 不同类型DtoSixcost=10.0, character=W 12-03-2020 14:21:25.759 调试 7216 m.m.a.RequestResponseBodyMethodProcessor:使用 'application/json',给定 [/] 并支持 [application/json] 2020 年 12 月 3 日 14:21:25.774 调试 7216 m.m.a.RequestResponseBodyMethodProcessor : 写作 [DifferentTypesDtoEightProducestatus=OPEN DifferentTypesDtoFouryear=2020, car=true com.service.dt (截断)...] 2020 年 12 月 3 日 14:21:25.781 调试 7216 os.web.servlet.DispatcherServlet : 完成 200 OK 12-03-2020 14:23:13.766 调试 7216 os.web.servlet.DispatcherServlet : POST "/sun/api/save/differentTypes", 参数= 12-03-2020 14:23:13.766 调试 7216 s.w.s.m.m.a.RequestMappingHandlerMapping : 映射到 com.web.rest.save.differentTypes.DifferentTypesSaveRest#setDifferentTypes(DifferentTypesDtoEightProduce) 2020 年 12 月 3 日 14:23:13.767 调试 7216 m.m.a.RequestResponseBodyMethodProcessor :读取 “应用程序/json;charset=UTF-8”到 [DifferentTypesDtoEightProducestatus=OPEN DifferentTypesDtoFouryear=2020, car=true com.service.dt (截断)...] 2020 年 12 月 3 日 14:23:13.804 调试 7216 os.web.servlet.DispatcherServlet :无法完成请求: org.springframework.transaction.UnexpectedRollbackException:JTA 事务意外回滚(可能是由于超时);嵌套的 例外是 weblogic.transaction.RollbackException: setRollbackOnly 调用事务 12-03-2020 14:23:13.811 调试 7216 os.web.servlet.DispatcherServlet : POST 的“FORWARD”调度 “/sun/error”,参数= 12-03-2020 14:23:13.812 调试 7216 s.w.s.m.m.a.RequestMappingHandlerMapping :映射到 org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest) 2020 年 12 月 3 日 14:23:13.820 调试 7216 o.s.w.s.m.m.a.HttpEntityMethodProcessor :使用“应用程序/json”, 给定 [/] 并支持 [application/json, application/+json, 应用程序/json,应用程序/+json] 12-03-2020 14:23:13.820 调试 7216 o.s.w.s.m.m.a.HttpEntityMethodProcessor:写作 [timestamp=Thu Mar 12 14:23:13 MSK 2020, status=500, error=Internal 服务器错误,消息 = JTA 事务(截断)...] 2020 年 12 月 3 日 14:23:13.824 调试 7216 os.web.servlet.DispatcherServlet: 退出“FORWARD”调度,状态 500
如何将详细的错误输出配置到控制台和文件中,当工作期间出现错误Hibernate,当Hibernate 抛出数据库异常时......
当应该写入数据库的值已经插入实例 PreparedStatement
时,我该如何配置【问题讨论】:
【参考方案1】:尝试将以下行添加到您的 weblogic.xml:
<wls:prefer-application-resources>
<wls:resource-name>org/slf4j/impl/StaticLoggerBinder.class</wls:resource-name>
</wls:prefer-application-resources>
这行也不是必须的:
<wls:package-name>ch.qos.logback.*</wls:package-name>
因为 logback 是您已经包含的 sl4j 的实现
【讨论】:
以上是关于Spring boot 2 + log detail 日志不起作用,有Logback、Hibernate + Weblogic的主要内容,如果未能解决你的问题,请参考以下文章
外部Log4j.properties在不打印日志中-Spring Boot 2.2.1