Skip to content

Commit

Permalink
more tests and rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
lmolkova committed Nov 24, 2021
1 parent d1dbb87 commit fc7aa8f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 38 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

import com.azure.core.util.CoreUtils;

import java.util.Arrays;

/**
* Contains utility methods for logging.
*/
public final class LoggingUtils {
private static final char CR = '\r';
private static final char LF = '\n';

private LoggingUtils() {
}

Expand Down Expand Up @@ -48,4 +50,29 @@ public static String removeNewLinesFromLogMessage(String logMessage) {
sb.append(logMessage, prevStart, logMessage.length());
return sb.toString();
}

/*
* Determines if the arguments contains a throwable that would be logged, SLF4J logs a throwable if it is the last
* element in the argument list.
*
* @param args The arguments passed to format the log message.
* @return True if the last element is a throwable, false otherwise.
*/
public static boolean doesArgsHaveThrowable(Object... args) {
if (args.length == 0) {
return false;
}

return args[args.length - 1] instanceof Throwable;
}

/*
* Removes the last element from the arguments as it is a throwable.
*
* @param args The arguments passed to format the log message.
* @return The arguments with the last element removed.
*/
public static Object[] removeThrowable(Object... args) {
return Arrays.copyOf(args, args.length - 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

import static com.azure.core.implementation.logging.LoggingUtils.removeNewLinesFromLogMessage;

import static com.azure.core.implementation.logging.LoggingUtil.doesArgsHaveThrowable;
import static com.azure.core.implementation.logging.LoggingUtil.removeThrowable;
import static com.azure.core.implementation.logging.LoggingUtils.doesArgsHaveThrowable;
import static com.azure.core.implementation.logging.LoggingUtils.removeThrowable;

/**
* This is a fluent logger helper class that wraps a pluggable {@link Logger}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import java.util.Objects;
import java.util.function.Supplier;

import static com.azure.core.implementation.logging.LoggingUtil.doesArgsHaveThrowable;
import static com.azure.core.implementation.logging.LoggingUtil.removeThrowable;
import static com.azure.core.implementation.logging.LoggingUtils.doesArgsHaveThrowable;
import static com.azure.core.implementation.logging.LoggingUtils.removeThrowable;

/**
* This class provides fluent API to write logs using {@link ClientLogger} and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,10 @@ public void logWithContextNullMessage() {
}

/**
* Tests that newline is removed from the message, keys and values.
* Tests that newline is escaped in message, keys and values.
*/
@Test
public void logWithContextNewLineIsReplaced() {
public void logWithContextNewLineIsEscaped() {
setupLogLevel(LogLevel.VERBOSE.getLogLevel());
ClientLogger logger = new ClientLogger(ClientLoggerTests.class);

Expand Down Expand Up @@ -468,7 +468,7 @@ public void logWithContextValueSupplier() {
logger.atWarning()
// this is technically invalid, but we should not throw because of logging in runtime
.addKeyValue("connectionId", (Supplier<String>) null)
.addKeyValue("linkName", String.format("complex value %s", 123))
.addKeyValue("linkName", () -> String.format("complex value %s", 123))
.log("test");

assertMessage(
Expand Down Expand Up @@ -528,6 +528,38 @@ public void logWithContextWithThrowableInArgs(LogLevel logLevelToConfigure) {
LogLevel.WARNING);
}

/**
* Tests json escape in keys, values, message and exception message
*/
@ParameterizedTest
@MethodSource("provideLogLevels")
public void logWithContextWithThrowableInArgsAndEscaping(LogLevel logLevelToConfigure) {
setupLogLevel(logLevelToConfigure.getLogLevel());
ClientLogger logger = new ClientLogger(ClientLoggerTests.class);

String exceptionMessage = "An exception \tmessage with \"special characters\"\r\n";
RuntimeException runtimeException = createIllegalStateException(exceptionMessage);

logger.atWarning()
.addKeyValue("connection\tId", "foo")
.addKeyValue("linkName", "\rbar")
.log("hello {}, \"and\" {more}", "world", runtimeException);


String escapedExceptionMessage = "An exception \\tmessage with \\\"special characters\\\"\\r\\n";

String expectedMessage = "{\"az.sdk.message\":\"hello world, \\\"and\\\" {more}\",\"exception\":\"" + escapedExceptionMessage + "\",\"connection\\tId\":\"foo\",\"linkName\":\"\\rbar\"}";
if (logLevelToConfigure.equals(LogLevel.VERBOSE)) {
expectedMessage += System.lineSeparator() + runtimeException.toString() + System.lineSeparator() + "\tat " + runtimeException.getStackTrace()[0].toString();
}

assertMessage(
expectedMessage,
byteArraySteamToString(logCaptureStream),
logLevelToConfigure,
LogLevel.WARNING);
}

/**
* Tests logging with context when cause is set
*/
Expand Down

0 comments on commit fc7aa8f

Please sign in to comment.