44import com .netease .lowcode .core .EnvironmentType ;
55import com .netease .lowcode .core .annotation .Environment ;
66import com .netease .lowcode .core .annotation .NaslConfiguration ;
7+ import com .wgx .logics .DataWriter ;
78import org .aspectj .lang .ProceedingJoinPoint ;
89import org .aspectj .lang .annotation .Around ;
910import org .aspectj .lang .annotation .Aspect ;
1011import org .aspectj .lang .annotation .Pointcut ;
1112import org .slf4j .Logger ;
1213import org .slf4j .LoggerFactory ;
14+ import org .springframework .beans .factory .NoSuchBeanDefinitionException ;
15+ import org .springframework .beans .factory .annotation .Autowired ;
1316import org .springframework .beans .factory .annotation .Value ;
17+ import org .springframework .context .ApplicationContext ;
1418import org .springframework .core .annotation .Order ;
1519import org .springframework .stereotype .Component ;
1620import org .springframework .web .context .request .RequestContextHolder ;
1721import org .springframework .web .context .request .ServletRequestAttributes ;
22+ import org .springframework .web .multipart .MultipartFile ;
1823
1924import javax .servlet .http .HttpServletRequest ;
25+ import javax .servlet .http .HttpServletResponse ;
26+ import java .lang .reflect .Method ;
27+ import java .text .MessageFormat ;
28+ import java .util .ArrayList ;
29+ import java .util .Arrays ;
30+ import java .util .List ;
31+ import java .util .Objects ;
32+ import java .util .stream .Collectors ;
2033
2134
2235@ Aspect
@@ -52,6 +65,9 @@ public class LoggingAspect {
5265 })
5366 private String loggingClassNames ;
5467
68+ @ Autowired
69+ private ApplicationContext applicationContext ;
70+
5571 //切点匹配类上有@Controller、@RestController注解的类下的所有方法
5672 @ Pointcut ("within(@org.springframework.stereotype.Controller *) || within(@org.springframework.web.bind.annotation.RestController *)" )
5773 public void controller () {}
@@ -62,6 +78,15 @@ public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
6278 return joinPoint .proceed ();
6379 }
6480
81+ Object recordService = null ;
82+ Method saveLog = null ;
83+ try {
84+ recordService = applicationContext .getBean ("saveLogOverriddenRecord_operationCustomizeService" );
85+ saveLog = recordService .getClass ().getMethod ("saveLogOverriddenRecord_operation" , String .class );
86+ } catch (NoSuchBeanDefinitionException e ) {
87+ // do nothing
88+ }
89+
6590 long startTime = System .currentTimeMillis ();
6691
6792 // Ensure the current request attributes are available.
@@ -80,35 +105,59 @@ public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
80105 } catch (Throwable ex ) {
81106 exceptionThrown = true ;
82107 if ("error" .equals (loggingFormat )) {
108+ String msg = MessageFormat .format ("Error in {0}#{1} - IP: {2} - URL: {3} - Args: {4} - Exception: {5}" ,
109+ joinPoint .getSignature ().getDeclaringTypeName (),
110+ joinPoint .getSignature ().getName (),
111+ requestIP ,
112+ requestURL ,
113+ serializeArgs (args ),
114+ ex .toString ());
83115 logger .error ("Error in {}#{} - IP: {} - URL: {} - Args: {} - Exception: {}" ,
84116 joinPoint .getSignature ().getDeclaringTypeName (),
85117 joinPoint .getSignature ().getName (),
86118 requestIP ,
87119 requestURL ,
88- serializeObject (args ),
120+ serializeArgs (args ),
89121 ex .toString ());
90122 logger .error ("Exception stack trace:" , ex );
123+ DataWriter .invoke (recordService , saveLog , msg );
91124 }
92125 throw ex ; // rethrow the exception
93126 } finally {
94127 long executionTime = System .currentTimeMillis () - startTime ;
95128 if (!exceptionThrown ) {
96129 if ("simple" .equals (loggingFormat )) {
130+ String msg = MessageFormat .format ("Completed {0}#{} - IP: {1} - URL: {2} - Duration: {3} ms" ,
131+ joinPoint .getSignature ().getDeclaringTypeName (),
132+ joinPoint .getSignature ().getName (),
133+ requestIP ,
134+ requestURL ,
135+ executionTime );
97136 logger .info ("Completed {}#{} - IP: {} - URL: {} - Duration: {} ms" ,
98137 joinPoint .getSignature ().getDeclaringTypeName (),
99138 joinPoint .getSignature ().getName (),
100139 requestIP ,
101140 requestURL ,
102141 executionTime );
142+ DataWriter .invoke (recordService , saveLog , msg );
103143 }else if ("detailed" .equals (loggingFormat )) {
144+ String msg = MessageFormat .format ("Completed {0}#{1} - IP: {2} - URL: {3} - Args: {4} - Result: {5} - Duration: {6} ms" ,
145+ joinPoint .getSignature ().getDeclaringTypeName (),
146+ joinPoint .getSignature ().getName (),
147+ requestIP ,
148+ requestURL ,
149+ serializeArgs (args ),
150+ serializeObject (result ),
151+ executionTime );
104152 logger .info ("Completed {}#{} - IP: {} - URL: {} - Args: {} - Result: {} - Duration: {} ms" ,
105153 joinPoint .getSignature ().getDeclaringTypeName (),
106154 joinPoint .getSignature ().getName (),
107155 requestIP ,
108156 requestURL ,
109- serializeObject (args ),
157+ serializeArgs (args ),
110158 serializeObject (result ),
111159 executionTime );
160+ DataWriter .invoke (recordService , saveLog , msg );
112161 }
113162 }
114163 }
@@ -141,6 +190,27 @@ private boolean shouldLog(ProceedingJoinPoint joinPoint) {
141190 return true ;
142191 }
143192
193+ private String serializeArgs (Object [] args ) {
194+ if (Objects .nonNull (args )) {
195+ List <Object > objList = new ArrayList <>();
196+ Arrays .asList (args ).forEach (obj -> {
197+ if (obj instanceof HttpServletRequest || obj instanceof HttpServletResponse ) {
198+ return ;
199+ }
200+ if (obj instanceof List ) {
201+ List <?> argsObjList = (List <?>) obj ;
202+ if (Objects .nonNull (argsObjList ) && !argsObjList .isEmpty () && argsObjList .get (0 ) instanceof MultipartFile ) {
203+ return ;
204+ }
205+ }
206+ objList .add (obj );
207+ });
208+
209+ return JSON .toJSONString (objList .toArray ());
210+ }
211+ return JSON .toJSONString (args );
212+ }
213+
144214 private String serializeObject (Object obj ) {
145215 try {
146216 return JSON .toJSONString (obj );
0 commit comments