11package com .wgx .aop ;
22
3- import com .alibaba .fastjson .JSON ;
3+ import com .fasterxml .jackson .core .JsonProcessingException ;
4+ import com .fasterxml .jackson .databind .ObjectMapper ;
45import com .netease .lowcode .core .EnvironmentType ;
56import com .netease .lowcode .core .annotation .Environment ;
67import com .netease .lowcode .core .annotation .NaslConfiguration ;
2930import java .util .Arrays ;
3031import java .util .List ;
3132import java .util .Objects ;
32- import java .util .stream .Collectors ;
3333
3434
3535@ Aspect
3636@ Component
3737@ Order (1 )
3838public class LoggingAspect {
39-
4039 public static final String CONTROLLER = "Controller" ;
40+ private static final ObjectMapper MAPPER = new ObjectMapper ();
4141 private static final Logger logger = LoggerFactory .getLogger ("LCAP_EXTENSION_LOGGER" );
4242
4343 //true开启打印日志、false关闭打印日志
4444 @ Value ("${loggingEnabled}" )
4545 @ NaslConfiguration (defaultValue = {
46- @ Environment (type = EnvironmentType .DEV ,value = "true" ),
47- @ Environment (type = EnvironmentType .ONLINE ,value = "false" )
46+ @ Environment (type = EnvironmentType .DEV , value = "true" ),
47+ @ Environment (type = EnvironmentType .ONLINE , value = "false" )
4848 })
4949 private boolean loggingEnabled ;
5050
5151 //simple简易格式、detailed详细格式、error异常格式 注意:null或空字符以及其他字符则不会打印日志
5252 @ Value ("${loggingFormat}" )
5353 @ NaslConfiguration (defaultValue = {
54- @ Environment (type = EnvironmentType .DEV ,value = "detailed" ),
55- @ Environment (type = EnvironmentType .ONLINE ,value = "simple" )
54+ @ Environment (type = EnvironmentType .DEV , value = "detailed" ),
55+ @ Environment (type = EnvironmentType .ONLINE , value = "simple" )
5656 })
5757 private String loggingFormat ;
5858
5959 // 需要拦截打印日志的控制类:all表示拦截所有控制类,如果为null或空字符串则不会拦截任何控制类,
6060 // 若需要拦截特定的控制类格式为 例如:logic1,logic2。注意:需要使用英文逗号为分隔符
6161 @ Value ("${loggingClassNames}" )
6262 @ NaslConfiguration (defaultValue = {
63- @ Environment (type = EnvironmentType .DEV ,value = "all" ),
64- @ Environment (type = EnvironmentType .ONLINE ,value = "all" )
63+ @ Environment (type = EnvironmentType .DEV , value = "all" ),
64+ @ Environment (type = EnvironmentType .ONLINE , value = "all" )
6565 })
6666 private String loggingClassNames ;
6767
@@ -70,7 +70,8 @@ public class LoggingAspect {
7070
7171 //切点匹配类上有@Controller、@RestController注解的类下的所有方法
7272 @ Pointcut ("within(@org.springframework.stereotype.Controller *) || within(@org.springframework.web.bind.annotation.RestController *)" )
73- public void controller () {}
73+ public void controller () {
74+ }
7475
7576 @ Around ("controller()" )
7677 public Object logAround (ProceedingJoinPoint joinPoint ) throws Throwable {
@@ -140,7 +141,7 @@ public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
140141 requestURL ,
141142 executionTime );
142143 DataWriter .invoke (recordService , saveLog , msg );
143- }else if ("detailed" .equals (loggingFormat )) {
144+ } else if ("detailed" .equals (loggingFormat )) {
144145 String msg = MessageFormat .format ("Completed {0}#{1} - IP: {2} - URL: {3} - Args: {4} - Result: {5} - Duration: {6} ms" ,
145146 joinPoint .getSignature ().getDeclaringTypeName (),
146147 joinPoint .getSignature ().getName (),
@@ -205,15 +206,24 @@ private String serializeArgs(Object[] args) {
205206 }
206207 objList .add (obj );
207208 });
208-
209- return JSON .toJSONString (objList .toArray ());
209+ try {
210+ return MAPPER .writeValueAsString (objList .toArray ());
211+ } catch (JsonProcessingException e ) {
212+ logger .error ("Error serializing object to JSON" , e );
213+ return "Error" ;
214+ }
215+ }
216+ try {
217+ return MAPPER .writeValueAsString (args );
218+ } catch (JsonProcessingException e ) {
219+ logger .error ("Error serializing object to JSON" , e );
220+ return "Error" ;
210221 }
211- return JSON .toJSONString (args );
212222 }
213223
214224 private String serializeObject (Object obj ) {
215225 try {
216- return JSON . toJSONString (obj );
226+ return MAPPER . writeValueAsString (obj );
217227 } catch (Exception e ) {
218228 logger .error ("Error serializing object to JSON" , e );
219229 return "Error" ;
0 commit comments