From d0017b73e8dcd2e9b41fe2ee0e89b5bd36fb0353 Mon Sep 17 00:00:00 2001 From: Thomas <> Date: Thu, 13 Oct 2022 07:30:27 +0200 Subject: feat(iaiklogger): add interface to re-classify log msg from WARN to INFO --- .../moa/spss/server/logging/IaikLog.java | 30 ++++++++++++++++- .../moa/spss/logger/IaikLoggerMaskingTest.java | 38 ++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) (limited to 'moaSig/moa-sig-lib/src') diff --git a/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/server/logging/IaikLog.java b/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/server/logging/IaikLog.java index 84dc8bf..2ddb783 100644 --- a/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/server/logging/IaikLog.java +++ b/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/server/logging/IaikLog.java @@ -24,11 +24,14 @@ package at.gv.egovernment.moa.spss.server.logging; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.IntStream; +import java.util.stream.Stream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -50,11 +53,29 @@ public class IaikLog implements iaik.logging.Log { /** The node ID to use. */ private String nodeId; + private static final Set LOGLEVEL_INFO_RECLASSIFICATION = Stream.of( + "Max. cert info store size exceeded, consider using a larger certinfostore.") + .collect(Collectors.toCollection(HashSet::new)); + + public static final String X509_INFO_CLEARING_PATTERN = "(?!serialNumber)(=)(.*?)(,|\"|$)"; private static Pattern multilinePattern; private static List maskPatterns = new ArrayList<>(); + /** + * Add log message that should be logged on INFO level instead of WARN. + * + *

IAIK-MOA and some other IAIK libs sometimes log on level WARN but it's only an info. + * However, log level WARN can trigger wrong alerts in monitoring systems.

+ * + * @param msg + */ + public static void addLogMsgForReclassification(String msg) { + LOGLEVEL_INFO_RECLASSIFICATION.add(msg); + + } + /** * Add masking pattern into logger. * @@ -130,7 +151,14 @@ public class IaikLog implements iaik.logging.Log { Object blankedMsg = log.isTraceEnabled() ? message : maskMessage(message); final IaikLogMsg msg = new IaikLogMsg(transactionId, nodeId, blankedMsg); - log.warn(msg.toString(), t); + // log some messages on INFO. That's a work-around for suboptimal levels in third-party libs. + if (LOGLEVEL_INFO_RECLASSIFICATION.contains(blankedMsg)) { + log.info(msg.toString(), t); + + } else { + log.warn(msg.toString(), t); + + } } /** diff --git a/moaSig/moa-sig-lib/src/test/java/test/at/gv/egovernment/moa/spss/logger/IaikLoggerMaskingTest.java b/moaSig/moa-sig-lib/src/test/java/test/at/gv/egovernment/moa/spss/logger/IaikLoggerMaskingTest.java index b3bf0e8..da8a8aa 100644 --- a/moaSig/moa-sig-lib/src/test/java/test/at/gv/egovernment/moa/spss/logger/IaikLoggerMaskingTest.java +++ b/moaSig/moa-sig-lib/src/test/java/test/at/gv/egovernment/moa/spss/logger/IaikLoggerMaskingTest.java @@ -171,7 +171,45 @@ public class IaikLoggerMaskingTest { } + @Test + public void noMsgReclassification() { + String msg = RandomStringUtils.randomAlphanumeric(25); + + //test + log.warn(transId, msg, null); + + //verify log + assertTrue("Log Msg on Level WARN", verifyMsgOnLevel(Level.WARN, msg)); + + } + @Test + public void msgReclassification() { + String msg1 = "Max. cert info store size exceeded, consider using a larger certinfostore."; + String msg2 = "my new test mgs"; + IaikLog.addLogMsgForReclassification(msg2); + + //test + log.warn(transId, msg1, null); + log.warn(transId, msg2, null); + + //verify log + assertFalse("Log Msg on wrong", verifyMsgOnLevel(Level.WARN, msg1)); + assertTrue("Log Msg on wrong", verifyMsgOnLevel(Level.INFO, msg1)); + + assertFalse("Log Msg on wrong", verifyMsgOnLevel(Level.WARN, msg2)); + assertTrue("Log Msg on wrong", verifyMsgOnLevel(Level.INFO, msg2)); + + } + + private boolean verifyMsgOnLevel(Level level, String msg) { + return memoryAppender.getLoggedEvents().stream() + .filter(el -> el.getLevel().equals(level)) + .filter(el -> el.getMessage().contains(msg)) + .findFirst() + .isPresent(); + } + private void verifyLogMessge(List checks) { assertEquals("no log", 1, memoryAppender.getSize()); checks.stream().forEach( -- cgit v1.2.3