Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the printing and output mode of SpringBoot log

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the SpringBoot log printing and output mode". In the daily operation, I believe that many people have doubts about the SpringBoot log printing and output mode. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "what is the SpringBoot log printing and output mode?" Next, please follow the editor to study!

SpringBoot log print and output Import jar package import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory; Code sample private final Log log = LogFactory.getLog (AppController.class); / / AppController.class is the current class file, and the background will output the logString input = "% CN%" of this class file; this.log.info (input)

The output log of the background is as follows:

Log output configuration

Add a configuration to the application.yml file

Logging: path: d:\ spring-boot-log / / Custom address of output log springboot section technology printing log log

Some functions of the project need to add the operation record of the log, so it is more convenient to use the section technology after thinking about it. It is equivalent to an agent, and it feels more powerful than an agent.

Say the key words you understand.

@ Aspect: make the current class into an aspect class, providing container reading

Pointcut: define the pointcut, scan the class under that package, and specify that method.

@ Around: surround enhancement, equivalent to MethodInterceptor

@ AfterReturning: post enhancement, equivalent to AfterReturningAdvice, which is executed when the method exits normally

Before: identifies a pre-enhancement method, which is equivalent to the function of BeforeAdvice, as well as similar features

@ AfterThrowing: exception throwing enhancement, equivalent to ThrowsAdvice

@ After:final enhancement, whether it throws an exception or exits normally

Mvaen is going to have org.slf4j slf4j-log4j12 1.7.25 of the spring family to create the WebLogAcpect class @ Aspect@Componentpublic class WebLogAcpect {private final static Logger orderLog = LoggerFactory.getLogger ("order"); / / define the pointcut @ Pointcut ("execution (* com.qszhuang.backstage.service.OrderService.updateOrder* (..)")) Public void webLog () {} @ Before ("webLog ()") public void doBefore (JoinPoint joinPoint) {/ / receives the request and records the request content ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes (); if (! StringUtils.isEmpty (attributes)) {HttpServletRequest request = attributes.getRequest () / / record the request content orderLog.info ("URL:" + request.getRequestURL (). ToString ()); / / orderLog.info ("HTTP_METHOD:" + request.getMethod ()); orderLog.info ("IP:" + HttpUtil.getIp (request)) / / get operator if (! StringUtils.isEmpty (request.getCookies () {String cookieValueByName = HttpUtil.getCookieValueByName (request.getCookies (), WebSecurityConfig.TOKEN_HEADER); Map stringClaimMap = TokenUtil.verifyToken (cookieValueByName) / method name / / orderLog.info ("CLASS_METHOD:" + joinPoint.getSignature (). GetDeclaringTypeName () + ". + joinPoint.getSignature (). GetName ()); orderLog.info (" order number: {}, operator: {} ", Arrays.toString (joinPoint.getArgs ()), stringClaimMap.get (" adminUsername ") .asString ()) }} else {orderLog.info ("prompt refund");}} @ AfterReturning (returning = "ret", pointcut = "webLog ()") public void doAfterReturning (Object ret) throws Throwable {/ / after processing the request, return content orderLog.info ("Operation return value:" + ret);}} configure log4j

Create a file log4j.properties print order logs to a specified directory of course you can configure multiple logs to be saved separately

Log4j.rootLogger=INFO Stdoutlog4j.appender.stdout = org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target = System.outlog4j.appender.stdout.layout = org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern =% d {ABSOLUTE}% 5p% c {1}:% L -% m%n#log4j.appender.timer_log=org.apache.log4j.DailyRollingFileAppender#log4j.appender.timer_log.Append=true#log4j.appender.timer_log.DatePattern='.'yyyy-MM-dd#log4j.appender.timer_log. File=E:/logs/timer/timer.log#log4j.appender.timer_log.Threshold=INFO#log4j.appender.timer_log.layout=org.apache.log4j.PatternLayout#log4j.appender.timer_log.layout.ConversionPattern=%d -% c [% t]%-5p% c% x% l -% m%nlog4j.logger.order=INFO Order_loglog4j.appender.order_log=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.order_log.Append=truelog4j.appender.order_log.DatePattern='.'yyyy-MM-ddlog4j.appender.order_log.File=../logs/order/order.loglog4j.appender.order_log.Threshold=INFOlog4j.appender.order_log.layout=org.apache.log4j.PatternLayoutlog4j.appender.order_log.layout.ConversionPattern= [% p] [% d {yyyy-MM-dd HH:mm:ss}]% m% n so far The study on "what is the printing and output mode of SpringBoot log" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report