启用设置org.slf4j.Logger打印并输出日志方式
org.slf4j.Logger打印并输出日志
在resouces目录下面新建logback.xml(此为Logback推荐目录)
内容配置如下:
logback 分为两种设置:
1. 输出到控制台 STDOUT
2. 输出到文件 FILE
pom.xml配置
<properties> <slf4j.version>1.7.25</slf4j.version> </properties> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> </dependency>
logback.xml配置
(下面的配置同时配置输出到文件和输出到控制台)
<?xml version="1.0" encoding="UTF-8" ?> <configuration scan="true" scanPeriod="3 seconds"> <!--设置日志输出为控制台--> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%X{userId}] [%X{requestId}] %logger - %msg%n</pattern> </encoder> </appender> <!--设置日志输出为文件--> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>logFile.log</File> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>logFile.%d{yyyy-MM-dd_HH-mm}.log.zip</FileNamePattern> </rollingPolicy> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern>%d{HH:mm:ss,SSS} [%thread] %-5level %logger{32} - %msg%n</Pattern> </layout> </appender> <root> <level value="DEBUG"/> <appender-ref ref="STDOUT"/> <appender-ref ref="FILE"/> </root> </configuration>
程序调用
1.申明 logger 变量
private Logger logger = LoggerFactory.getLogger(LoginLogDao.class);
2.在程序中调用日志
logger.debug(INSERT_LOGIN_LOG_SQL);
官方介绍网址:https://logback.qos.ch/demo.html
下面为官网介绍
logback-classic with two appenders: a ConsoleAppender and a RollingFileAppender. The RollingFileAppender sends logging events to a file called logFile.log and will rollover the active file every minute. The old file will be renamed and compressed to a zip file. The ConsoleAppender will output the logging requests to the console, and shorten the logger names to gain space on the console window, without loss of legibility. For example, ch.qos.logback.demo.prime.NumberCruncherImpl will be abbreviated as c.q.l.d.prime.NumberCruncherImpl.
输出结果如下
isDebugEnabled true
2017-04-23 23:58:35,502 DEBUG [http-nio-8080-exec-6] (LoginLogDao.java:32) - INSERT INTO t_login_log(user_id,ip,login_datetime) VALUES(?,?,?)
2017-04-23 23:58:35,503 DEBUG [http-nio-8080-exec-6] (JdbcTemplate.java:869) - Executing prepared SQL update
2017-04-23 23:58:35,503 DEBUG [http-nio-8080-exec-6] (JdbcTemplate.java:616) - Executing prepared SQL statement [INSERT INTO t_login_log(user_id,ip,login_datetime) VALUES(?,?,?)]
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Java中的CopyOnWriteArrayList原理详解
这篇文章主要介绍了Java中的CopyOnWriteArrayList原理详解,如源码所示,CopyOnWriteArrayList和ArrayList一样,都在内部维护了一个数组,操作CopyOnWriteArrayList其实就是在操作内部的数组,需要的朋友可以参考下2023-12-12Springboot如何读取resources下的json配置文件
这篇文章主要介绍了Springboot如何读取resources下的json配置文件问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-07-07SpringMVC中MultipartFile上传获取图片的宽度和高度详解
本篇文章主要介绍了SpringMVC中MultipartFile上传获取图片的宽度和高度,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-05-05
最新评论