最近项目需要将日志输出到日志服务器,服务器有一套SocketServer,现在项目中需要增加MQ输出
jeromq由于资料较少,在不断琢磨下,终于找出成功的配置,配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{1.} - %msg%n" />
</Console>
<JeroMQ name="JeroMQAppender">
<PatternLayout pattern="%msg%n" />
<Property name="endpoint">tcp://localhost:7878</Property>
</JeroMQ>
</appenders>
<loggers>
<logger name="com.dt.server" level="INFO" additivity="true">
<appender-ref ref="Console"/>
</logger>
<logger name="pubLogger" level="INFO" additivity="false">
<appender-ref ref="JeroMQAppender" />
</logger>
<root level="INFO">
<appender-ref ref="Console"/>
</root>
</loggers>
</configuration>
接下来是activeMQ的配置:
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="warn" name="MyApp" packages="">
<appenders>
<JMS name="jmsQueue" destinationBindingName="logQueue"
factoryBindingName="org.apache.activemq.jndi.ActiveMQInitialContextFactory"
providerURL="tcp://localhost:61616"/>
</appenders>
<loggers>
<root level="all">
<appenderRef ref="jmsQueue"/>
</root>
</loggers>
</configuration>
private Logger pubLogger = LogManager.getLogger("pubLogger");//这里的名字是你log4j中配置的logger的name,如果配置到root里,使用无参方法getLogger()便可以
调用你配置的级别输出就好了
jar包版本(我用2.2版本是不行的,用了最新的2.6版本可以)
<dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-1.2-api</artifactId> <version>2.6</version> </dependency>
大功告成