diff --git a/logging-log4j/src/main/java/com/cisco/oss/foundation/logging/ApplicationState.java b/logging-log4j/src/main/java/com/cisco/oss/foundation/logging/ApplicationState.java index 132642f..b21f28a 100644 --- a/logging-log4j/src/main/java/com/cisco/oss/foundation/logging/ApplicationState.java +++ b/logging-log4j/src/main/java/com/cisco/oss/foundation/logging/ApplicationState.java @@ -84,7 +84,7 @@ public Integer setState(final FoundationLevel level, final Object message) { String flowContext = (String) MDC.get("flowCtxt"); MDC.remove("flowCtxt"); LOGGER.log(FQCN, getLog4jLevel(level), message, null); - if (flowContext != null) { + if (flowContext != null) { MDC.put("flowCtxt", flowContext); } appState.put(key, newValue); diff --git a/logging-log4j/src/main/java/com/cisco/oss/foundation/logging/transactions/ConfigurationUtil.java b/logging-log4j/src/main/java/com/cisco/oss/foundation/logging/transactions/ConfigurationUtil.java index 5bea36e..9ecba05 100644 --- a/logging-log4j/src/main/java/com/cisco/oss/foundation/logging/transactions/ConfigurationUtil.java +++ b/logging-log4j/src/main/java/com/cisco/oss/foundation/logging/transactions/ConfigurationUtil.java @@ -2,6 +2,9 @@ import com.cisco.oss.foundation.configuration.ConfigurationFactory; import com.cisco.oss.foundation.configuration.FoundationConfigurationListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.env.Environment; /** * Created by Nuna on 27/10/2015. @@ -10,8 +13,12 @@ public enum ConfigurationUtil implements FoundationConfigurationListener { INSTANCE; + private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationUtil.class); + private boolean verbose = false; + private Environment environment; + ConfigurationUtil() { refresh(); } @@ -22,10 +29,32 @@ public void configurationChanged() { } private void refresh() { - verbose = ConfigurationFactory.getConfiguration().getBoolean("logging.verbose", false); + if (environment == null) { + verbose = ConfigurationFactory.getConfiguration().getBoolean("logging.verbose", false); + }else{ + verbose = Boolean.valueOf(environment.getProperty("logging.verbose", "false")); + } } public boolean isVerbose() { return verbose; } + + public void setEnvironment(Environment environment){ + this.environment = environment; + } + + public static void setConfigSource(Environment environment) { + boolean configuredReady = false; + try{ + ConfigurationFactory.getConfiguration(); + configuredReady = true; + }catch (Exception e){ + LOGGER.debug("can't load configuration. Error is: " + e); + } + + if (environment != null && !configuredReady){ + INSTANCE.setEnvironment(environment); + } + } } diff --git a/logging-log4j/src/main/java/com/cisco/oss/foundation/logging/transactions/HttpLogger.java b/logging-log4j/src/main/java/com/cisco/oss/foundation/logging/transactions/HttpLogger.java index f6cf802..942567f 100644 --- a/logging-log4j/src/main/java/com/cisco/oss/foundation/logging/transactions/HttpLogger.java +++ b/logging-log4j/src/main/java/com/cisco/oss/foundation/logging/transactions/HttpLogger.java @@ -1,10 +1,16 @@ package com.cisco.oss.foundation.logging.transactions; +import com.cisco.oss.foundation.configuration.ConfigurationFactory; +import org.apache.commons.configuration.Configuration; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Level; import org.slf4j.Logger; import com.cisco.oss.foundation.flowcontext.FlowContextFactory; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.*; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.core.MultivaluedMap; @@ -22,10 +28,23 @@ * @author abrandwi * */ +@org.springframework.stereotype.Component public class HttpLogger extends TransactionLogger { - private enum HttpPropertyKey {Method, SourceName, SourcePort, URL, ResponseStatusCode, ResponseContentLength, ResponseBody}; - private enum HttpVerbosePropertyKey {RequestHeaders, RequestBody, ResponseHeaders, ResponseBody}; + + + private enum HttpPropertyKey {Method, SourceName, SourcePort, URL, ResponseStatusCode, ResponseContentLength, ResponseBody;}; + private enum HttpVerbosePropertyKey {RequestHeaders, RequestBody, ResponseHeaders, ResponseBody;}; + + private static final Logger LOGGER = LoggerFactory.getLogger(HttpLogger.class); + + @Autowired + private Environment environment; + + public HttpLogger(){ + ConfigurationUtil.setConfigSource(environment); + } + // Those headers will be used when we'll add sourceIdentity to properties // private static final String SOURCE_TYPE_HEADER = "Source-Type"; // Cisco header (used by UPM): Should contain the type of machine sent the request (e.g. UPM, TSTVM) - currently won't be sent to PPS diff --git a/logging-log4j/src/main/java/com/cisco/oss/foundation/logging/transactions/HttpSpringLogger.java b/logging-log4j/src/main/java/com/cisco/oss/foundation/logging/transactions/HttpSpringLogger.java index a738992..6d9445f 100644 --- a/logging-log4j/src/main/java/com/cisco/oss/foundation/logging/transactions/HttpSpringLogger.java +++ b/logging-log4j/src/main/java/com/cisco/oss/foundation/logging/transactions/HttpSpringLogger.java @@ -1,10 +1,15 @@ package com.cisco.oss.foundation.logging.transactions; +import com.cisco.oss.foundation.configuration.ConfigurationFactory; import com.google.common.base.Joiner; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Level; import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.*; import javax.servlet.http.HttpServletRequest; import java.util.Enumeration; @@ -17,12 +22,20 @@ * * @author abrandwi */ +@org.springframework.stereotype.Component public class HttpSpringLogger extends TransactionLogger { private enum HttpPropertyKey {Summary, Method, SourceName, SourcePort, URL, ResponseStatusCode, ResponseContentLength, ResponseBody} + private enum HttpVerbosePropertyKey {RequestHeaders, RequestBody, ResponseHeaders, ResponseBody} + private static final Logger LOGGER = LoggerFactory.getLogger(HttpSpringLogger.class); - private enum HttpVerbosePropertyKey {RequestHeaders, RequestBody, ResponseHeaders, ResponseBody} + @Autowired + private Environment environment; + + public HttpSpringLogger(){ + ConfigurationUtil.setConfigSource(environment); + } // Those headers will be used when we'll add sourceIdentity to properties diff --git a/logging-log4j/src/main/java/com/cisco/oss/foundation/logging/transactions/MessageQueueLogger.java b/logging-log4j/src/main/java/com/cisco/oss/foundation/logging/transactions/MessageQueueLogger.java index 278a7e3..f85bff3 100644 --- a/logging-log4j/src/main/java/com/cisco/oss/foundation/logging/transactions/MessageQueueLogger.java +++ b/logging-log4j/src/main/java/com/cisco/oss/foundation/logging/transactions/MessageQueueLogger.java @@ -3,12 +3,16 @@ import org.apache.commons.lang.StringUtils; import org.apache.log4j.Level; import org.slf4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.*; /** * Class for MessageQueue transactions logging * @author abrandwi * */ +@org.springframework.stereotype.Component public class MessageQueueLogger extends TransactionLogger { private enum MessageQueuePropertyKey {NotificationType}; @@ -16,6 +20,13 @@ private enum MessageQueueVerbosePropertyKey {NotificationBody}; private String notificationType; + @Autowired + private Environment environment; + + public MessageQueueLogger(){ + ConfigurationUtil.setConfigSource(environment); + } + public static void start(final Logger logger, final Logger auditor, String notification) { if(!createLoggingAction(logger, auditor, new MessageQueueLogger())) { return;