Which logging log level will depend on the configuration of the user when they use Huong Dan Java Logger and more importantly whether the logging level is enabled in the Logging Framework configuration file? Therefore, we need to write a new method to test these conditions.
I write as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
private void doLogging(Logger logger, LoggingLevel loggingLevel, String message) { switch (loggingLevel) { case DEBUG: if (logger.isDebugEnabled()) { logger.debug(message); } break; case WARN: if (logger.isWarnEnabled()) { logger.warn(message); } break; case INFO: if (logger.isInfoEnabled()) { logger.info(message); } break; case ERROR: if (logger.isErrorEnabled()) { logger.error(message); } break; case TRACE: if (logger.isTraceEnabled()) { logger.trace(message); } break; default: LOGGER.warn("No Logging Level was set"); break; } } |
In the absence of logging level set, we need to display a warning message to inform the user.
And now we can use this new method to log the message out:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
package com.huongdanjava.hdjlogger; import org.apache.commons.lang3.StringUtils; import org.mule.api.MuleEvent; import org.mule.api.MuleException; import org.mule.api.annotations.Category; import org.mule.api.annotations.Config; import org.mule.api.annotations.Connector; import org.mule.api.annotations.Processor; import org.mule.api.annotations.display.FriendlyName; import org.mule.api.annotations.display.Placement; import org.mule.api.annotations.param.Default; import org.mule.api.annotations.param.Optional; import org.mule.api.expression.ExpressionManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.MDC; import com.huongdanjava.hdjlogger.config.ConnectorConfig; import com.huongdanjava.hdjlogger.constant.LoggingLevel; import com.huongdanjava.hdjlogger.constant.Text; @Connector(name="huong-dan-java-logger-component", friendlyName="Huong Dan Java Logger") @Category(name = "org.mule.tooling.category.core", description = "Components") public class HuongDanJavaLoggerComponentConnector { private static final Logger LOGGER = LoggerFactory.getLogger(HuongDanJavaLoggerComponentConnector.class); private static final String CORRELATION_ID_ELEMENT = "correlationId"; @Config ConnectorConfig config; @Processor public String log( @Placement(group = Text.GENERIC, order = 1) @Optional String message, @Placement(group = Text.GENERIC, order = 2) @Default("INFO") @FriendlyName(Text.LEVEL) LoggingLevel loggingLevel, @Placement(group = Text.OVERRIDE, order = 1) @Optional @FriendlyName(Text.OVVERIDE_CORRELATION_ID) String globalCorrelationIdOverride, @Placement(group = Text.OVERRIDE, order = 2) @Optional @FriendlyName(Text.OVVERIDE_CATEGORY) String globalCategoryOverride, MuleEvent event) { String category = getCategory(config, globalCategoryOverride); String correlationId = getEvaluatedCorrelationId(event, config, globalCorrelationIdOverride); MDC.put(CORRELATION_ID_ELEMENT, correlationId); String evaluatedMessage = getEvaluatedMessage(event, message); Logger logger = LoggerFactory.getLogger(category); if (evaluatedMessage != null) { doLogging(logger, loggingLevel, evaluatedMessage); } return config.getCorrelationId(); } private void doLogging(Logger logger, LoggingLevel loggingLevel, String message) { switch (loggingLevel) { case DEBUG: if (logger.isDebugEnabled()) { logger.debug(message); } break; case WARN: if (logger.isWarnEnabled()) { logger.warn(message); } break; case INFO: if (logger.isInfoEnabled()) { logger.info(message); } break; case ERROR: if (logger.isErrorEnabled()) { logger.error(message); } break; case TRACE: if (logger.isTraceEnabled()) { logger.trace(message); } break; default: LOGGER.warn("No Logging Level was set"); break; } } private String getEvaluatedMessage(MuleEvent event, String message) { if (StringUtils.isEmpty(message)) { try { return event.getMessageAsString(); } catch (MuleException e) { LOGGER.error(e.getMessage(), e); return null; } } return evaluateMEL(event, message); } private String getEvaluatedCorrelationId(MuleEvent event, ConnectorConfig cc, String globalCorrelationIdOverride) { if (!StringUtils.isEmpty(globalCorrelationIdOverride)) { return evaluateMEL(event, globalCorrelationIdOverride); } return evaluateMEL(event, cc.getCorrelationId()); } private String getCategory(ConnectorConfig cc, String globalCategoryOverride) { if (!StringUtils.isEmpty(globalCategoryOverride)) { return globalCategoryOverride; } return cc.getCategory(); } private String evaluateMEL(MuleEvent event, String text) { ExpressionManager em = event.getMuleContext().getExpressionManager(); return em.parse(text, event); } public ConnectorConfig getConfig() { return config; } public void setConfig(ConnectorConfig config) { this.config = config; } } |