Skip to content

Commit

Permalink
fixing bug with tabs in log line for MDC
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenwinship committed Mar 22, 2024
1 parent 63a09cb commit 2129555
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public void logEntry(MakeDataCountEntry entry) {
public String getLogFileName() {
return "counter_"+new SimpleDateFormat("yyyy-MM-dd").format(new Timestamp(new Date().getTime()))+".log";
}

// Sanitize the values to a safe string for the log file
static String sanitize(String in) {
// Log lines are tab delimited so tabs must be replaced. Replacing escape sequences with a space.
return in != null ? in.replaceAll("\\s+", " ") : null;
}

public static class MakeDataCountEntry {

Expand Down Expand Up @@ -367,7 +373,7 @@ public String getTitle() {
* @param title the title to set
*/
public final void setTitle(String title) {
this.title = title;
this.title = sanitize(title);
}

/**
Expand All @@ -384,7 +390,7 @@ public String getPublisher() {
* @param publisher the publisher to set
*/
public final void setPublisher(String publisher) {
this.publisher = publisher;
this.publisher = sanitize(publisher);
}

/**
Expand All @@ -401,7 +407,7 @@ public String getPublisherId() {
* @param publisherId the publisherId to set
*/
public final void setPublisherId(String publisherId) {
this.publisherId = publisherId;
this.publisherId = sanitize(publisherId);
}

/**
Expand All @@ -418,7 +424,7 @@ public String getAuthors() {
* @param authors the authors to set
*/
public final void setAuthors(String authors) {
this.authors = authors;
this.authors = sanitize(authors);
}

/**
Expand Down Expand Up @@ -452,7 +458,7 @@ public String getVersion() {
* @param version the version to set
*/
public final void setVersion(String version) {
this.version = version;
this.version = sanitize(version);
}

/**
Expand All @@ -469,7 +475,7 @@ public String getOtherId() {
* @param otherId the otherId to set
*/
public void setOtherId(String otherId) {
this.otherId = otherId;
this.otherId = sanitize(otherId);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;

import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -45,8 +44,8 @@ public void testMainAndFileConstructor() {
GlobalId id = dataset.getGlobalId();
dataset.setGlobalId(id);
dvVersion.setDataset(dataset);
dvVersion.setAuthorsStr("OneAuthor;TwoAuthor");
dvVersion.setTitle("Title");
dvVersion.setAuthorsStr("OneAuthor;\tTwoAuthor");
dvVersion.setTitle("Title\tWith Tab");
dvVersion.setVersionNumber(1L);
dvVersion.setReleaseTime(new Date());

Expand All @@ -64,7 +63,13 @@ public void testMainAndFileConstructor() {

//lastly setting attributes we don't actually use currently in our logging/constructors, just in case
entry.setUserCookieId("UserCookId");
entry.setOtherId("OtherId");
entry.setOtherId(null); // null pointer check for sanitize method
assertThat(entry.getOtherId(), is("-"));
entry.setOtherId("OtherId\t\r\nX");
// escape sequences get replaced with a space in sanitize method
assertThat(entry.getOtherId(), is("OtherId X"));
// check other replacements for author list ";" becomes "|"
assertThat(entry.getAuthors(), is("OneAuthor| TwoAuthor"));

//And test. "-" is the default
assertThat(entry.getEventTime(), is(not("-")));
Expand Down

0 comments on commit 2129555

Please sign in to comment.