Spring Boot 外部 WAR 在 Apache Tomcat 中运行没有日志



我已经按照指南中配置了Spring Boot以与tomcat一起使用: https://www.baeldung.com/spring-boot-war-tomcat-deploy .当我从IDE运行应用程序时,我可以在控制台上看到所有内容,但是在tomcat上,日志没有显示任何内容,我还配置了以下内容:

logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR

没有运气知道吗?

我的application.properites

# ===============================
# = DATA SOURCE
# ===============================
spring.datasource.url = jdbc:mysql://...
spring.datasource.username = root
spring.datasource.password = admin
# ===============================
# = JPA / HIBERNATE
# ===============================
spring.jpa.show-sql = true
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
# ==============================================================
# = Spring Security / Queries for AuthenticationManagerBuilder
# ==============================================================
spring.queries.users-query=select email, password, active from user where email=?
spring.queries.roles-query=select u.email, r.role from user u inner join user_role ur on(u.id=ur.user_id) inner join role r on(ur.role_id=r.id) where u.email=?
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR

我遇到了同样的问题,所以我认为如果有人有同样的情况,我的答案会有所帮助。

当我们创建 springboot 应用程序时,我们忘记在主类中扩展SpringBootServletInitializer。如果没有这一点,您的应用程序和 tomcat 容器之间的 servlet 上下文绑定就不会发生。这就是为什么,虽然战争爆发了,但应用程序并没有实际部署,因此没有应用程序日志。在此处查找更多详细信息 https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/web/servlet/support/SpringBootServletInitializer.html

@EnableSwagger2
@SpringBootApplication
@EnableJpaRepositories("com.scn.scheduler.dal")
@EnableScheduling
public class SCNSchedulerApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(SCNSchedulerApplication.class, args);
}
}

在 Spring Boot 的application.properties中添加以下内容

logging.file=../logs/mylog.log

在应用程序属性中使用以下方法的答案是正确的:

logging.file=../logs/mylog.log

但它没有提到为什么没有打印到卡塔琳娜的消息.log . 完整的答案来自 Spring 引导文档:

默认情况下,Spring 引导仅记录到控制台,不会写入 日志文件。如果要在控制台之外写入日志文件 输出,您需要设置 logging.file 或 logging.path 属性(对于 例如,在您的应用程序属性中(。

>通过在下面添加属性来application.properties对我有用

logging.file.name = ${catalina.base}/logs/${service.name}.log

当您从 IDE 运行文件弹簧启动战争并希望在 IDE 上显示日志控制台时。您必须编辑文件pom.xml:

第一。你必须排除弹簧引导启动器日志记录

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
**<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>**
</exclusions>
</dependency>

第二。您添加依赖关系logback-classic和jcl-over-slf4j

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>   

我做到了,我在IDE控制台上看到了日志。

最新更新