日志记录是所有应用程序的重要组成部分,不仅使我们,开发人员受益,而且使系统的用户和维护人员受益。Spring Boot应用程序应收集相关的日志数据,以帮助我们诊断和修复问题并衡量业务绩效。
Spring Boot框架使用Logback作为对Spring框架的“有效”方法的默认实现进行了预配置。 本文探讨了在Spring Boot中配置日志记录的各种方法。
样例代码
本文随附GitHub上的示例工作代码 。
为什么日记很重要
关于记录什么和记录何处的决定通常是战略性的,并且是在应用程序可能无法在实际环境中正常运行的前提下做出的。日志在帮助应用程序从任何此类崩溃中快速恢复并恢复正常运行中起着关键作用。
使集成点的错误可见
使用微服务架构构建的当今应用程序的分布式特性引入了许多工作。因此,很自然地,由于任何基础架构系统中的临时故障,您都可能会遇到问题。
在集成点记录的异常日志使我们能够确定中断的根本原因,并使我们能够采取适当的措施进行恢复,而对最终用户体验的影响却最小。
诊断生产系统中的功能错误
可能会有客户抱怨交易金额不正确。为了对此进行诊断,我们需要深入研究日志,以从调用API时的请求数据到处理API末尾的响应数据找到操作序列。
事件历史分析
. , .
, , , .
, , , , . CI / CD.
Spring Boot
Spring Boot - Logback .
, Spring Boot. -, start.spring.io . :
@SpringBootApplication
public class SpringLoggerApplication {
static final Logger log =
LoggerFactory.getLogger(SpringLoggerApplication.class);
public static void main(String[] args) {
log.info("Before Starting application");
SpringApplication.run(SpringLoggerApplication.class, args);
log.debug("Starting my application in debug with {} args", args.length);
log.info("Starting my application with {} args.", args.length);
}
}
Maven Gradle jar , :
13:21:45.673 [main] INFO io.pratik.springLogger.SpringLoggerApplication - Before Starting application
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.2.RELEASE)
.
.
.
... : Started SpringLoggerApplication in 3.054 seconds (JVM running for 3.726)
... : Starting my application 0
, Spring, . .
application.properties
( application.yml
), .
. .
java -jar target/springLogger-0.0.1-SNAPSHOT.jar --trace
, , , .
, , Spring. , log.level.<package-name>
:
java \\
-jar target/springLogger-0.0.1-SNAPSHOT.jar \\
-Dlogging.level.org.springframework=ERROR \\
-Dlogging.level.io.pratik=TRACE
application.properties
:
logging.level.org.springframework=ERROR
logging.level.io.app=TRACE
, logging.file.name
logging.file.path
application.properties
. info.
# Output to a file named application.log.
logging.file.name=application.log
# Output to a file named spring.log in path /Users
logging.file.path=/Users
, logging.file.name
.
, Spring 2.2 , . 2.3.2.RELEASE.
, logging.pattern.file
:
# Logging pattern for file
logging.pattern.file= %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%
, :
| , | |
---|---|---|
|
| 10 Mb |
|
| 7 |
| . , . |
|
|
|
, .
Spring . , off application.properties
:
spring.main.banner-mode=off
ANSI, spring.output.ansi.enabled
. : , .
spring.output.ansi.enabled=ALWAYS
spring.output.ansi.enabled
DETECT
. , ANSI.
Logback Spring Boot . log4j java util, spring-boot-starter-loging pom.xml
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
logback-spring.xml
, logback.xml
logback-spring.xml
XML . Spring logback-spring.xml
, logback-spring.groovy
.
appender
configuration
. encoder
:
<configuration >
<include
resource="/org/springframework/boot/logging/logback/base.xml" />
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
</configuration>
Logback
debug
configuration
true
, .
<configuration debug="true">
, Logback, :
...- About to instantiate appender of type [...ConsoleAppender]
...- About to instantiate appender of type [...RollingFileAppender]
..SizeAndTimeBasedRollingPolicy.. - setting totalSizeCap to 0 Bytes
..SizeAndTimeBasedRollingPolicy.. - ..limited to [10 MB] each.
..SizeAndTimeBasedRollingPolicy.. Will use gz compression
..SizeAndTimeBasedRollingPolicy..use the pattern /var/folders/
..RootLoggerAction - Setting level of ROOT logger to INFO
, , .
, logback-spring.xml
. , .
. Spring Boot , . , Logstash:
<appender name="LOGSTASH"
class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>localhost:4560</destination>
<encoder charset="UTF-8"
class="net.logstash.logback.encoder.LogstashEncoder" />
</appender>
LogstashEncoder
JSON localhost:4560
. .
Lombok
, : Lombok, Slf4j
:
@Service
@Slf4j
public class UserService {
public String getUser(final String userID) {
log.info("Service: Fetching user with id {}", userID);
}
}
在本文中,我们已经了解了如何在Spring Boot中使用日志记录以及如何根据我们的要求进行配置。但是要充分利用这些优势,需要在整个开发团队中使用健壮和标准化的日志记录实践来补充框架的日志记录功能。
还需要通过同行评审和自动代码质量控制工具的组合来实现这些技术。综上所述,这可以确保在发生生产错误时,我们拥有尽可能多的信息来进行诊断。
您可以在Github上找到本文中使用的所有源代码 。