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()); } } }
/** * Info,error,warn,callback method implementation */ public void append(LogEvent event) { if (null == producer) { return; } byte[] data = this.getLayout().toByteArray(event); try { Message msg = new Message(topic, tag, data); msg.getProperties().put(ProducerInstance.APPENDER_TYPE, ProducerInstance.LOG4J2_APPENDER); //Send message and do not wait for the ack from the message broker. producer.sendOneway(msg); } catch (Exception e) { ErrorHandler handler = this.getHandler(); if (handler != null) { String msg = new String(data); handler.error("Could not send message in RocketmqLog4j2Appender [" + this.getName() + "].Message is : " + msg, e); } } }
/** * 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()); } } }
/** * Info,error,warn,callback method implementation */ public void append(LogEvent event) { if (null == producer) { return; } byte[] data = this.getLayout().toByteArray(event); try { Message msg = new Message(topic, tag, data); msg.getProperties().put(ProducerInstance.APPENDER_TYPE, ProducerInstance.LOG4J2_APPENDER); //Send message and do not wait for the ack from the message broker. producer.sendOneway(msg); } catch (Exception e) { ErrorHandler handler = this.getHandler(); if (handler != null) { String msg = new String(data); handler.error("Could not send message in RocketmqLog4j2Appender [" + this.getName() + "].Message is : " + msg, e); } } }
/** * 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; }
/** * Handle an error with a message using the {@link ErrorHandler} configured for this Appender. * * @param msg The message. */ public void error(final String msg) { handler.error(msg); }
/** * Handle an error with a message, exception, and a logging event, using the {@link ErrorHandler} configured for * this Appender. * * @param msg The message. * @param event The LogEvent. * @param t The Throwable. */ public void error(final String msg, final LogEvent event, final Throwable t) { handler.error(msg, event, t); }
/** * Handle an error with a message and an exception using the {@link ErrorHandler} configured for this Appender. * * @param msg The message. * @param t The Throwable. */ public void error(final String msg, final Throwable t) { handler.error(msg, t); }
private String appenderErrorHandlerMessage(final String prefix) { final String result = createErrorMsg(prefix); appender.getHandler().error(result); return result; }
private synchronized void changeState(State initialState, State transitionState, State finalState, Runnable body) { logger.debug("expecting state: %s", initialState); if (initialState != state) { String message = String.format("expecting: %s, found: %s", initialState, state); errorHandler.error(message); throw new IllegalStateException(message); } logger.debug("transitioning state: %s", transitionState); state = transitionState; try { if (body != null) { body.run(); } logger.debug("finalizing state: %s", finalState); state = finalState; } catch (Exception error) { String message = String.format( "state change failure (initialState=%s, transitionState=%s, finalState=%s)", initialState, transitionState, finalState); if (debugEnabled) { logger.debug("%s: %s", message, error.getMessage()); error.printStackTrace(); } errorHandler.error(message, error); throw new RuntimeException(message, error); } }
private void handleAppenderError(final RuntimeException ex) { appender.getHandler().error(createErrorMsg("An exception occurred processing Appender "), ex); if (!appender.ignoreExceptions()) { throw ex; } }
/** * {@inheritDoc} */ public void append(LogEvent event) { if (_handle == null) { registerEventSource(); } String s = new String(getLayout().toByteArray(event)); final int messageID = 0x1000; String[] buffer = { s }; if (Advapi32.INSTANCE.ReportEvent(_handle, getEventLogType(event.getLevel()), getEventLogCategory(event.getLevel()), messageID, null, buffer.length, 0, buffer, null) == false) { Exception e = new Win32Exception(Kernel32.INSTANCE.GetLastError()); getHandler().error("Failed to report event [" + s + "].", event, e); } }