Commit 82e541cfb5d144ed72d55c540f88912897986bc8
1 parent
e3df6564
Exists in
develop
1、使用MDC的方式结合logback记录日志
Showing
5 changed files
with
89 additions
and
2 deletions
Show diff stats
src/main/java/com/example/mina/Application.java
... | ... | @@ -64,11 +64,11 @@ public class Application { |
64 | 64 | MatrixClient client = clientManager.getOrCreateClient(connectConfig); |
65 | 65 | |
66 | 66 | |
67 | -// client.setAttenuation(1,2,3, null); | |
67 | + client.setAttenuation(1,2,3, null); | |
68 | 68 | // LockSupport.parkNanos(1000_000_000); |
69 | 69 | |
70 | 70 | // client.setOffset(1, 2); |
71 | - client.getOffset(2); | |
71 | +// client.getOffset(2); | |
72 | 72 | LockSupport.parkNanos(1000_000_000); |
73 | 73 | |
74 | 74 | // client.setOffset(1,2); | ... | ... |
src/main/java/com/example/mina/client/box/qrb3000/Qrb3000ProtocolFactory.java
... | ... | @@ -3,6 +3,7 @@ package com.example.mina.client.box.qrb3000; |
3 | 3 | import com.example.mina.client.base.MatrixCommand; |
4 | 4 | import com.example.mina.client.base.MatrixConstants; |
5 | 5 | import com.example.mina.client.base.MatrixResponse; |
6 | +import com.example.mina.processor.VbLogUtils; | |
6 | 7 | import com.example.mina.server.util.CommandHelper; |
7 | 8 | import lombok.extern.slf4j.Slf4j; |
8 | 9 | import org.apache.mina.core.buffer.IoBuffer; |
... | ... | @@ -52,6 +53,7 @@ public class Qrb3000ProtocolFactory implements ProtocolCodecFactory { |
52 | 53 | mc.getType() |
53 | 54 | ); |
54 | 55 | int cmd = mc.getCommand(); |
56 | + VbLogUtils.infoLog(log, "", mc.getMatrixId(),msg); | |
55 | 57 | |
56 | 58 | IoBuffer buffer = IoBuffer.allocate(100, false).setAutoExpand(true); |
57 | 59 | ... | ... |
src/main/java/com/example/mina/processor/LogBeanPostProcessor.java
0 → 100644
... | ... | @@ -0,0 +1,30 @@ |
1 | +package com.example.mina.processor; | |
2 | + | |
3 | +import lombok.extern.slf4j.Slf4j; | |
4 | +import org.slf4j.MDC; | |
5 | +import org.slf4j.Marker; | |
6 | +import org.slf4j.MarkerFactory; | |
7 | +import org.springframework.beans.BeansException; | |
8 | +import org.springframework.beans.factory.config.BeanPostProcessor; | |
9 | +import org.springframework.stereotype.Component; | |
10 | + | |
11 | +@Slf4j | |
12 | +@Component | |
13 | +public class LogBeanPostProcessor implements BeanPostProcessor { | |
14 | + | |
15 | + @Override | |
16 | + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { | |
17 | +// log.info("postProcessBeforeInitialization: {}", beanName); | |
18 | + return bean; | |
19 | + } | |
20 | + | |
21 | + @Override | |
22 | + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { | |
23 | +// Marker marker = MarkerFactory.getMarker("postProcessAfterInitialization"); | |
24 | +// MDC.put("taskId", "QRB3000/100_20210402"); | |
25 | +// log.info(marker, "postProcessAfterInitialization: {}", beanName); | |
26 | +// MDC.clear(); | |
27 | + return bean; | |
28 | + } | |
29 | + | |
30 | +} | ... | ... |
src/main/java/com/example/mina/processor/VbLogUtils.java
0 → 100644
... | ... | @@ -0,0 +1,30 @@ |
1 | +package com.example.mina.processor; | |
2 | + | |
3 | +import org.slf4j.Logger; | |
4 | +import org.slf4j.MDC; | |
5 | +import org.slf4j.Marker; | |
6 | +import org.slf4j.MarkerFactory; | |
7 | + | |
8 | +import java.time.LocalDate; | |
9 | +import java.time.format.DateTimeFormatter; | |
10 | + | |
11 | +/** | |
12 | + * @ProjectName: demo | |
13 | + * @Package: com.example.mina.processor | |
14 | + * @ClassName: VbLogUtils | |
15 | + * @Author: HuangLang | |
16 | + * @Description: 用于记录设备日志 | |
17 | + * @Date: 2021-04-01 下午 8:46 | |
18 | + */ | |
19 | +public class VbLogUtils { | |
20 | + public static void infoLog(Logger log, String matrixType, String matrixId, Object msg){ | |
21 | + Marker marker = MarkerFactory.getMarker("postProcessAfterInitialization"); | |
22 | + matrixType = "QRB1234"; | |
23 | + matrixId = "100"; | |
24 | + MDC.put("taskId", | |
25 | + matrixType + "/" + matrixId + "_" + LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))); | |
26 | + log.info("info msg, msg is: {}", msg); | |
27 | + MDC.clear(); | |
28 | + } | |
29 | + | |
30 | +} | ... | ... |
... | ... | @@ -0,0 +1,25 @@ |
1 | +<configuration> | |
2 | + | |
3 | + <appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender"> | |
4 | + <!-- in the absence of the class attribute, it is assumed that the | |
5 | + desired discriminator type is | |
6 | + ch.qos.logback.classic.sift.MDCBasedDiscriminator --> | |
7 | + <discriminator> | |
8 | + <key>taskId</key> | |
9 | + <defaultValue>log</defaultValue> | |
10 | + </discriminator> | |
11 | + <sift> | |
12 | + <appender name="FILE-${taskId}" class="ch.qos.logback.core.FileAppender"> | |
13 | + <file>log/${taskId}.log</file> | |
14 | + <append>false</append> | |
15 | + <layout class="ch.qos.logback.classic.PatternLayout"> | |
16 | + <pattern>%d %marker [%thread] %level %mdc %logger{35} - %msg%n</pattern> | |
17 | + </layout> | |
18 | + </appender> | |
19 | + </sift> | |
20 | + </appender> | |
21 | + | |
22 | + <root level="DEBUG"> | |
23 | + <appender-ref ref="SIFT" /> | |
24 | + </root> | |
25 | +</configuration> | |
0 | 26 | \ No newline at end of file | ... | ... |