Skip to content

Commit

Permalink
Add Support for Spring Environment instead of CommonsConfig for sprin…
Browse files Browse the repository at this point in the history
…g boot applications (fix #19)
  • Loading branch information
Yair Ogen committed Aug 4, 2016
1 parent 5ca2311 commit 53b7169
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
}
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,30 @@
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};
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;
Expand Down

0 comments on commit 53b7169

Please sign in to comment.