log4j2.xml
<?xml version="1.0" encoding="UTF-8"?> <Configuration> <Appenders> <CONSOLE name="CONSOLE"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{requestId}] %-5level | %C.%M:%L - %m%n"/> </CONSOLE> </Appenders> <Loggers> <Root level="ERROR"> <AppenderRef ref="CONSOLE"/> </Root> <logger name="com.ebc.web" level="INFO"> <AppenderRef ref="CONSOLE"/> </logger> <logger name="com.ebc.disruptor" level="INFO"> <AppenderRef ref="CONSOLE"/> </logger> </Loggers> </Configuration>
测试输出
2019-01-25 09:58:26.861 [5c4a6d42cbb0e727aad9a7a0] INFO | com.ebc.web.IndexController.index:25 - 接收参数:name=遥远2,minernum=222 2019-01-25 09:58:26.861 [5c4a6d42cbb0e727aad9a7a0] INFO | com.ebc.web.IndexController.index:25 - 接收参数:name=遥远2,minernum=222 一月 25, 2019 9:58:26 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@27fa7512: startup date [Fri Jan 25 09:58:26 GMT+08:00 2019]; root of context hierarchy 2019-01-25 09:58:26.909 [5c4a6d42cbb0e727aad9a7a0] INFO | com.ebc.disruptor.UserMinerProducer.publish:16 - 接收:5c4a6d42cbb0e727aad9a7a0,222 2019-01-25 09:58:26.909 [5c4a6d42cbb0e727aad9a7a0] INFO | com.ebc.disruptor.UserMinerProducer.publish:16 - 接收:5c4a6d42cbb0e727aad9a7a0,222 2019-01-25 09:58:26.912 [] INFO | com.ebc.disruptor.UserMinerHandler.onEvent:18 - userName=5c4a6d42cbb0e727aad9a7a0,amount=222
解决:
把以上红色字体:<AppenderRef ref="CONSOLE"/>,去掉即可。
原因:
logger继承Root,而Root中已经写了<AppenderRef ref="CONSOLE"/>,如果在logger中再次写,则重复打印。