Skip to content

Commit

Permalink
Merge pull request #2764 from cloudfoundry/klaus-sap-patch-1
Browse files Browse the repository at this point in the history
more methods for sanitized logging
  • Loading branch information
klaus-sap authored Mar 7, 2024
2 parents 73979d3 + c57dad7 commit e5e64b7
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
*/
public class SanitizedLogFactory {

private SanitizedLogFactory() {
}
private SanitizedLogFactory() { }

public static SanitizedLog getLog(Class<?> clazz) {
return new SanitizedLog(LogManager.getLogger(clazz));
Expand All @@ -32,12 +31,24 @@ public void info(String message) {
}
}

public void info(String message, Throwable t) {
if (fallback.isInfoEnabled()) {
fallback.info(LogSanitizerUtil.sanitize(message), t);
}
}

public void warn(String message) {
if (fallback.isWarnEnabled()) {
fallback.warn(LogSanitizerUtil.sanitize(message));
}
}

public void warn(String message, Throwable t) {
if (fallback.isWarnEnabled()) {
fallback.warn(LogSanitizerUtil.sanitize(message), t);
}
}

public void debug(String message) {
if (fallback.isDebugEnabled()) {
fallback.debug(LogSanitizerUtil.sanitize(message));
Expand All @@ -49,5 +60,29 @@ public void debug(String message, Throwable t) {
fallback.debug(LogSanitizerUtil.sanitize(message), t);
}
}

public void error(String message) {
if (fallback.isErrorEnabled()) {
fallback.error(LogSanitizerUtil.sanitize(message));
}
}

public void error(String message, Throwable t) {
if (fallback.isErrorEnabled()) {
fallback.error(LogSanitizerUtil.sanitize(message), t);
}
}

public void trace(String message) {
if (fallback.isTraceEnabled()) {
fallback.trace(LogSanitizerUtil.sanitize(message));
}
}

public void trace(String message, Throwable t) {
if (fallback.isTraceEnabled()) {
fallback.trace(LogSanitizerUtil.sanitize(message), t);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,77 +1,155 @@
package org.cloudfoundry.identity.uaa.logging;

import org.apache.logging.log4j.Logger;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.apache.commons.lang3.RandomStringUtils;

public class SanitizedLogFactoryTest {

private final String dirtyMessage = "one\ntwo\tthree\rfour";
private final String sanitizedMsg = "one|two|three|four[SANITIZED]";
private final String cleanMessage = "one two three four";

Logger mockLog;
SanitizedLogFactory.SanitizedLog log;
Exception ex;

@Before
public void setUp() {
mockLog = mock(Logger.class);
log = new SanitizedLogFactory.SanitizedLog(mockLog);
ex = new Exception(RandomStringUtils.randomAlphanumeric(8));
}

@Test
public void testSanitizeDebug() {
SanitizedLogFactory.SanitizedLog log = new SanitizedLogFactory.SanitizedLog(mockLog);
doReturn(true).when(mockLog).isDebugEnabled();
log.debug("one\ntwo\tthree\rfour");
verify(mockLog).debug("one|two|three|four[SANITIZED]");
public void testInit() {
Assert.assertNotNull(SanitizedLogFactory.getLog(SanitizedLogFactoryTest.class));
}

@Test
public void testSanitizeDebugCleanMessage() {
SanitizedLogFactory.SanitizedLog log = new SanitizedLogFactory.SanitizedLog(mockLog);
doReturn(true).when(mockLog).isDebugEnabled();
log.debug("one two three four");
verify(mockLog).debug("one two three four");
public void testSanitizeInfo() {
when(mockLog.isInfoEnabled()).thenReturn(true);
log.info(dirtyMessage);
verify(mockLog).info(sanitizedMsg);
log.info(dirtyMessage, ex);
verify(mockLog).info(sanitizedMsg, ex);
}

@Test
public void testSanitizeDebugCleanMessageException() {
SanitizedLogFactory.SanitizedLog log = new SanitizedLogFactory.SanitizedLog(mockLog);
doReturn(true).when(mockLog).isDebugEnabled();
Exception exception = new Exception("");
log.debug("one two three four", exception);
verify(mockLog).debug("one two three four", exception);
public void testSanitizeInfoCleanMessage() {
when(mockLog.isInfoEnabled()).thenReturn(true);
log.info(cleanMessage);
verify(mockLog).info(cleanMessage);
log.info(cleanMessage, ex);
verify(mockLog).info(cleanMessage, ex);
}

@Test
public void testSanitizeInfo() {
SanitizedLogFactory.SanitizedLog log = new SanitizedLogFactory.SanitizedLog(mockLog);
doReturn(true).when(mockLog).isInfoEnabled();
log.info("one\ntwo\tthree\rfour");
verify(mockLog).info("one|two|three|four[SANITIZED]");
public void testSanitizeDebug() {
when(mockLog.isDebugEnabled()).thenReturn(true);
log.debug(dirtyMessage);
verify(mockLog).debug(sanitizedMsg);
log.debug(dirtyMessage, ex);
verify(mockLog).debug(sanitizedMsg, ex);
}

@Test
public void testSanitizeInfoCleanMessage() {
SanitizedLogFactory.SanitizedLog log = new SanitizedLogFactory.SanitizedLog(mockLog);
doReturn(true).when(mockLog).isInfoEnabled();
log.info("one two three four");
verify(mockLog).info("one two three four");
public void testSanitizeDebugCleanMessage() {
when(mockLog.isDebugEnabled()).thenReturn(true);
log.debug(cleanMessage);
verify(mockLog).debug(cleanMessage);
log.debug(cleanMessage, ex);
verify(mockLog).debug(cleanMessage, ex);
}

@Test
public void testSanitizeDebugCleanMessageNotEnabled() {
when(mockLog.isDebugEnabled()).thenReturn(false);
log.debug(cleanMessage);
verify(mockLog, never()).debug(cleanMessage);
log.debug(cleanMessage, ex);
verify(mockLog, never()).debug(cleanMessage, ex);
Assert.assertFalse(log.isDebugEnabled());
}


@Test
public void testSanitizeWarn() {
SanitizedLogFactory.SanitizedLog log = new SanitizedLogFactory.SanitizedLog(mockLog);
doReturn(true).when(mockLog).isWarnEnabled();
log.warn("one\ntwo\tthree\rfour");
verify(mockLog).warn("one|two|three|four[SANITIZED]");
when(mockLog.isWarnEnabled()).thenReturn(true);
log.warn(dirtyMessage);
verify(mockLog).warn(sanitizedMsg);
log.warn(dirtyMessage, ex);
verify(mockLog).warn(sanitizedMsg, ex);
}

@Test
public void testSanitizeWarnCleanMessage() {
SanitizedLogFactory.SanitizedLog log = new SanitizedLogFactory.SanitizedLog(mockLog);
doReturn(true).when(mockLog).isWarnEnabled();
log.warn("one two three four");
verify(mockLog).warn("one two three four");
when(mockLog.isWarnEnabled()).thenReturn(true);
log.warn(cleanMessage);
verify(mockLog).warn(cleanMessage);
log.warn(cleanMessage, ex);
verify(mockLog).warn(cleanMessage, ex);
}

}
@Test
public void testSanitizeWarnCleanMessageNotEnabled() {
when(mockLog.isWarnEnabled()).thenReturn(false);
log.warn(cleanMessage);
verify(mockLog, never()).warn(cleanMessage);
log.warn(cleanMessage, ex);
verify(mockLog, never()).warn(cleanMessage, ex);
}

@Test
public void testSanitizeError() {
when(mockLog.isErrorEnabled()).thenReturn(true);
log.error(dirtyMessage);
verify(mockLog).error(sanitizedMsg);
log.error(dirtyMessage, ex);
verify(mockLog).error(sanitizedMsg, ex);
}

@Test
public void testSanitizeErrorCleanMessage() {
when(mockLog.isErrorEnabled()).thenReturn(true);
log.error(cleanMessage);
verify(mockLog).error(cleanMessage);
log.error(cleanMessage, ex);
verify(mockLog).error(cleanMessage, ex);
}

@Test
public void testSanitizeTrace() {
when(mockLog.isTraceEnabled()).thenReturn(true);
log.trace(dirtyMessage);
verify(mockLog).trace(sanitizedMsg);
log.trace(dirtyMessage, ex);
verify(mockLog).trace(sanitizedMsg, ex);
}

@Test
public void testSanitizeTraceCleanMessage() {
when(mockLog.isTraceEnabled()).thenReturn(true);
log.trace(cleanMessage);
verify(mockLog).trace(cleanMessage);
log.trace(cleanMessage, ex);
verify(mockLog).trace(cleanMessage, ex);
}

@Test
public void testSanitizeTraceCleanMessageWhenNotEnabled() {
when(mockLog.isTraceEnabled()).thenReturn(false);
log.trace(cleanMessage);
verify(mockLog, never()).trace(cleanMessage);
log.trace(cleanMessage, ex);
verify(mockLog, never()).trace(cleanMessage, ex);
}
}

0 comments on commit e5e64b7

Please sign in to comment.