summaryrefslogtreecommitdiff
path: root/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/ajp/logging
diff options
context:
space:
mode:
authorThomas Lenz <thomas.lenz@egiz.gv.at>2020-12-25 21:03:24 +0100
committerThomas Lenz <thomas.lenz@egiz.gv.at>2020-12-25 21:03:24 +0100
commit7b83b319fce24faf12a3d69db9ccce87d0dde4f6 (patch)
treea1772907af9e12feff748bc68062d0e89e1cea97 /eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/ajp/logging
parenta378db97e9f14bfa81bd490186c065d77f5806c7 (diff)
parenta3b7c488300b9b231959663bbd2dd96a4a452ec0 (diff)
downloadEAAF-Components-7b83b319fce24faf12a3d69db9ccce87d0dde4f6.tar.gz
EAAF-Components-7b83b319fce24faf12a3d69db9ccce87d0dde4f6.tar.bz2
EAAF-Components-7b83b319fce24faf12a3d69db9ccce87d0dde4f6.zip
Merge branch 'feature/add_springboot_commons' into 'nightlyBuild'
Feature/add springboot commons See merge request egiz/eaaf_components!8
Diffstat (limited to 'eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/ajp/logging')
-rw-r--r--eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/ajp/logging/LoggingProperties.java94
-rw-r--r--eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/ajp/logging/MdcEnhancerFilter.java99
2 files changed, 193 insertions, 0 deletions
diff --git a/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/ajp/logging/LoggingProperties.java b/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/ajp/logging/LoggingProperties.java
new file mode 100644
index 00000000..b3d5d846
--- /dev/null
+++ b/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/ajp/logging/LoggingProperties.java
@@ -0,0 +1,94 @@
+package at.gv.egiz.eaaf.utils.springboot.ajp.logging;
+
+import java.util.Collections;
+import java.util.List;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * Logger configuration for embedded Tomcat.
+ *
+ * @author BRZ development team
+ * @author tlenz
+ *
+ */
+@ConfigurationProperties(prefix = "logging")
+@Getter
+@Setter
+public class LoggingProperties {
+ /**
+ * Whether to log in JSON format.
+ */
+ private boolean json = true;
+ /**
+ * Whether to log in plain text.
+ */
+ private boolean text = false;
+ /**
+ * Default Logback Pattern.
+ */
+ private String pattern = "### unused property ###";
+ /**
+ * Logback Mapped Diagnostic Context.
+ */
+ private Mdc mdc = new Mdc();
+
+ /**
+ * Logback Mapped Diagnostic Context.
+ */
+
+ @Getter
+ @Setter
+ public static class Mdc {
+ /**
+ * Whether to use Logback's MDC.
+ */
+ private boolean enabled = false;
+ /**
+ * List of HTTP Headers to make available in Logback's MDC.
+ */
+ private List<String> headers = Collections.emptyList();
+ private String headerPrefix = "";
+ private String headerPostfix = "";
+ /**
+ * List of HTTP Cookies to make available in Logback's MDC.
+ */
+ private List<String> cookies = Collections.emptyList();
+ private String cookiePrefix = "";
+ private String cookiePostfix = "";
+ /**
+ * List of HTTP Session Attributes to make available in Logback's MDC.
+ */
+ private List<String> sessionAttributes = Collections.emptyList();
+ private String sessionAttributePrefix = "";
+ private String sessionAttributePostfix = "";
+ /**
+ * Value to use if a configured MDC entry would be null.
+ */
+ private String nullValue = null;
+ }
+
+ /**
+ * Tomcat AccessLog.
+ */
+ private AccessLog accessLog = new AccessLog();
+
+ /**
+ * Tomcat AccessLog.
+ */
+ @Getter
+ @Setter
+ public static class AccessLog {
+ /**
+ * Enable AccessLog.
+ */
+ private boolean enabled = false;
+ /**
+ * Logback access log filename.
+ */
+ private String filename = "logback-access.xml";
+ }
+}
diff --git a/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/ajp/logging/MdcEnhancerFilter.java b/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/ajp/logging/MdcEnhancerFilter.java
new file mode 100644
index 00000000..d63c47c9
--- /dev/null
+++ b/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/ajp/logging/MdcEnhancerFilter.java
@@ -0,0 +1,99 @@
+package at.gv.egiz.eaaf.utils.springboot.ajp.logging;
+
+import java.io.IOException;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.MDC;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+@Component
+@EnableConfigurationProperties(LoggingProperties.class)
+public class MdcEnhancerFilter implements Filter {
+
+ /**
+ * Logging properties.
+ */
+ @Autowired
+ private LoggingProperties loggingProperties;
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse,
+ final FilterChain filterChain) throws IOException, ServletException {
+ final HttpServletRequest request = (HttpServletRequest) servletRequest;
+
+ String key;
+ String value;
+ for (final String header : loggingProperties.getMdc().getHeaders()) {
+ key = loggingProperties.getMdc().getHeaderPrefix() + header + loggingProperties.getMdc()
+ .getHeaderPostfix();
+ value = request.getHeader(header);
+ if (!StringUtils.isEmpty(value)) {
+ MDC.put(key, value);
+ } else if (loggingProperties.getMdc().getNullValue() != null) {
+ MDC.put(key, loggingProperties.getMdc().getNullValue());
+ }
+ }
+
+ for (final String cookie : loggingProperties.getMdc().getCookies()) {
+ key = loggingProperties.getMdc().getCookiePrefix() + cookie + loggingProperties.getMdc()
+ .getCookiePostfix();
+ value = getCookie(cookie, request.getCookies());
+ if (!StringUtils.isEmpty(value)) {
+ MDC.put(key, value);
+ } else if (loggingProperties.getMdc().getNullValue() != null) {
+ MDC.put(key, loggingProperties.getMdc().getNullValue());
+ }
+ }
+
+ Object object;
+ for (final String attribute : loggingProperties.getMdc().getSessionAttributes()) {
+ key = loggingProperties.getMdc().getSessionAttributePrefix() + attribute + loggingProperties.getMdc()
+ .getSessionAttributePostfix();
+ object = request.getSession(true).getAttribute(attribute);
+ if (object != null) {
+ MDC.put(key, object.toString());
+ } else if (loggingProperties.getMdc().getNullValue() != null) {
+ MDC.put(key, loggingProperties.getMdc().getNullValue());
+ }
+ }
+
+ try {
+ filterChain.doFilter(servletRequest, servletResponse);
+ } finally {
+ for (final String header : loggingProperties.getMdc().getHeaders()) {
+ MDC.remove(header);
+ }
+ for (final String cookie : loggingProperties.getMdc().getCookies()) {
+ MDC.remove(cookie);
+ }
+ for (final String attribute : loggingProperties.getMdc().getSessionAttributes()) {
+ MDC.remove(attribute);
+ }
+ }
+ }
+
+ private static String getCookie(final String cookie, final Cookie[] cookies) {
+ if (cookies == null || StringUtils.isEmpty(cookie)) {
+ return null;
+ }
+ for (final Cookie c : cookies) {
+ if (c.getName().equals(cookie)) {
+ return c.getValue();
+ }
+ }
+ return null;
+ }
+} \ No newline at end of file