spring log4j.properties 没有日志的问题

一.   log4j.properties

1. log4j.properties放在spring工程的src/main/rescours目录下无法读取。

   测试后发现需要把log4j.properties文件放在src/main/webapp/WEB-INF目录下,

2. web.xml配置

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/log4j.properties</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

3. log4j.properties中日志文件路径

${webapp.root}/....  

4. 代码

private static final Log log= LogFactory.getLog("logname");

 

二. log4j.xml

1. log4j.xml放在spring工程的src/main/rescours目录下可以读取。

2. web.xml配置

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.xml</param-value>
</context-param>

<context-param>
    <param-name>rootLevel</param-name>
    <param-value>DEBUG</param-value>
</context-param>

<context-param>
    <param-name>loggingLevel</param-name>
    <param-value>INFO</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

3. log4j.properties中日志文件路径

${webapp.root}/....  

4. 代码

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

private static final Logger log= LoggerFactory.getLogger("logname");

 

转载于:https://www.cnblogs.com/myibm/p/5979937.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot支持多种日志框架,其中就包括log4j。将log4j配置到Spring Boot中需要以下步骤: 1. 在pom.xml文件中添加log4j的依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId> </dependency> ``` 2. 在src/main/resources目录下创建log4j.properties文件,然后在该文件中添加log4j的配置信息。例如: ``` # 设置日志级别为DEBUG log4j.rootLogger=DEBUG, console # 定义输出到控制台的appender log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.Target=System.out log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n ``` 3. 在Spring Boot的配置文件application.properties或application.yml中,设置logging.config属性的值为log4j.properties文件的路径。例如: application.properties ``` logging.config=classpath:log4j.properties ``` application.yml ``` logging: config: classpath:log4j.properties ``` 4. 在Spring Boot应用程序中使用日志。例如: ``` import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class MyClass { private static final Logger logger = LoggerFactory.getLogger(MyClass.class); public void doSomething() { logger.debug("This is a debug message"); logger.info("This is an info message"); logger.warn("This is a warn message"); logger.error("This is an error message"); } } ``` 以上就是在Spring Boot中配置log4j.properties的完整步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值