Last active
December 10, 2015 23:49
-
-
Save 166MMX/4512366 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import de.huxhorn.lilith.data.eventsource.EventWrapper | |
import de.huxhorn.lilith.data.eventsource.LoggerContext | |
import de.huxhorn.lilith.data.logging.ExtendedStackTraceElement | |
import de.huxhorn.lilith.data.logging.LoggingEvent | |
import de.huxhorn.lilith.data.logging.Marker | |
import de.huxhorn.lilith.data.logging.ThrowableInfo | |
import de.huxhorn.lilith.services.clipboard.ClipboardFormatter | |
import de.huxhorn.lilith.data.logging.ThreadInfo | |
import de.huxhorn.lilith.data.logging.Message | |
class CopyGEventEvaluatorExp implements ClipboardFormatter | |
{ | |
private static final String mdcFormat = 'e.MDCPropertyMap[%s]' | |
private static final String loggerContextFormat = 'e.loggerContextVO?.propertyMap[%s]' | |
private static final int colSize = 24; | |
String getName() | |
{ | |
return 'Copy GEventEvaluatorExp' | |
} | |
String getDescription() | |
{ | |
return 'Copy GEventEvaluatorExp' | |
} | |
String getAccelerator() | |
{ | |
return null | |
} | |
boolean isCompatible(Object object) | |
{ | |
if (!(object instanceof EventWrapper)) | |
{ | |
return false | |
} | |
EventWrapper wrapper = (EventWrapper) object | |
Object event = wrapper.getEvent() | |
if (!(event instanceof LoggingEvent)) | |
{ | |
return false | |
} | |
return true | |
} | |
String toString(Object object) | |
{ | |
if (!(object instanceof EventWrapper)) | |
{ | |
return null | |
} | |
EventWrapper wrapper = (EventWrapper) object | |
Object event = wrapper.getEvent() | |
if (!(event instanceof LoggingEvent)) | |
{ | |
return null | |
} | |
LoggingEvent loggingEvent = (LoggingEvent) event | |
StringBuilder sb = new StringBuilder() | |
if (loggingEvent.level instanceof LoggingEvent.Level) | |
{ | |
appendCondition(sb, 'e.level', loggingEvent.level.toString()) | |
} | |
if (loggingEvent.logger instanceof String) | |
{ | |
appendCondition(sb, 'e.loggerName', loggingEvent.logger.inspect()) | |
} | |
if (loggingEvent.message instanceof Message && loggingEvent.message?.messagePattern instanceof String) | |
{ | |
appendCondition(sb, 'e.message', loggingEvent.message.messagePattern.inspect()) | |
} | |
if (loggingEvent.message instanceof Message && loggingEvent.message?.message instanceof String) | |
{ | |
appendCondition(sb, 'e.formattedMessage', loggingEvent.message.message.inspect()) | |
} | |
if (loggingEvent.mdc instanceof Map<String,String>) | |
{ | |
loggingEvent.mdc.each { key, val -> | |
String formattedKey = String.format(mdcFormat, key.inspect()) | |
appendCondition(sb, formattedKey, val.inspect()) | |
} | |
} | |
if (loggingEvent.marker instanceof Marker && loggingEvent.marker.name instanceof String) | |
{ | |
appendMethod(sb, 'e.marker?.contains', loggingEvent.marker.name.inspect()) | |
// TODO add all markers when available | |
} | |
if (loggingEvent.throwable instanceof ThrowableInfo) | |
{ | |
if (loggingEvent.throwable.message instanceof String) | |
{ | |
appendCondition(sb, 'e.throwableProxy?.message', loggingEvent.throwable.message.inspect()) | |
} | |
if (loggingEvent.throwable.name instanceof String) | |
{ | |
appendCondition(sb, 'e.throwableProxy?.className', loggingEvent.throwable.name.inspect()) | |
} | |
} | |
if (loggingEvent.callStack instanceof ExtendedStackTraceElement[]) | |
{ | |
appendCondition(sb, 'e.callerData?.length', loggingEvent.callStack.length.toString()) | |
loggingEvent.callStack.eachWithIndex { ExtendedStackTraceElement it, int i -> | |
StringBuilder prefix = new StringBuilder() | |
prefix.append('e.callerData[').append(i).append(']') | |
if (it.methodName instanceof String) | |
{ | |
appendCondition(sb, new StringBuilder(prefix).append('?.methodName'), it.methodName.inspect()) | |
} | |
if (it.className instanceof String) | |
{ | |
appendCondition(sb, new StringBuilder(prefix).append('?.className'), it.className.inspect()) | |
} | |
if (it.lineNumber instanceof Integer) | |
{ | |
appendCondition(sb, new StringBuilder(prefix).append('?.lineNumber'), it.lineNumber.toString()) | |
} | |
if (it.fileName instanceof String) | |
{ | |
appendCondition(sb, new StringBuilder(prefix).append('?.fileName'), it.fileName.inspect()) | |
} | |
} | |
} | |
if (loggingEvent.threadInfo instanceof ThreadInfo && loggingEvent.threadInfo?.name instanceof String) | |
{ | |
appendCondition(sb, 'e.threadName', loggingEvent.threadInfo.name.inspect()) | |
} | |
if (loggingEvent.loggerContext instanceof LoggerContext && loggingEvent.loggerContext?.properties instanceof Map<String,String>) | |
{ | |
loggingEvent.loggerContext.properties.each { key, val -> | |
String formattedKey = String.format(loggerContextFormat, key.inspect()) | |
appendCondition(sb, formattedKey, val.inspect()) | |
} | |
} | |
return sb | |
} | |
private void appendCondition(StringBuilder sb, StringBuilder left, String right) | |
{ | |
String paddedLeft = left.padRight(colSize) | |
if (sb.length() > 0) | |
{ | |
sb.append(' &&\n') | |
} | |
sb.append(paddedLeft) | |
sb.append(' == ') | |
sb.append(right) | |
} | |
private void appendCondition(StringBuilder sb, String left, String right) | |
{ | |
String paddedLeft = left.padRight(colSize) | |
if (sb.length() > 0) | |
{ | |
sb.append(' &&\n') | |
} | |
sb.append(paddedLeft) | |
sb.append(' == ') | |
sb.append(right) | |
} | |
private void appendMethod(StringBuilder sb, String name, String arg) | |
{ | |
if (sb.length() > 0) | |
{ | |
sb.append(' &&\n') | |
} | |
sb.append(name) | |
sb.append('(') | |
sb.append(arg) | |
sb.append(')') | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import de.huxhorn.lilith.data.eventsource.EventWrapper | |
import de.huxhorn.lilith.data.logging.LoggingEvent | |
import de.huxhorn.lilith.data.logging.Message | |
import de.huxhorn.lilith.data.logging.Marker | |
import de.huxhorn.lilith.data.logging.ThrowableInfo | |
import de.huxhorn.lilith.data.logging.ExtendedStackTraceElement | |
import de.huxhorn.lilith.data.logging.ThreadInfo | |
import de.huxhorn.lilith.data.eventsource.LoggerContext | |
import de.huxhorn.lilith.services.clipboard.ClipboardFormatter | |
class CopyLilithConditionExp implements ClipboardFormatter | |
{ | |
private static final String mdcFormat = 'e.mdc[%s]' | |
private static final String loggerContextFormat = 'e.loggerContext?.properties[%s]' | |
private static final int colSize = 24; | |
String getName() | |
{ | |
return 'Copy LilithConditionExp' | |
} | |
String getDescription() | |
{ | |
return 'Copy LilithConditionExp' | |
} | |
String getAccelerator() | |
{ | |
return null | |
} | |
boolean isCompatible(Object object) | |
{ | |
if (!(object instanceof EventWrapper)) | |
{ | |
return false | |
} | |
EventWrapper wrapper = (EventWrapper) object | |
Object event = wrapper.getEvent() | |
if (!(event instanceof LoggingEvent)) | |
{ | |
return false | |
} | |
return true | |
} | |
String toString(Object object) | |
{ | |
if (!(object instanceof EventWrapper)) | |
{ | |
return null | |
} | |
EventWrapper wrapper = (EventWrapper) object | |
Object event = wrapper.getEvent() | |
if (!(event instanceof LoggingEvent)) | |
{ | |
return null | |
} | |
LoggingEvent loggingEvent = (LoggingEvent) event | |
StringBuilder sb = new StringBuilder() | |
if (loggingEvent.level instanceof LoggingEvent.Level) | |
{ | |
appendCondition(sb, 'e.level', loggingEvent.level.toString()) | |
} | |
if (loggingEvent.logger instanceof String) | |
{ | |
appendCondition(sb, 'e.logger', loggingEvent.logger.inspect()) | |
} | |
if (loggingEvent.message instanceof Message && loggingEvent.message?.messagePattern instanceof String) | |
{ | |
appendCondition(sb, 'e.message?.messagePattern', loggingEvent.message.messagePattern.inspect()) | |
} | |
if (loggingEvent.message instanceof Message && loggingEvent.message?.message instanceof String) | |
{ | |
appendCondition(sb, 'e.message?.message', loggingEvent.message.message.inspect()) | |
} | |
if (loggingEvent.mdc instanceof Map<String, String>) | |
{ | |
loggingEvent.mdc.each { key, val -> | |
String formattedKey = String.format(mdcFormat, key.inspect()) | |
appendCondition(sb, formattedKey, val.inspect()) | |
} | |
} | |
if (loggingEvent.marker instanceof Marker && loggingEvent.marker.name instanceof String) | |
{ | |
appendMethod(sb, 'e.marker?.contains', loggingEvent.marker.name.inspect()) | |
// TODO add all markers when available | |
} | |
if (loggingEvent.throwable instanceof ThrowableInfo) | |
{ | |
if (loggingEvent.throwable.message instanceof String) | |
{ | |
appendCondition(sb, 'e.throwable?.message', loggingEvent.throwable.message.inspect()) | |
} | |
if (loggingEvent.throwable.name instanceof String) | |
{ | |
appendCondition(sb, 'e.throwable?.name', loggingEvent.throwable.name.inspect()) | |
} | |
} | |
if (loggingEvent.callStack instanceof ExtendedStackTraceElement[]) | |
{ | |
appendCondition(sb, 'e.callStack?.length', loggingEvent.callStack.length.toString()) | |
loggingEvent.callStack.eachWithIndex { ExtendedStackTraceElement it, int i -> | |
StringBuilder prefix = new StringBuilder() | |
prefix.append('e.callStack[').append(i).append(']') | |
if (it.methodName instanceof String) | |
{ | |
appendCondition(sb, new StringBuilder(prefix).append('?.methodName'), it.methodName.inspect()) | |
} | |
if (it.className instanceof String) | |
{ | |
appendCondition(sb, new StringBuilder(prefix).append('?.className'), it.className.inspect()) | |
} | |
if (it.lineNumber instanceof Integer) | |
{ | |
appendCondition(sb, new StringBuilder(prefix).append('?.lineNumber'), it.lineNumber.toString()) | |
} | |
if (it.fileName instanceof String) | |
{ | |
appendCondition(sb, new StringBuilder(prefix).append('?.fileName'), it.fileName.inspect()) | |
} | |
} | |
} | |
if (loggingEvent.threadInfo instanceof ThreadInfo && loggingEvent.threadInfo?.name instanceof String) | |
{ | |
appendCondition(sb, 'e.threadInfo?.name', loggingEvent.threadInfo.name.inspect()) | |
} | |
if (loggingEvent.loggerContext instanceof LoggerContext && loggingEvent.loggerContext?.properties instanceof Map<String, String>) | |
{ | |
loggingEvent.loggerContext.properties.each { key, val -> | |
String formattedKey = String.format(loggerContextFormat, key.inspect()) | |
appendCondition(sb, formattedKey, val.inspect()) | |
} | |
} | |
return sb | |
} | |
private void appendCondition(StringBuilder sb, StringBuilder left, String right) | |
{ | |
String paddedLeft = left.padRight(colSize) | |
if (sb.length() > 0) | |
{ | |
sb.append(' &&\n') | |
} | |
sb.append(paddedLeft) | |
sb.append(' == ') | |
sb.append(right) | |
} | |
private void appendCondition(StringBuilder sb, String left, String right) | |
{ | |
String paddedLeft = left.padRight(colSize) | |
if (sb.length() > 0) | |
{ | |
sb.append(' &&\n') | |
} | |
sb.append(paddedLeft) | |
sb.append(' == ') | |
sb.append(right) | |
} | |
private void appendMethod(StringBuilder sb, String name, String arg) | |
{ | |
if (sb.length() > 0) | |
{ | |
sb.append(' &&\n') | |
} | |
sb.append(name) | |
sb.append('(') | |
sb.append(arg) | |
sb.append(')') | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment