In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly analyzes the relevant knowledge points of how to use the log framework Logback in the Java interface test, the content is detailed and easy to understand, the operation details are reasonable, and has a certain reference value. If you are interested, you might as well follow the editor and learn more about how to use the logging framework Logback in Java interface testing.
I. introduction
Log management is essential for a mature interface testing framework. In the development and debugging phase, the log can help us to locate the problem more quickly, and in the process of operation and maintenance of the test, the log system can help us to record most of the abnormal information.
II. Preface
Spring Boot uses Commons Logging in all internal logs, but the default configuration also provides support for common logs, such as Java Util Logging,Log4J, Log4J2, and Logback. Each Logger can be configured to use the console or file to output the log contents.
III. The relationship between LogBack, Slf4j and Log4j
Slf4j is the abbreviation of The Simple Logging Facade for Java, is a simple log facade abstract framework, it only provides log Facade API and a simple log class implementation, usually used with Log4j,LogBack,java.util.logging. When Slf4j is connected as the Log of the application layer, the program can dynamically adjust the underlying log implementation framework (Log4j/LogBack/JdkLog …) according to the actual application scenario. .
LogBack and Log4j are both open source diary tool libraries. LogBack is an improved version of Log4j. It has more features than Log4j, brings great performance improvements, and naturally supports SLF4J.
LogBack officially recommends that it be used in conjunction with Slf4j, so that you can flexibly replace the underlying logging framework.
TIPS: in order to optimize log4j and improve performance, the Apache Foundation has begun to develop log4j 2.0, which also draws on and absorbs some advanced features of logback.
4. Default log Logback
By default, Spring Boot uses Logback to log and output to the console at the INFO level. You should have seen a lot of INFO-level logs when running applications and other examples.
As you can see from the above figure, the log output elements are as follows:
Time date: accurate to milliseconds
Log level: ERROR, WARN, INFO, DEBUG or TRACE
Logger name: the class name that usually uses the source code
Delimiter:-identifies the beginning of the actual log
Process ID
Thread name: enclosed in square brackets (may truncate console output)
Log content
5. Detailed explanation of configuration 1. Add log dependency
If spring-boot-starter-logging is added to the maven dependency:
Org.springframework.boot spring-boot-starter-logging
Then, our Spring Boot application will automatically use logback as the application logging framework, and when Spring Boot starts, it will be initialized and used by org.springframework.boot.logging.Logging-Application-Listener according to the situation.
However, in actual development, we do not need to add this dependency directly. You will find that spring-boot-starter contains spring-boot-starter-logging, which is Spring Boot's default logging framework, logback.
2. Configuration file
Spring Boot officially recommends that you use a file name with-spring as your log configuration (such as using logback-spring.xml instead of logback.xml) and a log configuration file named logback-spring.xml, for which spring boot can add some spring boot-specific configuration items (mentioned below). And put it under src/main/resources.
Profile logback-spring.xml
${LOG_MSG} ${LOG_HOME} / all_$ {LOG_PREFIX} .log ${LOG_DIR} / all_$ {LOG_PREFIX}% i.log ${MAX_HISTORY} ${MAX_FILE_SIZE} ${LOG_MSG} ERROR DENY ACCEPT ${LOG_HOME} / err_$ {LOG_PREFIX} .log ${LOG_DIR} / err_$ {LOG_PREFIX}% i.log ${MAX_HISTORY} ${MAX_FILE_SIZE} ${LOG_MSG}
Profile application.yml
Server: port: 8888 # Port number logging: path:. / logs/zuozewei level: root: info # Log level is TRACE from low to high
< DEBUG < INFO < WARN < ERROR < FATAL,如果设置为WARN,则低于WARN的信息都不会输出 日志会每天新建一个文件夹,日文文件配置的每50兆,一个文本文件,超过新写入一个 文件夹:20181228 文件夹内容:all_spring-boot-logback0.log 文件夹内容:all_spring-boot-logback1.log 文件夹内容:all_spring-boot-logback2.log 文件夹内容:err_spring-boot-logback0.log 六、多环境日志输出 根据不同环境(prod:生产环境,test:测试环境,dev:开发环境)来定义不同的日志输出,在 logback-spring.xml中使用 springProfile 节点来定义,方法如下: 文件名称不是 logback.xml,想使用spring扩展profile支持,要以logback-spring.xml 命名 可以启动服务的时候指定 profile (如不指定使用默认),如指定prod 的方式为: java -jar xxx.jar -spring.profiles.active=prod七、单元测试 此处我选择使用lombok效率插件,所以只需要@Slf4j注解即可简化private Logger logger = LoggerFactory.getLogger(this.getClass())的写法 RunWith(SpringRunner.class) @SpringBootTest@Slf4jpublic class LogbackdemoApplicationTests { @Test public void contextLoads() { log.info("输出info"); log.debug("输出debug"); log.error("输出error"); }} 生成的日志: - | [] | [20181228 22:53:20.756] | [INFO] | [192.168.1.18] | [main] | [c.z.l.LogbackdemoApplicationTests] | -->Starting LogbackdemoApplicationTests on 192.168.1.18 with PID 82507 (started by apple in / Users/apple/Downloads/Springboot-logback-demo) |
| | [] | [20181228 22 c.z.l.LogbackdemoApplicationTests 53 c.z.l.LogbackdemoApplicationTests 20.762] | [INFO] | [192.168.1.18] | [main] | [c.z.l.LogbackdemoApplicationTests] |-- > No active profile set, falling back to default profiles: default |
-| [] | [20181228 22 c.z.l.LogbackdemoApplicationTests 53 seconds 21.590] | [INFO] | [192.168.1.18] | [main] | [c.z.l.LogbackdemoApplicationTests] |-- > Started LogbackdemoApplicationTests in 1.69 seconds (JVM running for 3.525) |
-| [] | [20181228 22 c.z.l.LogbackdemoApplicationTests 53 c.z.l.LogbackdemoApplicationTests 21.955] | [INFO] | [192.168.1.18] | [main] | [c.z.l.LogbackdemoApplicationTests] |-- > output info |
-| [] | [20181228 22 c.z.l.LogbackdemoApplicationTests 53 c.z.l.LogbackdemoApplicationTests 21.955] | [ERROR] | [192.168.1.18] | [main] | [c.z.l.LogbackdemoApplicationTests] |-- > output error |
VIII. Project catalogue
IX. Summary
Finally, the Logback logging framework is introduced. It is recommended to use custom logback-spring.xml when you use it. It is also very simple to use logs in the code. Add private Logger logger = LoggerFactory.getLogger (this.getClass ()) to the class. You can use @ Slf4j annotation if you use the lombok efficiency plug-in.
What are the characteristics of Java? what are the characteristics of Java? what is the 1.Java language that represents the static object-oriented programming language? it implements the object-oriented theory and allows programmers to carry out complex programming in an elegant way of thinking. 2.Java has the characteristics of simplicity, object-oriented, distributed, security, platform independence and portability, dynamic and so on. 3. Using Java, you can write desktop applications, Web applications, distributed systems, embedded system applications, and so on.
This is the end of the introduction on "how to use the log framework Logback in the Java interface test". More related content can be searched for previous articles, hoping to help you answer questions and questions, please support the website!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.