Skip to content

Commit

Permalink
minor edits in handling request context data
Browse files Browse the repository at this point in the history
  • Loading branch information
jmezna committed Jul 10, 2019
1 parent 693d14a commit 00175b9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
22 changes: 11 additions & 11 deletions fluentd/src/main/java/com/kumuluz/ee/logs/FluentdLogCommons.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,8 @@ private void log(LogLevel level, Marker marker, Marker parentMarker, LogMessage
}

String markerString = parentMarker == null ? marker.toString() : marker + "[ " + parentMarker + " ]";
HashMap<String, Object> context = new HashMap<>();

context = addContextData(context);
HashMap<String, Object> context = getRequestContextData();

if (logMessage != null && logMessage.getFields() != null) {
context.put("level", FluentdLogUtil.convertToFluentdLevel(level).getName());
Expand All @@ -220,27 +219,28 @@ private void log(LogLevel level, Marker marker, Marker parentMarker, LogMessage
}

/**
* @param data defines input data
* @return data with appended context data
* @return request context data from external RequestContext objects
*/
private HashMap<String, Object> addContextData(HashMap<String, Object> data) {
private HashMap<String, Object> getRequestContextData() {

// FluentdConfig data
FluentdConfig fluentdConfig = FluentdConfig.getInstance();

data.put("environmentType", fluentdConfig.getEnvironmentType());
data.put("applicationName", fluentdConfig.getApplicationName());
data.put("applicationVersion", fluentdConfig.getApplicationVersion());
data.put("uniqueInstanceId", fluentdConfig.getUniqueInstanceId());
HashMap<String, Object> requestContextData = new HashMap<>();

requestContextData.put("environmentType", fluentdConfig.getEnvironmentType());
requestContextData.put("applicationName", fluentdConfig.getApplicationName());
requestContextData.put("applicationVersion", fluentdConfig.getApplicationVersion());
requestContextData.put("uniqueInstanceId", fluentdConfig.getUniqueInstanceId());

ServiceLoader.load(RequestContext.class).forEach(provider -> {
HashMap<String, String> context = provider.getContext();
if (context != null) {
data.putAll(context);
requestContextData.putAll(context);
}
});

return data;
return requestContextData;
}

}
28 changes: 17 additions & 11 deletions log4j2/src/main/java/com/kumuluz/ee/logs/Log4j2LogCommons.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
* out of or in connection with the software or the use or other dealings in the
* software. See the License for the specific language governing permissions and
* limitations under the License.
*/
*/

package com.kumuluz.ee.logs;

import com.kumuluz.ee.common.config.EeConfig;
import com.kumuluz.ee.common.runtime.EeRuntime;
import com.kumuluz.ee.logs.enums.LogLevel;
import com.kumuluz.ee.logs.markers.CommonsMarker;
import com.kumuluz.ee.logs.markers.Marker;
Expand Down Expand Up @@ -168,10 +170,6 @@ private void logEnd(LogResourceContext logResourceContext) {
* @param logMessage object defining LogMessage
*/
private void log(LogLevel level, Marker marker, Marker parentMarker, LogMessage logMessage) {
HashMap<String, String> context = new HashMap<>();

context = addContextData(context);


String message;
if (logMessage == null || logMessage.getMessage() == null) {
Expand All @@ -181,7 +179,7 @@ private void log(LogLevel level, Marker marker, Marker parentMarker, LogMessage
}

if (logMessage != null && logMessage.getFields() != null) {
CloseableThreadContext.putAll(context);
CloseableThreadContext.putAll(getRequestContextData());
try (final CloseableThreadContext.Instance ctc = CloseableThreadContext.putAll(logMessage.getFields())) {
logger.log(Log4j2LogUtil.convertToLog4j2Level(level), MarkerManager.getMarker(marker.toString())
.setParents(MarkerManager.getMarker(parentMarker.toString())), message);
Expand All @@ -194,18 +192,26 @@ private void log(LogLevel level, Marker marker, Marker parentMarker, LogMessage
}

/**
* @param data defines input data
* @return data with appended context data
* @return request context data from external RequestContext objects
*/
private HashMap<String, String> addContextData(HashMap<String, String> data) {
private HashMap<String, String> getRequestContextData() {

EeConfig eeConfig = EeConfig.getInstance();

HashMap<String, String> requestContextData = new HashMap<>();

requestContextData.put("environmentType", eeConfig.getEnv().getName());
requestContextData.put("applicationName", eeConfig.getName());
requestContextData.put("applicationVersion", eeConfig.getVersion());
requestContextData.put("uniqueInstanceId", EeRuntime.getInstance().getInstanceId());

ServiceLoader.load(RequestContext.class).forEach(provider -> {
HashMap<String, String> context = provider.getContext();
if (context != null) {
data.putAll(context);
requestContextData.putAll(context);
}
});

return data;
return requestContextData;
}
}

0 comments on commit 00175b9

Please sign in to comment.