Get the App
SLTechnology News&Howtos  ›  Development  › 

How to save console log in SpringBoot project

Shulou Source: shulou.com Published: 2022-06-01 01:09:25 09月21日 Update

This article mainly introduces the relevant knowledge of "how to save the console log in the SpringBoot project". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "how to save the console log in the SpringBoot project" can help you solve the problem.

Save configuration of SpringBoot console Log

Scenario: the console log needs to be saved in the SpringBoot project.

Implementation method:

1. In the configuration file application-prod.yml

The configuration is as follows:

Logging: config: classpath:logback-spring-prod.xml2.logback-spring-prod.xml

This file is recorded in the console to print the log and save the path configuration as follows:

% black (% contextName -)% red (% d {yyyy-MM-dd HH:mm:ss})% green ([% thread])% highlight (%-5level)% boldMagenta (% logger {60}) -% gray (% msg%n) DEBUG ACCEPT DENY ${logPathDebug} / ${name} _ debug.log ${logPathDebug} / ${name} _ debug.log.%d {yyyy-MM-dd}.% i.log ${maxHistory} 50MB% d {yyyy-MM-dd HH:mm:ss.SSS} [% thread]-[%-5level] [% logger {50}:% line ] -% msg%n INFO ACCEPT DENY ${logPathInfo} / ${name} _ info.log ${logPathInfo} / ${name} _ info.log.%d {yyyy-MM-dd}.% i.log ${maxHistory} 50MB % d {yyyy-MM-dd HH:mm:ss.SSS} [% thread]-[%-5level] [% logger {50}:% line] -% msg%n WARN ACCEPT DENY ${logPathWarn} / ${name} _ Warn.log ${logPathWarn} / ${name} _ warn.log.%d {yyyy-MM-dd}.% i.log ${maxHistory} 50MB% d {yyyy-MM-dd HH:mm:ss.SSS} [% thread]-[%-5level] [% logger {50}:% line] -% msg%n ERROR ACCEPT DENY ${logPathError} / ${name} _ error.log ${logPathError} / ${name} _ error.log.%d {yyyy-MM-dd}.% i.log ${maxHistory} 50MB% d {yyyy-MM-dd HH:mm:ss.SSS} [% thread]-[%-5level] [% logger {50}:% line] -% msg%n 0 ${queueSize} 0 ${queueSize} 0 ${queueSize} 0 ${queueSize}

Note:

The path to the Chinese log is stored under / usr/server/shop/logs/.

How does a SpringBoot project save the runtime log of a service

The problem is relatively simple, with only two steps to complete.

Configure the file for the log4j output log

In pom, you need to refer to lombok dependency, and the version can follow your springboot husband version.

Org.projectlombok lombok true StudentContext% 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 outputs the log file and puts it under resources. Note that the fileNamePattern tag is replaced with something that identifies the role of your microservice, such as the student management system I wrote, so it uses student.

In application specify the location of this configuration file logging: level: com.netflix.discovery.shared.resolver.aws.ConfigClusterResolver: WARN config: classpath:logback-common.xml on "how to save console logs in the SpringBoot project". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Log console control configuration project file UTF-8 knowledge method version industry path problem service output different practical east-west two location Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MySQL Docker Linux vpn