Under Spring Boot, I tried to use log4j to logstash and add the log4j.properties file in the src/main/resources directory to customize the output log file, but it failed. Configuring logging path in application.yml The log printing was successfully printed, but the log file logging failed to be successfully debugged. I checked the information online and said that Spring Boot uses logback to record logs by default. After many attempts, log4j failed, so he changed to logback to record, and the final test was successful.
1. Do you have any questions about the path of Spring Boot log file?
The logging.path and logging.file properties are configured at the same time, as follows:
logging: path: /var/log file: test.log
Only the test.log file will be generated under the project root path, and the log file will not be generated under the specified path (the expected log path is: logging.path + logging.file).
Reason: In Spring Boot, you only need to configure one of the two properties: logging.path and logging.file. If configured at the same time, use the logging.file attribute.
When the loggin.path property is configured, the spring.log file will be generated in this path, that is, the default log file name spring.log is used at this time.
When the loggin.file property is configured, a log file with the specified name is generated under the specified path. The default is the project relative path, and you can specify an absolute path for logging.file.
logging:
path: /var/logs # Generate spring.log file in /var/logs directory file: /var/logs/test.log # Generate test.log file in /var/logs directory
2. Reasons for logback to replace log4j:
Here are excerpts from the Internet:
Logback and log4j are very similar. If you are familiar with log4j, logback will be very easy to get used to it. The following are some advantages of logback over log4j:
1. The kernel that implements Logback faster has been rewritten, and the performance is improved by more than 10 times on some key execution paths. Moreover, logback not only improves performance, but also loads the initial memory.
2. Very sufficient test Logback After several years, countless hours of testing. Logback's tests are completely different. In the author's point of view, it is simply important to choose logback instead of log4j.
3. Logback-classic implements SLF4j very naturally. Logback-classic implements SLF4j. When using SLF4j, you can't even feel logback-classic. And because logback-classic implements SLF4J very naturally, switching to log4j or other is very easy. It is OK to just provide it into another jar package, and there is no need to touch the code implemented through SLF4JAPI.
4. Very sufficient documents, the official website has more than 200 pages of documents.
5. Automatically reload the configuration file. When the configuration file is modified, Logback-classic can automatically reload the configuration file. The scanning process is fast and safe, and it does not require another scanning thread to be created. This technology fully ensures that the application can run happily in the JEE environment.
6. Lilith, Lilith is the observer of log events, similar to log4j chainsaw. Lilith can also process large amounts of log data.
7. Caution mode and very friendly recovery. In Caution mode, multiple FileAppender instances run under multiple JVMs and can safely write the same log file. RollingFileAppender will have some limitations. Logback's FileAppender and its subclasses, including RollingFileAppender, are able to recover from I/O exceptions very friendly.
8. Configuration files can handle different situations. Developers often need to judge that different Logback configuration files are in different environments (development, testing, production). And these configuration files are only slightly different, and can be implemented through, and so that a configuration file can adapt to multiple environments.
9. Filters, sometimes, need to diagnose a problem and log it. In log4j, the log level can only be reduced, but this will produce a large number of logs and will affect the application performance. In Logback, you can continue to maintain that log level and eliminate some special situations. For example, the user who is logged in, her log will be hit at the DEBUG level and other users can continue to be hit at the WARN level. To implement this function, only 4 lines of XML configuration are required. You can refer to MDCFIlter.
10. SiftingAppender (a very multifunctional Appender) It can be used to split log files based on any given running parameters. For example, SiftingAppender can distinguish log events from follow-up to the user's session, and then each user will have a log file.
11. Automatically compress the log that has been printed. When a new file is generated, the log file that has been printed will be automatically compressed. Compression is an asynchronous process, so even for large log files, applications will not be affected in the compression process.
12. The stack tree has a package version. Logback will bring the package data when it is published.
13. Automatically remove old log files. By setting the maxHistory property of TimeBasedRollingPolicy or SizeAndTimeBasedFNATP, you can control the maximum number of log files that have been generated. If maxHistory 12 is set, those log files that have been over 12 months will be automatically removed.
In short, logback is better than log4j. Let us build all our applications on logback!
3. Introduce logback into the project
Add package dependencies, Maven style:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency> <dependency> <groupId>net.logstash.logback</groupId> <artifactId>logstash-logback-encoder</artifactId> <version>4.11</version> </dependency> <dependency> <groupId>net.logstash.log4j</groupId> <artifactId>jsonevenvent-layout</artifactId> <version>1.7</version> </dependency>
4. Add logback.xml
Add the logback.xml file to the src/main/resources directory, and the system will automatically load the logback.xml configuration.
logback.xml:
<?xml version="1.0" encoding="UTF-8"?><configuration scan="true" scanPeriod="60 seconds" debug="false"> <include resource="org/springframework/boot/logging/logback/base.xml" /> <contextName>logback</contextName> <!-- Log files to specific directory--> <!-- <property name="log.path" value="E://test//logback.log" /> --> <property name="log.path" value="/Users/chang/Desktop/CHLogs/logback.log" /><appender name="stash"> <destination>192.168.220.83:9601</destination> <encoder /></appender><!--Output to console-><appender name="console"> <!-- <filter> <level>ERROR</level> </filter>--> <encoder> <pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder></appender><!--Output to file--><appender name="file"> <file>${log.path}</file> <rollingPolicy> <fileNamePattern>logback.%d{yyyy-MM-dd}.log</fileNamePattern> </rollingPolicy> <encoder> <pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder></appender><root level="info"> <appender-ref ref="stash"/> <appender-ref ref="console" /> <appender-ref ref="file" /></root><!-- logback is a package in java <logger name="com.dudu.controller"/>logback.LogbackDemo: full path to class<logger name="com.dudu.controller.LearnController" level="WARN" addition="false"> <appender-ref ref="console"/></logger> --></configuration>5. Logstash-logback-encoder version issue
In spring-boot, if you use logstash-logback-encoder, you may need to tell maven the specific version number to avoid package dependency conflicts. For example, you can set the dependencies of logback-core, logback-classic, and logback-access like the following:
<properties> <ch.qos.logback.version>1.2.3</ch.qos.logback.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>${ch.qos.logback.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${ch.qos.logback.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-access</artifactId> <version>${ch.qos.logback.version}</version> </dependency> </dependency> </dependencyManagement>6. Logstash service construction
refer to:
1.logstash-logback-encoder
Summarize
The above is the method of using logback, logstash, and ELK to record log files in Spring Boot introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!