下面列出了怎么用org.apache.logging.log4j.core.ErrorHandler的API类实例代码及写法,或者点击链接到github查看源代码。
protected RocketmqLog4j2Appender(String name, Filter filter, Layout<? extends Serializable> layout,
boolean ignoreExceptions, String nameServerAddress, String producerGroup,
String topic, String tag) {
super(name, filter, layout, ignoreExceptions);
this.producer = producer;
this.topic = topic;
this.tag = tag;
this.nameServerAddress = nameServerAddress;
this.producerGroup = producerGroup;
try {
this.producer = ProducerInstance.getProducerInstance().getInstance(this.nameServerAddress, this.producerGroup);
} catch (Exception e) {
ErrorHandler handler = this.getHandler();
if (handler != null) {
handler.error("Starting RocketmqLog4j2Appender [" + this.getName()
+ "] nameServerAddress:" + nameServerAddress + " group:" + producerGroup + " " + e.getMessage());
}
}
}
/**
* When system exit,this method will be called to close resources
*/
public boolean stop(long timeout, TimeUnit timeUnit) {
this.setStopping();
try {
ProducerInstance.getProducerInstance().removeAndClose(this.nameServerAddress, this.producerGroup);
} catch (Exception e) {
ErrorHandler handler = this.getHandler();
if (handler != null) {
handler.error("Closeing RocketmqLog4j2Appender [" + this.getName()
+ "] nameServerAddress:" + nameServerAddress + " group:" + producerGroup + " " + e.getMessage());
}
}
boolean stopped = super.stop(timeout, timeUnit, false);
this.setStopped();
return stopped;
}
protected RocketmqLog4j2Appender(String name, Filter filter, Layout<? extends Serializable> layout,
boolean ignoreExceptions, String nameServerAddress, String producerGroup,
String topic, String tag) {
super(name, filter, layout, ignoreExceptions);
this.producer = producer;
this.topic = topic;
this.tag = tag;
this.nameServerAddress = nameServerAddress;
this.producerGroup = producerGroup;
try {
this.producer = ProducerInstance.getProducerInstance().getInstance(this.nameServerAddress, this.producerGroup);
} catch (Exception e) {
ErrorHandler handler = this.getHandler();
if (handler != null) {
handler.error("Starting RocketmqLog4j2Appender [" + this.getName()
+ "] nameServerAddress:" + nameServerAddress + " group:" + producerGroup + " " + e.getMessage());
}
}
}
/**
* When system exit,this method will be called to close resources 当系统退出时,将调用此方法来关闭资源
*/
public boolean stop(long timeout, TimeUnit timeUnit) {
this.setStopping();
try {
ProducerInstance.getProducerInstance().removeAndClose(this.nameServerAddress, this.producerGroup);
} catch (Exception e) {
ErrorHandler handler = this.getHandler();
if (handler != null) {
handler.error("Closeing RocketmqLog4j2Appender [" + this.getName()
+ "] nameServerAddress:" + nameServerAddress + " group:" + producerGroup + " " + e.getMessage());
}
}
boolean stopped = super.stop(timeout, timeUnit, false);
this.setStopped();
return stopped;
}
protected RocketmqLog4j2Appender(String name, Filter filter, Layout<? extends Serializable> layout,
boolean ignoreExceptions, String nameServerAddress, String producerGroup,
String topic, String tag) {
super(name, filter, layout, ignoreExceptions);
this.producer = producer;
this.topic = topic;
this.tag = tag;
this.nameServerAddress = nameServerAddress;
this.producerGroup = producerGroup;
try {
this.producer = ProducerInstance.getProducerInstance().getInstance(this.nameServerAddress, this.producerGroup);
} catch (Exception e) {
ErrorHandler handler = this.getHandler();
if (handler != null) {
handler.error("Starting RocketmqLog4j2Appender [" + this.getName()
+ "] nameServerAddress:" + nameServerAddress + " group:" + producerGroup + " " + e.getMessage());
}
}
}
/**
* When system exit,this method will be called to close resources
*/
public boolean stop(long timeout, TimeUnit timeUnit) {
this.setStopping();
try {
ProducerInstance.getProducerInstance().removeAndClose(this.nameServerAddress, this.producerGroup);
} catch (Exception e) {
ErrorHandler handler = this.getHandler();
if (handler != null) {
handler.error("Closeing RocketmqLog4j2Appender [" + this.getName()
+ "] nameServerAddress:" + nameServerAddress + " group:" + producerGroup + " " + e.getMessage());
}
}
boolean stopped = super.stop(timeout, timeUnit, false);
this.setStopped();
return stopped;
}
protected RocketmqLog4j2Appender(String name, Filter filter, Layout<? extends Serializable> layout,
boolean ignoreExceptions, String nameServerAddress, String producerGroup,
String topic, String tag) {
super(name, filter, layout, ignoreExceptions);
this.producer = producer;
this.topic = topic;
this.tag = tag;
this.nameServerAddress = nameServerAddress;
this.producerGroup = producerGroup;
try {
this.producer = ProducerInstance.getProducerInstance().getInstance(this.nameServerAddress, this.producerGroup);
} catch (Exception e) {
ErrorHandler handler = this.getHandler();
if (handler != null) {
handler.error("Starting RocketmqLog4j2Appender [" + this.getName()
+ "] nameServerAddress:" + nameServerAddress + " group:" + producerGroup + " " + e.getMessage());
}
}
}
/**
* When system exit,this method will be called to close resources
*/
public boolean stop(long timeout, TimeUnit timeUnit) {
this.setStopping();
try {
ProducerInstance.getProducerInstance().removeAndClose(this.nameServerAddress, this.producerGroup);
} catch (Exception e) {
ErrorHandler handler = this.getHandler();
if (handler != null) {
handler.error("Closeing RocketmqLog4j2Appender [" + this.getName()
+ "] nameServerAddress:" + nameServerAddress + " group:" + producerGroup + " " + e.getMessage());
}
}
boolean stopped = super.stop(timeout, timeUnit, false);
this.setStopped();
return stopped;
}
protected RocketmqLog4j2Appender(String name, Filter filter, Layout<? extends Serializable> layout,
boolean ignoreExceptions, String nameServerAddress, String producerGroup,
String topic, String tag) {
super(name, filter, layout, ignoreExceptions);
this.producer = producer;
this.topic = topic;
this.tag = tag;
this.nameServerAddress = nameServerAddress;
this.producerGroup = producerGroup;
try {
this.producer = ProducerInstance.getInstance(this.nameServerAddress, this.producerGroup);
} catch (Exception e) {
ErrorHandler handler = this.getHandler();
if (handler != null) {
handler.error("Starting RocketmqLog4j2Appender [" + this.getName()
+ "] nameServerAddress:" + nameServerAddress + " group:" + producerGroup + " " + e.getMessage());
}
}
}
/**
* When system exit,this method will be called to close resources
*
* @param timeout
* @param timeUnit
* @return
*/
public boolean stop(long timeout, TimeUnit timeUnit) {
this.setStopping();
try {
ProducerInstance.removeAndClose(this.nameServerAddress, this.producerGroup);
} catch (Exception e) {
ErrorHandler handler = this.getHandler();
if (handler != null) {
handler.error("Closeing RocketmqLog4j2Appender [" + this.getName()
+ "] nameServerAddress:" + nameServerAddress + " group:" + producerGroup + " " + e.getMessage());
}
}
boolean stopped = super.stop(timeout, timeUnit, false);
this.setStopped();
return stopped;
}
protected RocketmqLog4j2Appender(String name, Filter filter, Layout<? extends Serializable> layout,
boolean ignoreExceptions, String nameServerAddress, String producerGroup,
String topic, String tag) {
super(name, filter, layout, ignoreExceptions);
this.producer = producer;
this.topic = topic;
this.tag = tag;
this.nameServerAddress = nameServerAddress;
this.producerGroup = producerGroup;
try {
this.producer = ProducerInstance.getProducerInstance().getInstance(this.nameServerAddress, this.producerGroup);
} catch (Exception e) {
ErrorHandler handler = this.getHandler();
if (handler != null) {
handler.error("Starting RocketmqLog4j2Appender [" + this.getName()
+ "] nameServerAddress:" + nameServerAddress + " group:" + producerGroup + " " + e.getMessage());
}
}
}
/**
* When system exit,this method will be called to close resources
*/
public boolean stop(long timeout, TimeUnit timeUnit) {
this.setStopping();
try {
ProducerInstance.getProducerInstance().removeAndClose(this.nameServerAddress, this.producerGroup);
} catch (Exception e) {
ErrorHandler handler = this.getHandler();
if (handler != null) {
handler.error("Closeing RocketmqLog4j2Appender [" + this.getName()
+ "] nameServerAddress:" + nameServerAddress + " group:" + producerGroup + " " + e.getMessage());
}
}
boolean stopped = super.stop(timeout, timeUnit, false);
this.setStopped();
return stopped;
}
/**
* The handler must be set before the appender is started.
*
* @param handler The ErrorHandler to use.
*/
@Override
public void setHandler(final ErrorHandler handler) {
if (handler == null) {
LOGGER.error("The handler cannot be set to null");
return;
}
if (isStarted()) {
LOGGER.error("The handler cannot be changed once the appender is started");
return;
}
this.handler = handler;
}
/**
* Tests https://issues.apache.org/jira/browse/LOG4J2-2441
*/
@Test
public void testSetNullErrorHandlerIsNotAllowed() {
final ConsoleAppender appender = ConsoleAppender.newBuilder().setName("test").build();
ErrorHandler handler = appender.getHandler();
Assert.assertNotNull(handler);
// This could likely be allowed to throw, but we're just testing that
// setting null does not actually set a null handler.
appender.setHandler(null);
Assert.assertSame(handler, appender.getHandler());
}
/**
* {@inheritDoc}
*/
public ErrorHandler getErrorHandler() {
return null;
}
@Override
public ErrorHandler getHandler() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setHandler(ErrorHandler handler) {
// TODO Auto-generated method stub
}
@Override
public ErrorHandler getHandler() {
return null;
}
/**
* {@inheritDoc}
*/
public void setErrorHandler(final ErrorHandler handler) {
}
@Override
public void setHandler(ErrorHandler errorHandler) {
}
/**
* Returns the ErrorHandler, if any.
*
* @return The ErrorHandler.
*/
@Override
public ErrorHandler getHandler() {
return handler;
}