|
@@ -0,0 +1,87 @@
|
|
|
+package cn.gygxzc.envir.core.logger;
|
|
|
+
|
|
|
+import ch.qos.logback.classic.Level;
|
|
|
+import ch.qos.logback.classic.spi.ILoggingEvent;
|
|
|
+import ch.qos.logback.core.AsyncAppenderBase;
|
|
|
+import ch.qos.logback.core.spi.FilterReply;
|
|
|
+import ch.qos.logback.core.status.WarnStatus;
|
|
|
+import cn.gygxzc.tina.cloud.jwt.session.utils.SessionUtils;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author tuonina
|
|
|
+ * @createTime 2019/3/25
|
|
|
+ */
|
|
|
+public class MQAsyncAppender extends AsyncAppenderBase<ILoggingEvent> {
|
|
|
+
|
|
|
+
|
|
|
+ private boolean includeCallerData = false;
|
|
|
+ /**
|
|
|
+ * The guard prevents an appender from repeatedly calling its own doAppend
|
|
|
+ * method.
|
|
|
+ */
|
|
|
+ private ThreadLocal<Boolean> guard = new ThreadLocal<Boolean>();
|
|
|
+ private int statusRepeatCount = 0;
|
|
|
+ private int exceptionCount = 0;
|
|
|
+ private static final int ALLOWED_REPEATS = 3;
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void doAppend(ILoggingEvent eventObject) {
|
|
|
+ // WARNING: The guard check MUST be the first statement in the
|
|
|
+ // doAppend() method.
|
|
|
+ // prevent re-entry.
|
|
|
+ if (Boolean.TRUE.equals(guard.get())) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ guard.set(Boolean.TRUE);
|
|
|
+ if (!this.started) {
|
|
|
+ if (statusRepeatCount++ < ALLOWED_REPEATS) {
|
|
|
+ addStatus(new WarnStatus("Attempted to append to non started appender [" + name + "].", this));
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (getFilterChainDecision(eventObject) == FilterReply.DENY) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ CoreLoggerEvent loggerEvent = new CoreLoggerEvent(eventObject);
|
|
|
+ loggerEvent.setUser(SessionUtils.getUser());
|
|
|
+ // ok, we now invoke derived class' implementation of append
|
|
|
+ this.append(loggerEvent);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ if (exceptionCount++ < ALLOWED_REPEATS) {
|
|
|
+ addError("Appender [" + name + "] failed to append.", e);
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ guard.set(Boolean.FALSE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Events of level TRACE, DEBUG and INFO are deemed to be discardable.
|
|
|
+ *
|
|
|
+ * @param event
|
|
|
+ * @return true if the event is of level TRACE, DEBUG or INFO false otherwise.
|
|
|
+ */
|
|
|
+ protected boolean isDiscardable(ILoggingEvent event) {
|
|
|
+ Level level = event.getLevel();
|
|
|
+ return level.toInt() <= Level.INFO_INT;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected void preprocess(ILoggingEvent eventObject) {
|
|
|
+ eventObject.prepareForDeferredProcessing();
|
|
|
+ if (includeCallerData)
|
|
|
+ eventObject.getCallerData();
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean isIncludeCallerData() {
|
|
|
+ return includeCallerData;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setIncludeCallerData(boolean includeCallerData) {
|
|
|
+ this.includeCallerData = includeCallerData;
|
|
|
+ }
|
|
|
+}
|