Spring +slf4j logger

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/kezhen/article/details/52061423

添加slf4j-api和slf4j-log4j12 两个jar包:

    <dependency>
    	<groupId>org.slf4j</groupId>
    	<artifactId>slf4j-api</artifactId>
    	<version>1.7.21</version>
    </dependency>
    <dependency>
    	<groupId>org.slf4j</groupId>
    	<artifactId>slf4j-log4j12</artifactId>
    	<version>1.7.21</version>
    </dependency>

在src/main/resources目录下添加配置文件log4j.properties:

# Root logger option
log4j.rootLogger=INFO, stdout, file

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Redirect log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
#log4j.appender.file.File=${catalina.home}/logs/myapp.log
log4j.appender.file.File=myapp.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
在需要输出日志的位置使用:

...
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
...
private Logger logger=LoggerFactory.getLogger(UserController.class);
...
logger.info("registrate new user,username="+userForm.getUsername()+",password="+userForm.getPassword());
...


展开阅读全文

没有更多推荐了,返回首页