Spring Boot logging SLF4J
To print the content during development, use System. out. println () and Log4j should be the human Bypass Method.
In fact, we do not recommend using System. out during development because a large amount of System. out will increase resource consumption.
Log4j is more flexible and has higher performance than System. out. We can configure the output level and specify multiple log files to record different logs.
System. out is executed in the current thread, and the following program is executed after the file is written. The Log tool can not only control whether or not logs are output, but also how to output logs. Its processing mechanism is also to notify Log writing, so you do not have to wait for the Log to finish the subsequent code execution.
If this is not necessary, we recommend that you do not use console output because it is too messy if the console output has no priority.
I personally recommend using SLF4J (Simple Logging Facade For Java) logback to output logs, which is better than log4j because it is more efficient.
Spring Boot provides a set of log systems, and logback is the top choice. With logback. xml configured, you can use the default log configuration provided by Spring Boot:
In this way, a log capturing org. springframework. web is defined. The log level is DEBUG, and the content of the base. xml file referenced above is:
The log system of Spring Boot pre-defines some system variables:
PID, current process ID {LOG_FILE}, the value of logging. file in the Spring Boot configuration file (application. properties |. yml)
$ {LOG_PATH}: the value of logging. path in the Spring Boot configuration file
At the same time, by default there is another appender -- one is the console, the other is the file, which is defined in the console-appender.xml and file-appender.xml, respectively. At the same time, the log level of the application can also be defined through application. properties:
logging.level.org.springframework.web=DEBUGlogging.level.org.springboot.sample=TRACE
This is equivalent to the log level we configured in logback. xml. The name starts with logging. level, followed by the package name of the log to be entered.
* If logback. xml and application. properties defines the same configuration (for example, org. springframework. web) but the output level is different, the actual application. properties has a higher priority than logback. xml *
Now that maven is used to manage projects, we can define different log outputs according to different environments. springProfile nodes are used in logback. xml to define them. The method is as follows:
As shown above, the TRACE-level output is defined for org. springboot. sample by default. The following two springprofiles are defined: dev and staging, and the output levels are DEBUG and INFO.
You can specify the profile when starting the service (for example, if you do not specify the default profile). For example, you can specify the staging mode as follows:
java -jar myapp.jar --spring.profiles.active=staging
The following describes two common Appender
ConsoleAppender
Logback uses appender to define log output. The most common feature in the development process is to output logs to the console:
.%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg %n
TRACE
Indicates encoding logs.
% D {HH: mm: ss. SSS} -- log output time % thread -- Name of the process that outputs the log, which is useful in Web applications and asynchronous Task Processing %-5level -- log level, and use five characters to align to the left: % logger {36} -- log outputer name % msg -- Log message % n -- platform line break
The output result of the next log in this format is as follows:
10:12:51.012 [threadName] DEBUG o.c.d.r.util.LoggingResponseFilter
RollingFileAppender
Another common log is output to a file. As the application runs longer and longer, logs will grow more and more. It is not a good way to output logs to the same file. RollingFileAppender is used to split file logs:
/data/log/app.log
rest-demo.%d{yyyy-MM-dd}.log
30
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n
What is important is the definition of rollingPolicy. In the previous example, rest-demo is used. % d {yyyy-MM-dd }. log defines the log splitting method-archive the logs of each day to a file. 30 indicates that only logs of the last 30 days are retained to prevent logs from filling up the entire disk space. Similarly, you can use % d {yyyy-MM-dd_HH-mm} to define a log splitting method that is accurate to minutes.
Sentry
Sentry is a unified log tracking platform. In traditional log management, logs are viewed on servers using tools such as tail and vim, and different log locations are also different, sentry collects these logs (mainly error logs) through a unified interface and provides tracking and management functions, so that application errors and bugs can be solved instantly.
Sentry provides the Java library-Raven Java. Java applications can send exceptions to the Sentry server after capturing exceptions. On the other hand, it supports various log frameworks. Taking Logbakc as an example:
net.kencochrane.raven
raven-logback
6.0.0
Define appender in logback. xml:
https://publicKey:secretKey@host:port/1?options
tag1:value1,tag2:value2
We recommend that you add logs for ERROR-level filtering.
Summary
Only two steps are required to record logs in Spring Boot:
1. Create the logback. xml file under src/main/resources and configure it as described above.
Or use the simplest method to configure in the application configuration file.
2. Create an instance in Java code and use it where logs need to be output.
// Create a logger instance private static final Logger logger = LoggerFactory in the Java class. getLogger (SpringBootSampleApplication. class); // use log output in the method, such as public void logTest () {logger. debug ("log output test Debug"); logger. trace ("log output test Trace"); logger.info ("log output test Info ");}