@Override public void logJsonWithPrefixedMessage(String prefixedMessage, Object... instancesToLog) { this.connectorsWrapper.sendErrorReportToConnectors(prefixedMessage, createJson(instancesToLog)); }
/** * Initializes all available connectors. */ private void initConnectors() { connectorsWrapper = new ConnectorFactory(); }
private TraceeContextLogger(ContextLoggerConfiguration contextLoggerConfiguration) { this.contextLoggerConfiguration = contextLoggerConfiguration; initConnectors(); }
/** * Sends the error reports to all connectors. * * @param proceedingJoinPoint the aspectj calling context * @param annotatedId the id defined in the watchdog annotation */ void sendErrorReportToConnectors(ProceedingJoinPoint proceedingJoinPoint, String annotatedId, Throwable e) { // try to get error message annotation ErrorMessage errorMessage = WatchdogUtils.getErrorMessageAnnotation(proceedingJoinPoint); if (errorMessage == null) { TraceeContextLogger .create() .enforceOrder() .apply() .logWithPrefixedMessage(MessagePrefixProvider.provideLogMessagePrefix(MessageLogLevel.ERROR, Watchdog.class), CoreImplicitContextProviders.COMMON, CoreImplicitContextProviders.TRACEE, WatchdogDataWrapper.wrap(annotatedId, proceedingJoinPoint), e); } else { TraceeContextLogger .create() .enforceOrder() .apply() .logWithPrefixedMessage(MessagePrefixProvider.provideLogMessagePrefix(MessageLogLevel.ERROR, Watchdog.class), TraceeMessage.wrap(errorMessage.value()), CoreImplicitContextProviders.COMMON, CoreImplicitContextProviders.TRACEE, WatchdogDataWrapper.wrap(annotatedId, proceedingJoinPoint), e); } }
private void handleHttpServletRequest(HttpServletRequest servletRequest, HttpServletResponse servletResponse, Throwable e) { TraceeContextLogger .create() .enforceOrder() .apply() .logWithPrefixedMessage(MessagePrefixProvider.provideLogMessagePrefix(MessageLogLevel.ERROR, TraceeErrorLoggingFilter.class), CoreImplicitContextProviders.COMMON, CoreImplicitContextProviders.TRACEE, servletRequest, servletResponse, servletRequest.getSession(false), e); }
return createInstance(contextLoggerConfiguration.getImplicitContextClassMap().get(instance)); Class knownWrapperType = contextLoggerConfiguration.getClassToWrapperMap().get(instance.getClass()); if (knownWrapperType != null) { Object wrappedInstance = createInstance(knownWrapperType); if (WrappedContextData.class.isAssignableFrom(knownWrapperType)) { ((WrappedContextData) wrappedInstance).setContextData(instance); for (TypeToWrapper wrapper : contextLoggerConfiguration.getWrapperList()) { if (wrapper.getWrappedInstanceType().isAssignableFrom(instance.getClass())) { try { WrappedContextData wrapperInstance = (WrappedContextData) createInstance(wrapper.getWrapperType()); wrapperInstance.setContextData(instance);
/** * Initializes all available connectors. */ private void initConnectors() { // first get all connector configuration Names Set<String> connectorConfigurationNames = this.getConnectorConfigurationNames(); for (String connectorConfigurationName : connectorConfigurationNames) { Connector connector = this.createConnector(connectorConfigurationName); if (connector != null) { this.connectorMap.put(connectorConfigurationName, connector); } } // Add mandatory logger if (!isConnectorConfigured(LogConnector.class)) { Connector logConnector = new LogConnector(); this.connectorMap.put("LOGGER", logConnector); } }
/** * This method handles the wrapping of the incoming object and passes them to the context toJson builder implementation. * * @param instancesToLog an array of objects to wrap * @return the contextual toJson information as a String */ String propagateToContextLogBuilder(Object[] instancesToLog) { Object[] propagateArray = null; if (instancesToLog != null) { propagateArray = new Object[instancesToLog.length]; for (int i = 0; i < instancesToLog.length; i++) { propagateArray[i] = wrapInstance(instancesToLog[i]); } } return traceeContextLogBuilder.createStringRepresentationForPassedDataContextProvider(new PassedDataContextProvider(propagateArray, traceeContextLogBuilder.getKeepOrder())); }
public static ContextLoggerBuilder create() { TraceeContextLogger contextLoggerInstance = new TraceeContextLogger(ContextLoggerConfiguration.getOrCreateContextLoggerConfiguration()); return new ContextLoggerBuilderImpl(contextLoggerInstance); }
public static String provideLogMessagePrefix(final MessageLogLevel logLevel, final Class type) { return provideLogMessagePrefix(logLevel, type != null ? type.getSimpleName() : null); } }
/** * Tries to create a Connector for a given connector configuration name. * * @param connectorConfigurationName the name of the connector configuration * @return the connector if it could be created and initialized without error, otherwise null */ final Connector createConnector(final String connectorConfigurationName) { Map<String, String> propertyMap = this.getPropertiesForConnectorConfigurationName(connectorConfigurationName); String type = propertyMap.get(TraceeContextLoggerConstants.SYSTEM_PROPERTY_CONTEXT_LOGGER_CONNECTOR_TYPE); // get canonical class name for well known connectors if (WELL_KNOW_CONNECTOR_MAPPINGS.containsKey(type)) { type = WELL_KNOW_CONNECTOR_MAPPINGS.get(type); } try { // try to create connector instance Connector connector = (Connector) Class.forName(type).newInstance(); // now try to call init method connector.init(propertyMap); return connector; } catch (Exception e) { LOGGER.error("An error occurred while creating connector with name '" + connectorConfigurationName + "' of type '" + type + "'", e); } return null; }
/** * Tries to create a Connector for a given connector configuration name. * * @param connectorConfigurationName the name of the connector configuration * @return the connector if it could be created and initialized without error, otherwise null */ final Connector createConnector(final String connectorConfigurationName) { Map<String, String> propertyMap = this.getPropertiesForConnectorConfigurationName(connectorConfigurationName); String type = propertyMap.get(TraceeContextLoggerConstants.SYSTEM_PROPERTY_CONTEXT_LOGGER_CONNECTOR_TYPE); // get canonical class name for well known connectors if (WELL_KNOW_CONNECTOR_MAPPINGS.containsKey(type)) { type = WELL_KNOW_CONNECTOR_MAPPINGS.get(type); } try { // try to create connector instance Connector connector = (Connector) Class.forName(type).newInstance(); // now try to call init method connector.init(propertyMap); return connector; } catch (Exception e) { LOGGER.error("An error occurred while creating connector with name '" + connectorConfigurationName + "' of type '" + type + "'", e); } return null; }
/** * Send error report to all initialized connector instances. * * @param prefix the prefix to prepend only via the log connector output * @param json the context data to output */ public final void sendErrorReportToConnectors(String prefix, String json) { for (Connector connector : this.connectorMap.values()) { // prevent if (LogConnector.class.isInstance(connector)) { connector.sendErrorReport(prefix != null ? prefix + json : json); } else { connector.sendErrorReport(json); } } }
/** * Extracts all names for connector configurations from System properties. * * @return a Set containing all connector configuration names */ final Set<String> getConnectorConfigurationNames() { Set<String> connectorNames = new HashSet<String>(); Enumeration<Object> keyEnumeration = getSystemProperties().keys(); while (keyEnumeration.hasMoreElements()) { String key = keyEnumeration.nextElement().toString(); // check if property key has tracee connector format Matcher matcher = KEY_MATCHER_PATTERN.matcher(key); if (matcher.matches() && matcher.groupCount() > 0) { connectorNames.add(matcher.group(1)); } } return connectorNames; }
@Override public void logJson(Object... instancesToLog) { this.logJsonWithPrefixedMessage(null, instancesToLog); }
@Override public String createJson(Object... instancesToLog) { return propagateToContextLogBuilder(instancesToLog); }
ConnectorFactory() { initConnectors(); }
/** * Send error report to all initialized connector instances. * * @param prefix the prefix to prepend only via the createStringRepresentation connector output * @param json the context data to output */ final void sendErrorReportToConnectors(String prefix, String json) { for (Connector connector : this.connectorMap.values()) { // prevent if (LogConnector.class.isInstance(connector)) { connector.sendErrorReport(prefix != null ? prefix + json : json); } else { connector.sendErrorReport(json); } } }
/** * Collects all properties for a given connector configuration name and writes them to a Map. * * @param connectorName the name of the connector configuration * @return a Map containing all properties for a connector configuration name */ final Map<String, String> getPropertiesForConnectorConfigurationName(final String connectorName) { final Map<String, String> propertyMap = new HashMap<String, String>(); final String patternString = String.format(CONNECTOR_PROPERTY_GRABBER_PATTERN, connectorName); final Pattern propertyGrabPattern = Pattern.compile(patternString); final Set<Map.Entry<Object, Object>> entries = getSystemProperties().entrySet(); for (Map.Entry<Object, Object> entry : entries) { final String key = entry.getKey().toString(); final Object value = entry.getValue(); // check if property key has tracee connector format final Matcher matcher = propertyGrabPattern.matcher(key); if (value != null && matcher.matches() && matcher.groupCount() > 0) { final String propertyName = matcher.group(1); propertyMap.put(propertyName, value.toString()); } } return propertyMap; }