【问题标题】:Log4j appender on WildflyWildfly 上的 Log4j 附加程序
【发布时间】:2018-08-16 15:12:42
【问题描述】:

我正在 Wildfly 10.1 Final 上运行 Java EE 项目。

我想制作一个自定义 Log4j 附加程序,用于记录到 JMS 队列。 我已经在我的 Java EE 应用程序中创建了一个 Message Driven Bean,它接收来自队列的日志消息。

我通过添加此注释创建了我的 MDB:

@MessageDriven(activationConfig = {
         @ActivationConfigProperty(propertyName = "destinationType", 
         propertyValue = "javax.jms.Queue"),
         @ActivationConfigProperty(propertyName = "destination", 
         propertyValue = "queue/MyQueue")
        })

这个 bean 还实现了 MessageListener 接口,并覆盖了它的 onMessage() 方法,我在该方法中定义了日志消息到达时所需的行为。它看起来像这样:

public class myBean implements MessageListener {

    @Override
    public void onMessage(Message msg) {

                        if(msg instanceof TextMessage){

                            TextMessage txtMess = (TextMessage)msg;
                            String context = (String)txtMess.getText();
                            processLogData(context);
    }
}

我还需要将 JMS 添加到 Wildfly 的 standalone.xml,方法是从 standalone-full.xml 复制所有与 JMS 相关的行。我还需要将我的队列添加到 standalone.xml,方法是将这一行添加到

<jms-queue name="SequenceQueue" entries="java:/jms/queue/MyQueue"/>

现在,我想使用 apache 提供的JMSQueueAppender 类,将日志消息发送到 MyQueue。

我不确定,我应该怎么做。我应该把JMSQueueApender 类放在哪里?我应该为 queueConnectionFactoryBindingNamequeueBindingName 以及 initialContextFactoryproviderURL 提供什么值?

我读到了这个SO 线程,我应该把log4j.properties 文件放在我的EAR/META-INF 中。我还在this SO 线程中阅读了 log4j.properties 的外观。

不过,我不确定需要向 JMSQueueApender 类提供哪些值,以及应该将其放在哪里(如何初始化)。

由于我的项目在 Application Server(Java EE 环境)中运行,并且我的队列是该容器的一部分,我可以编写一个更简单的 JMSQueuApender 类吗?像这样的:

@Singleton
@Startup
public class JMSQueueAppender extends AppenderSkeleton {

    private Logger log = LoggerFactory.getLogger(LogSomethingBean.class);

    @Inject
    private JMSContext context;

    @Resource(mappedName = "java:/jms/queue/MyQueue")
    private Queue queue;

    public void append(LoggingEvent event) {
        try {
            TextMessage msg = queueSession.createTextMessage();

            String str = (String)event.getMessage();
            msg.setText(str);
            context.createProducer().send(queue, msg);
        } catch (JMSException e) {
            log.error("An error has occured while trying to send a msg to queue", e);
        }
    }

}

编辑:是否有可能为整个 Wildfly 实例定义一个 log4j.properties 文件,以便在其上运行的每个项目都读取该配置?

【问题讨论】:

    标签: java jakarta-ee log4j jms wildfly


    【解决方案1】:

    我找到了在 Wildfly 10.1 Final 上使用此附加程序的方法。

    我按原样使用JMSQueueApender 类。

    我将 log4j.properties 文件添加到 ear/application/META-INF(我使用的是 Maven)。

    我的 log4j.properties 看起来像这样:

    log4j.rootLogger=INFO, stdout, jms
    
    ## Be sure that ActiveMQ messages are not logged to 'jms' appender
    log4j.logger.org.apache.activemq=INFO, stdout
    
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d %-5p %c - %m%n
    
    ## Configure 'jms' appender
    log4j.appender.jms=rs.netset.authority.log4j.JMSQueueAppender
    log4j.appender.jms.initialContextFactory=org.apache.activemq.jndi.ActiveMQInitialContextFactory
    log4j.appender.jms.providerUrl=tcp://localhost:61616
    log4j.appender.jms.queueBindingName=java:/jms/queue/MyQueue
    log4j.appender.jms.queueConnectionFactoryBindingName=ConnectionFactory
    

    而且,一切正常

    【讨论】:

      猜你喜欢
      • 2021-01-22
      • 1970-01-01
      • 1970-01-01
      • 2012-06-24
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多