@Override public final String log(Object... instancesToLog) { return getOrCreateGson().toJson(instancesToLog); }
/** * Gets or creates the gson instance generation of output. */ private Gson getOrCreateGson() { if (gson == null) { final GsonBuilder gsonBuilder = new GsonBuilder(); final TraceeGenericGsonSerializer gsonSerializer = new TraceeGenericGsonSerializer(new ProfileSettings(this.getProfile(), this.getManualContextOverrides())); for (Class clazz : this.getWrapperClasses()) { gsonBuilder.registerTypeAdapter(clazz, gsonSerializer); } gson = gsonBuilder.create(); } return gson; }
/** * Creates a TraceeGsonContextLogBuilder instance which can be used for creating the log message. * * @return An instance of TraceeGsonContextLogBuilder */ private TraceeGsonContextLogBuilder createGsonLogBuilder() { TraceeGsonContextLogBuilder tmpTraceeGsonContextLogBuilder = new TraceeGsonContextLogBuilder(); tmpTraceeGsonContextLogBuilder.setWrapperClasses(contextLoggerConfiguration.getWrapperClasses()); tmpTraceeGsonContextLogBuilder.setManualContextOverrides(configBuilder.getManualContextOverrides()); tmpTraceeGsonContextLogBuilder.setProfile(this.configBuilder.getProfile()); return tmpTraceeGsonContextLogBuilder; } }
Collections.sort(entriesToPrint, new MethodAnnotationPairComparator()); if (!singleEntry.shouldBeProcessed(profileSettings)) { continue; Object returnValue = singleEntry.getMethod().invoke(instance, null); if (TraceeContextLogAnnotationUtilities.isFlatable(singleEntry.getMethod()) && (isNameStringValuePair(returnValue))) { if (TraceeContextLogAnnotationUtilities.isFlatable(singleEntry.getMethod()) && (isNameObjectValuePair(returnValue))) { final Object value = getValueOfNameObjectValuePair(nameObjectValuePair); result.add(nameObjectValuePair.getName(), jsonSerializationContext.serialize(value)); } else if (TraceeContextLogAnnotationUtilities.isFlatable(singleEntry.getMethod()) && ListUtilities.isListOfType(returnValue, NameStringValuePair.class)) { } else if (TraceeContextLogAnnotationUtilities.isFlatable(singleEntry.getMethod()) && ListUtilities.isListOfType(returnValue, NameObjectValuePair.class)) { final Object value = getValueOfNameObjectValuePair(nameObjectValuePair); result.add(nameObjectValuePair.getName(), jsonSerializationContext.serialize(value)); result.add(singleEntry.getAnnotation().displayName(), jsonSerializationContext.serialize(returnValue));
@Override public int compare(MethodAnnotationPair instance1, MethodAnnotationPair instance2) { // primary sort criteria is the order value of the annotation if (instance1 == null && instance2 == null) { return 0; } else if (instance1 != null && instance2 == null) { return 1; } else if (instance1 == null) { return -1; } else { int result = Integer.valueOf(instance1.getAnnotation().order()).compareTo(instance2.getAnnotation().order()); if (result == 0) { result = instance1.getAnnotation().displayName().compareTo(instance2.getAnnotation().displayName()); } return result; } } }
/** * Extracts all {@link io.tracee.contextlogger.api.TraceeContextLogProviderMethod} annotated methods of the passed instance. * * @param instance the instance to check for * @return a list containing all annotated methods of an instance */ public static List<MethodAnnotationPair> getAnnotatedMethodsOfInstance(final Object instance) { List<MethodAnnotationPair> result = new ArrayList<MethodAnnotationPair>(); for (Method method : instance.getClass().getDeclaredMethods()) { if (checkIsPublic(method) && checkMethodHasNoParameters(method) && checkMethodHasNonVoidReturnType(method)) { TraceeContextLogProviderMethod annotation = method.getAnnotation(TraceeContextLogProviderMethod.class); // check if method has no parameters and is annotated if (annotation != null) { result.add(new MethodAnnotationPair(method, annotation)); } } } return result; }
/** * selects the best macthing serialization depending on value type. * * @param nameObjectValuePair * @return */ private Object getValueOfNameObjectValuePair(final NameObjectValuePair nameObjectValuePair) { if (nameObjectValuePair != null && nameObjectValuePair.getValue() != null) { if (TraceeContextLogAnnotationUtilities.getAnnotationFromType(nameObjectValuePair.getValue()) != null) { return nameObjectValuePair.getValue(); } else if (!shouldBeIgnoreAtDeSerialization(nameObjectValuePair.getValue())) { return ReflectionToStringBuilder.reflectionToString(nameObjectValuePair.getValue(), new RecursiveReflectionToStringStyle()); } else { // not null value - but type is in IGNORED_AT_DESERIALIZATION set return nameObjectValuePair.getValue().toString(); } } else { return null; } }
@Override public final String logPassedContext(PassedDataContextProvider passedContextData) { return getOrCreateGson().toJson(passedContextData); }