Skip to content

Commit 91d6593

Browse files
author
jiazhifeng
committed
add:添加日志框架
1 parent 1da08e3 commit 91d6593

File tree

4 files changed

+56
-11
lines changed

4 files changed

+56
-11
lines changed

remote-core/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
</distributionManagement>
3030

3131
<dependencies>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-logging</artifactId>
35+
</dependency>
3236
<!-- https://mvnrepository.com/artifact/org.ow2.asm/asm -->
3337
<dependency>
3438
<groupId>org.ow2.asm</groupId>

remote-core/src/main/java/com/jzf/remote/core/DynamicCompiler.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.jzf.remote.core;
22

3-
import javax.tools.*;
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
45

6+
import javax.tools.*;
57
import java.io.IOException;
68
import java.nio.charset.StandardCharsets;
79
import java.util.Arrays;
@@ -14,6 +16,7 @@
1416
* @date 2021/11/25 14:53
1517
*/
1618
public class DynamicCompiler {
19+
private static final Logger LOG = LoggerFactory.getLogger(DynamicCompiler.class);
1720
private final JavaCompiler javaCompiler;
1821
private final DiagnosticCollector<JavaFileObject> diagnosticCollector;
1922
private final ClassFileManager fileManager;
@@ -41,15 +44,15 @@ public Class<?> compileAndLoad(String className, String sourceCode) throws Class
4144
Arrays.asList(fileObject));
4245
boolean result = task.call();
4346
diagnosticCollector.getDiagnostics().forEach(diagnostic -> {
44-
System.out.println("msg:" + diagnostic.getMessage(Locale.getDefault()));
45-
System.out.println("source:" + diagnostic.getSource() + " line:" + diagnostic.getLineNumber());
47+
LOG.info("msg:{}", diagnostic.getMessage(Locale.getDefault()));
48+
LOG.info("source:{} line:{}", diagnostic.getSource(), diagnostic.getLineNumber());
4649
});
4750
if (result) {
48-
System.out.println("Compiler Succeeded");
51+
LOG.info("Compiler Succeeded");
4952
HotSwapClassLoader hotSwapClassLoader = new HotSwapClassLoader(fileManager.getBytes());
5053
return hotSwapClassLoader.loadClass(className);
5154
} else {
52-
System.out.println("Compiler failure!");
55+
LOG.info("Compiler failure!");
5356
return null;
5457
}
5558
}

remote-core/src/main/java/com/jzf/remote/core/HotSwapClassLoader.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
package com.jzf.remote.core;
22

3+
import com.jzf.remote.core.util.Constants;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
37
import java.io.FileOutputStream;
48
import java.io.IOException;
59
import java.time.LocalDateTime;
610
import java.time.format.DateTimeFormatter;
711

8-
import com.jzf.remote.core.util.Constants;
9-
1012
/**
1113
* @author jiazhifeng
1214
* @date 2021/11/25 16:29
1315
*/
1416
public class HotSwapClassLoader extends ClassLoader {
17+
private static final Logger LOG = LoggerFactory.getLogger(HotSwapClassLoader.class);
1518
private byte[] byteCode;
1619

1720
public HotSwapClassLoader(byte[] byteCode) {
@@ -27,13 +30,13 @@ protected Class<?> findClass(String name) {
2730

2831
String userDir = System.getProperty("user.dir");
2932
String nameSuffix = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"));
30-
String path = userDir + "/../dynamic_compiler_files/" + name + "_"+ nameSuffix + ".class";
31-
System.out.println("动态编译存储路径: " + path);
33+
String path = userDir + "/../dynamic_compiler_files/" + name + "_" + nameSuffix + ".class";
34+
LOG.info("动态编译存储路径:{}", path);
3235
try (FileOutputStream fos = new FileOutputStream(path)) {
3336
fos.write(modiBytes);
3437
} catch (IOException e) {
35-
System.out.println(e.getMessage());
38+
LOG.error(e.getMessage());
3639
}
37-
return super.defineClass(name.replaceAll("/","."), modiBytes, 0, modiBytes.length);
40+
return super.defineClass(name.replaceAll("/", "."), modiBytes, 0, modiBytes.length);
3841
}
3942
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration scan="true" scanPeriod="10 seconds">
3+
<contextName>logback</contextName>
4+
<property name="FILE_NAME" value="../logs/remote-web-%d.log"/>
5+
<property name="LOG_PATTERN"
6+
value="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%30.30thread] %-20.20logger{20}:%-4.4line --->>> %msg%n"/>
7+
8+
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
9+
<encoder>
10+
<Pattern>${LOG_PATTERN}</Pattern>
11+
<charset>UTF-8</charset>
12+
</encoder>
13+
</appender>
14+
15+
<!--文件日志, 按照每天生成日志文件 -->
16+
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
17+
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
18+
<FileNamePattern>${FILE_NAME}</FileNamePattern>
19+
<MaxHistory>7</MaxHistory>
20+
<CleanHistoryOnStart>true</CleanHistoryOnStart>
21+
</rollingPolicy>
22+
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
23+
<MaxFileSize>100MB</MaxFileSize>
24+
</triggeringPolicy>
25+
<encoder>
26+
<Pattern>${LOG_PATTERN}</Pattern>
27+
<charset>UTF-8</charset>
28+
</encoder>
29+
</appender>
30+
31+
<root level="info">
32+
<appender-ref ref="CONSOLE"/>
33+
<appender-ref ref="FILE"/>
34+
</root>
35+
</configuration>

0 commit comments

Comments
 (0)