Log4j配置具有相同类或包的不同记录器级别

时间:2017-12-19 21:50:00

标签: java logging log4j axis

现在我正在使用AXIS客户端与一些Web服务相结合。我在log4j属性中配置了记录器级别,如下所示:

org.apache.axis.client = INFO

但是,我需要为此客户端配置不同的记录器级别,具体取决于使用AXIS客户端的类或方法。

是否有可能为此目的配置log4j?

1 个答案:

答案 0 :(得分:-1)

记录Axis Faults的解决方案是扩展OnFault方法:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.BasicHandler;

public class SOAPLogHandler extends BasicHandler {

  private static final String AXIS = "AXIS";
  private static final String AXIS_FAULT = "AXIS FAULT";
  private static Logger LOG = LoggerFactory.getLogger(SOAPLogHandler.class);
  private static final long serialVersionUID = 1;

  @Override
  public void invoke(MessageContext msgContext) throws AxisFault {
    logMessage(AXIS, msgContext);
  }

  @Override
  public void onFault(MessageContext msgContext) {
    try {            
        logMessage(AXIS_FAULT,msgContext);
    } catch (AxisFault axisFault) {
        LOG.error("Error on logging messages ",axisFault);
    }
  }

  private void logMessage(String preamble, MessageContext msgContext) throws AxisFault {
    if (msgContext.getResponseMessage() != null && msgContext.getResponseMessage().getSOAPPart() != null) {
        LOG.info("{} Response ={}",preamble ,msgContext.getResponseMessage().getSOAPPartAsString());
        return;
    }

    if (msgContext.getRequestMessage() != null && msgContext.getRequestMessage().getSOAPPartAsString() != null) {
        LOG.info("{} Request={}",preamble,msgContext.getRequestMessage().getSOAPPartAsString());
    }
  }
}

在全局配置请求流中设置处理程序也是必须的,在wsdd文件中将是这样的:

<globalConfiguration>     
  <requestFlow>
    <handler type="log" />
  </requestFlow>
  <responseFlow>
    <handler type="log" />
  </responseFlow>
</globalConfiguration>

<handler name="log" type="java:your.package.SOAPLogHandler"/>