前几天在springboot中配置日志的时候发现Logger.getLogger()在编译的时候老是提示'找不到符号',于是找了好长时间的原因,最后发现引入包的时候引入的有问题。记录一下,方便日后查看 。
1.配置maven
<!--log4j日志框架-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.log4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<!--slf4j和log4j结合-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.6</version>
</dependency>
2.配置log4j.properties
log4j.rootLogger=DEBUG,A1
#将controller层log记录到ssmtest中
log4j.logger.controller=DEBUG,A2
#A1--打印到屏幕上
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-5p [%t] %37c %3x - %m%n
#A2--打印到文件ssmtest中--专门为controller层服务
log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A2.file= ../logs/ssmtest.log
log4j.appender.A2.DatePattern='.'yyyy-MM-dd
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
3.代码中如下:
@Controller
public class UserController {
Logger logger = Logger.getLogger(String.valueOf(UserController.class));
@Autowired
private UserService userService;
@RequestMapping("/getUserInfo")
@ResponseBody
public User getUserInfo() {
User user = userService.getUserInfo();
if(user!=null){
System.out.println("user.getName():"+user.getName());
logger.info("user.getAge():"+user.getAge());
}
return user;
}
}
注:如果在编译的时候Logger.getLogger()提示找不到符号,那么可能是包引入的有问题,应该引入如下的包:
import java.util.logging.Logger;
网友评论