SpringBoot console log saving configuration
Scenario: The console log needs to be saved in the SpringBoot project.
Implementation method:
1. Configure the configuration file application-prod.yml
as follows:
logging: config: classpath:logback-spring-prod.xml
2.logback-spring-prod.xml
This file is the path where the console print log is saved. The path configuration is as follows:
<?xml version="1.0" encoding="UTF-8"?> <!-- 分级别异步文件日志输出配置 --> <configuration> <!-- 日志级别 --> <property></property> <!-- 日志地址 --> <property></property> <property></property> <property></property> <property></property> <property></property> <!-- 最大保存时间 --> <property></property> <!-- 异步缓冲队列的深度,该值会影响性能.默认值为256 --> <property></property> <!--日志文件名前缀--> <property></property> <appender> <encoder> <pattern>%black(%contextName -) %red(%d{yyyy-MM-dd HH:mm:ss}) %green([%thread]) %highlight(%-5level) %boldMagenta(%logger{60}) - %gray(%msg%n)</pattern> </encoder> </appender> <appender> <filter> <level>DEBUG</level> <onmatch>ACCEPT</onmatch> <onmismatch>DENY</onmismatch> </filter> <file>${logPathDebug}/${name}_debug.log</file> <rollingpolicy> <filenamepattern>${logPathDebug}/${name}_debug.log.%d{yyyy-MM-dd}.%i.log</filenamepattern> <maxhistory>${maxHistory}</maxhistory> <timebasedfilenamingandtriggeringpolicy> <maxfilesize>50MB</maxfilesize> </timebasedfilenamingandtriggeringpolicy> </rollingpolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [ %thread ] - [ %-5level ] [ %logger{50} : %line ] - %msg%n</pattern> </encoder> </appender> <appender> <filter> <level>INFO</level> <onmatch>ACCEPT</onmatch> <onmismatch>DENY</onmismatch> </filter> <file>${logPathInfo}/${name}_info.log</file> <rollingpolicy> <filenamepattern>${logPathInfo}/${name}_info.log.%d{yyyy-MM-dd}.%i.log</filenamepattern> <maxhistory>${maxHistory}</maxhistory> <timebasedfilenamingandtriggeringpolicy> <maxfilesize>50MB</maxfilesize> </timebasedfilenamingandtriggeringpolicy> </rollingpolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [ %thread ] - [ %-5level ] [ %logger{50} : %line ] - %msg%n</pattern> </encoder> </appender> <appender> <filter> <level>WARN</level> <onmatch>ACCEPT</onmatch> <onmismatch>DENY</onmismatch> </filter> <file>${logPathWarn}/${name}_warn.log</file> <rollingpolicy> <filenamepattern>${logPathWarn}/${name}_warn.log.%d{yyyy-MM-dd}.%i.log</filenamepattern> <maxhistory>${maxHistory}</maxhistory> <timebasedfilenamingandtriggeringpolicy> <maxfilesize>50MB</maxfilesize> </timebasedfilenamingandtriggeringpolicy> </rollingpolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [ %thread ] - [ %-5level ] [ %logger{50} : %line ] - %msg%n</pattern> </encoder> </appender> <appender> <filter> <level>ERROR</level> <onmatch>ACCEPT</onmatch> <onmismatch>DENY</onmismatch> </filter> <file>${logPathError}/${name}_error.log</file> <rollingpolicy> <filenamepattern>${logPathError}/${name}_error.log.%d{yyyy-MM-dd}.%i.log</filenamepattern> <maxhistory>${maxHistory}</maxhistory> <timebasedfilenamingandtriggeringpolicy> <maxfilesize>50MB</maxfilesize> </timebasedfilenamingandtriggeringpolicy> </rollingpolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [ %thread ] - [ %-5level ] [ %logger{50} : %line ] - %msg%n</pattern> </encoder> </appender> <appender> <!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 --> <discardingthreshold>0</discardingthreshold> <!-- 更改默认的队列的深度,该值会影响性能.默认值为256 --> <queuesize>${queueSize}</queuesize> <appender-ref></appender-ref> </appender> <appender> <!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 --> <discardingthreshold>0</discardingthreshold> <!-- 更改默认的队列的深度,该值会影响性能.默认值为256 --> <queuesize>${queueSize}</queuesize> <appender-ref></appender-ref> </appender> <appender> <!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 --> <discardingthreshold>0</discardingthreshold> <!-- 更改默认的队列的深度,该值会影响性能.默认值为256 --> <queuesize>${queueSize}</queuesize> <appender-ref></appender-ref> </appender> <appender> <!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 --> <discardingthreshold>0</discardingthreshold> <!-- 更改默认的队列的深度,该值会影响性能.默认值为256 --> <queuesize>${queueSize}</queuesize> <appender-ref></appender-ref> </appender> <root> <!-- appender referenced after it is defined --> <appender-ref></appender-ref> <appender-ref></appender-ref> <appender-ref></appender-ref> <appender-ref></appender-ref> <appender-ref></appender-ref> </root> </configuration>
Note: The path where the log is stored in
<property></property>
is /usr/server/shop/logs /stored under.
How does the SpringBoot project save the runtime log of the service?
This problem is relatively simple. You only need to complete two steps.
Configure the log4j output log file
You need to quote the lombok dependency in the pom. The version can follow your springboot version.
<dependency> <groupid>org.projectlombok</groupid> <artifactid>lombok</artifactid> <optional>true</optional> </dependency>
StudentContext <property></property>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{76}.%M\(%line\) - %msg%n UTF-8 ${log.path}/student/student.%d{yyyy-MM-dd}.log 60 %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{76}.%M\(%line\) - %msg%n UTF-8 ${log.path}/student/student.warn.%d{yyyy-MM-dd}.log 60 %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{76}.%M\(%line\) - %msg%n UTF-8 WARN ACCEPT DENY ${log.path}/student/student.error.%d{yyyy-MM-dd}.log 60 %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{76}.%M\(%line\) - %msg%n UTF-8 ERROR ACCEPT DENY
log4j output log file, just put it under resources. Note that the things in the fileNamePattern tag are replaced with logos Your microservice functions, such as the student management system I wrote, so I use student
Specify the location of this configuration file in the application
logging: level: com.netflix.discovery.shared.resolver.aws.ConfigClusterResolver: WARN config: classpath:logback-common.xml
The above is the detailed content of How to save console logs in SpringBoot project. For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1
Powerful PHP integrated development environment