Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.UUID;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.test.layout.LogEventFixtures;
import org.junit.jupiter.api.Test;

public class UuidTest {
Expand Down Expand Up @@ -107,6 +109,20 @@ public void testThreads() throws Exception {
assertEquals(0, errors, errors + " duplicate UUIDS");
}

@Test
public void testHashBasedUuid() {
LogEvent event1 = LogEventFixtures.createLogEvent();
LogEvent event2 = LogEventFixtures.createLogEvent()
.asBuilder()
.setThrown(event1.getThrown())
.build();
UUID uuid1 = UuidUtil.getHashBasedUuid(event1);
UUID uuid2 = UuidUtil.getHashBasedUuid(event2);
assertEquals(uuid1, uuid2, "UUIDs don't match");
assertEquals(8, uuid1.version(), "Wrong version");
assertEquals(2, uuid1.variant(), "Wrong variant");
}

private static class Worker extends Thread {

private final UUID[] uuids;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,40 +29,60 @@
@Plugin("UuidPatternConverter")
@ConverterKeys({"u", "uuid"})
public final class UuidPatternConverter extends LogEventPatternConverter {
private enum UuidType {
TIME,
RANDOM,
HASH
}

private final boolean isRandom;
private final UuidType uuidType;

/**
* Private constructor.
*/
private UuidPatternConverter(final boolean isRandom) {
private UuidPatternConverter(final UuidType uuidType) {
super("u", "uuid");
this.isRandom = isRandom;
this.uuidType = uuidType;
}

/**
* Obtains an instance of SequencePatternConverter.
* Obtains an instance of UuidPatternConverter.
*
* @param options options, currently ignored, may be null.
* @return instance of SequencePatternConverter.
* @param options options
* @return instance of UuidPatternConverter.
*/
public static UuidPatternConverter newInstance(final String[] options) {
if (options.length == 0) {
return new UuidPatternConverter(false);
return new UuidPatternConverter(UuidType.TIME);
}

if (options.length > 1 || (!options[0].equalsIgnoreCase("RANDOM") && !options[0].equalsIgnoreCase("Time"))) {
LOGGER.error("UUID Pattern Converter only accepts a single option with the value \"RANDOM\" or \"TIME\"");
if (options.length == 1) {
switch (options[0].toUpperCase()) {
case "TIME":
return new UuidPatternConverter(UuidType.TIME);
case "RANDOM":
return new UuidPatternConverter(UuidType.RANDOM);
case "HASH":
return new UuidPatternConverter(UuidType.HASH);
}
}
return new UuidPatternConverter(options[0].equalsIgnoreCase("RANDOM"));

LOGGER.error(
"UUID Pattern Converter only accepts a single option with the value \"TIME\" or \"RANDOM\" or \"HASH\"");
return new UuidPatternConverter(UuidType.TIME);
}

/**
* {@inheritDoc}
*/
@Override
public void format(final LogEvent event, final StringBuilder toAppendTo) {
final UUID uuid = isRandom ? UUID.randomUUID() : UuidUtil.getTimeBasedUuid();
final UUID uuid =
switch (uuidType) {
case TIME -> UuidUtil.getTimeBasedUuid();
case RANDOM -> UUID.randomUUID();
case HASH -> UuidUtil.getHashBasedUuid(event);
};
toAppendTo.append(uuid.toString());
}
}
Loading
Loading