Springboot配置保存日志文件
共 4201字,需浏览 9分钟
·
2021-10-08 22:18
点击上方“码农突围”,马上关注
这里是码农充电第一站,回复“666”,获取一份专属大礼包 真爱,请设置“星标”或点个“在看
来源:blog.csdn.net/gaopinqiang/
article/details/109228641
springboot日志配置:
springboot默认日志是打印再console中的,不会保存在文件中。我们项目上线肯定要保存日志用于分析的。
一、使用xml配置日志保存(并不需要pom配置slf4j依赖,starter里面已经配置了依赖了)
1、在项目的resources目录下创建一个【logback-spring.xml】日志配置文件
名称只要是一 logback开头就行,测试使用log.xml并不会生成日志。合法名称:logback.xml
、logback-spring.xml
备注:要配置logback-spring.xml
,springboot会默认加载此文件,为什么不配置logback.xml
,因为logback.xml
会先application.properties
加载,而logback-spring.xml
会后于application.properties
加载,这样我们在application.properties
文中设置日志文件名称和文件路径才能生效
2、编写xml文件内容:
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<contextName>logbackcontextName>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%npattern>
encoder>
appender>
<appender name="logFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<Prudent>truePrudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>
poslog/%d{yyyy-MM-dd}/%d{yyyy-MM-dd}.log
FileNamePattern>
<maxHistory>7maxHistory>
rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n
Pattern>
layout>
appender>
<root level="INFO">
<appender-ref ref="console" />
<appender-ref ref="logFile" />
root>
configuration>
上述配置的编码中,对应符号的含义如下
%d{HH:mm:ss.SSS}
——日志输出时间%thread
——输出日志的进程名字,这在Web应用以及异步任务处理中很有用%-5level
——日志级别,并且使用5个字符靠左对齐%logger
——日志输出者的名字%msg
——日志消息%n
——平台的换行符
3、编写打印日志
private Logger logger = LoggerFactory.getLogger(TestController.class);
logger.info("name = " + name + " || views = " + views);// 使用
4、启动测试
在当前文件夹下会创建一个【poslog/2020-10/22】的文件夹,里面会按天生成日志:【2020-10-22.log】,例如:
2020-10-22 16:38 [main] INFO org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8088 (http)
2020-10-22 16:38 [main] INFO org.apache.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8088"]
2020-10-22 16:38 [main] INFO org.apache.catalina.core.StandardService - Starting service [Tomcat]
2020-10-22 16:38 [main] INFO org.apache.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/9.0.38]
2020-10-22 16:38 [main] INFO org.apache.catalina.core.AprLifecycleListener - Loaded Apache Tomcat Native library [1.2.23] using APR version [1.7.0].
2020-10-22 16:38 [main] INFO org.apache.catalina.core.AprLifecycleListener - APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
2020-10-22 16:38 [main] INFO org.apache.catalina.core.AprLifecycleListener - APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
2020-10-22 16:38 [main] INFO org.apache.catalina.core.AprLifecycleListener - OpenSSL successfully initialized [OpenSSL 1.1.1c 28 May 2019]
2020-10-22 16:38 [main] INFO org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
2020-10-22 16:38 [main] INFO org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 1422 ms
2020-10-22 16:38 [main] INFO org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor - Initializing ExecutorService 'applicationTaskExecutor'
2020-10-22 16:38 [main] INFO org.apache.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-8088"]
2020-10-22 16:38 [main] INFO org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 8088 (http) with context path ''
2020-10-22 16:38 [main] INFO com.example.demo.DockerComposeTestApplication - Started DockerComposeTestApplication in 2.841 seconds (JVM running for 3.257)
备注:发现使用这个默认不用配置pom依赖,最新的spring-boot-starter-web
中已经集成了。
总结:启动一个项目,直接将logback-spring.xml
文件复制到resources目录下就可以实现日志文件记录。非常方便。
二、进阶配置:
分类logback.xml配置:
日志配置到d盘了
<configuration>
<property name="log.path" value="d:/logback" />
<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" />
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log.pattern}pattern>
encoder>
appender>
<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/sys-info.logfile>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/sys-info.%d{yyyy-MM-dd}.logfileNamePattern>
<maxHistory>60maxHistory>
rollingPolicy>
<encoder>
<pattern>${log.pattern}pattern>
encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/sys-error.logfile>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/sys-error.%d{yyyy-MM-dd}.logfileNamePattern>
<maxHistory>60maxHistory>
rollingPolicy>
<encoder>
<pattern>${log.pattern}pattern>
encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERRORlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<appender name="sys-user" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/sys-user.logfile>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/sys-user.%d{yyyy-MM-dd}.logfileNamePattern>
<maxHistory>60maxHistory>
rollingPolicy>
<encoder>
<pattern>${log.pattern}pattern>
encoder>
appender>
<logger name="com.example" level="debug" />
<logger name="org.springframework" level="warn" />
<root level="info">
<appender-ref ref="console" />
root>
<root level="info">
<appender-ref ref="file_info" />
<appender-ref ref="file_error" />
root>
<logger name="sys-user" level="info">
<appender-ref ref="sys-user"/>
logger>
configuration>
还可以在application.yml中配置
# 日志配置
logging:
level:
com.example: info
org.springframework: warn
配置info就只有info以上日志才会输出
如果配置 com.example: debug
,那么我们项目com.example
包里面debug以上的日志也会输出。
- END - 最近热文
• 程序员之天梯排行榜,你在哪一级? • 提高千倍效率的 Java 代码小技巧 • 武大94年博士年薪201万入职华为天才计划!学霸日程表曝光!太牛逼了! • 22年前被嘲养猪的北大学子,如今怎么样了? • 清华计算机系上热搜!近9成优秀毕业生放弃留学,前50名41人留校深造