Skip to content

Commit

Permalink
fix(log.filesystem.provider): Fixed parsing pid with spaces in log li…
Browse files Browse the repository at this point in the history
…nes. (#4108)

* Fixed parsing pid with spaces.

* Fixed Regex

* Updated year in copyright plate.

* Updated bundle version.
  • Loading branch information
salvatore-coppola authored Aug 11, 2022
1 parent af2ab50 commit 99192b4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
2 changes: 1 addition & 1 deletion kura/distrib/config/kura.build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ org.eclipse.kura.web2.ext.version=1.2.0-SNAPSHOT
org.eclipse.kura.useradmin.store.version=1.2.0-SNAPSHOT
org.eclipse.kura.network.threat.manager.version=1.2.0-SNAPSHOT
org.eclipse.kura.core.tamper.detection.version=1.2.0-SNAPSHOT
org.eclipse.kura.log.filesystem.provider.version=1.1.0-SNAPSHOT
org.eclipse.kura.log.filesystem.provider.version=1.1.1-SNAPSHOT
org.eclipse.kura.rest.configuration.provider.version=1.1.0-SNAPSHOT
org.eclipse.kura.request.handler.jaxrs.version=1.1.0-SNAPSHOT
org.eclipse.kura.rest.wire.provider.version=1.1.0-SNAPSHOT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: org.eclipse.kura.log.filesystem.provider
Bundle-SymbolicName: org.eclipse.kura.log.filesystem.provider;singleton:=true
Bundle-Version: 1.1.0.qualifier
Bundle-Version: 1.1.1.qualifier
Bundle-Vendor: Eclipse Kura
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Service-Component: OSGI-INF/*.xml
Expand Down
2 changes: 1 addition & 1 deletion kura/org.eclipse.kura.log.filesystem.provider/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</parent>

<artifactId>org.eclipse.kura.log.filesystem.provider</artifactId>
<version>1.1.0-SNAPSHOT</version>
<version>1.1.1-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.kura.log.LogEntry;
import org.slf4j.Logger;
Expand All @@ -32,13 +34,16 @@ public final class KuraLogLineParser {
public static final String DEFAULT_SYSLOG_IDENTIFIER = "Kura";
public static final String DEFAULT_STACKTRACE = "";

private static final Pattern PID_PATTERN = Pattern.compile("\\[[A-Za-z0-9 ]*\\]");

private long timestamp;
private String pid;
private String priority;
private String message;
private final String transport;
private String syslogIdentifier;
private String stacktrace;
private boolean pidWhitespaceReplaced;

private static KuraLogLineParser instance;

Expand Down Expand Up @@ -72,13 +77,16 @@ public static LogEntry createLogEntry(String message, String filepath, String st
* _SOURCE_REALTIME_TIMESTAMP [PID] PRIORITY MESSAGE_WITH_POSSIBLE_SPACES
*/
private void parseKuraLog() {
String[] splits = this.message.split(" ");
String[] splits = innerTrimPid(this.message).split(" ");
if (splits.length >= 3) {
instance.timestamp = parseStringToEpoch("yyyy-MM-dd'T'hh:mm:ss,S", splits[0]);

instance.pid = splits[1];
instance.pid = instance.pid.replace("[", "");
instance.pid = instance.pid.replace("]", "");

instance.pid = instance.pidWhitespaceReplaced ? instance.pid.replace("-", " ") : instance.pid;

instance.priority = splits[2];
StringBuilder sb = new StringBuilder();
for (int i = 3; i < splits.length; i++) {
Expand All @@ -89,6 +97,21 @@ private void parseKuraLog() {
}
}

private String innerTrimPid(String message) {

String trimmedMessage = message;

Matcher pidMatcher = PID_PATTERN.matcher(message);

if (pidMatcher.find()) {
String foundPid = pidMatcher.group();
trimmedMessage = trimmedMessage.replace(foundPid, foundPid.replace(" ", "-"));
instance.pidWhitespaceReplaced = true;
}

return trimmedMessage;
}

private long parseStringToEpoch(String format, String date) {
try {
Date parsedDate = new SimpleDateFormat(format).parse(date);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021 Eurotech and/or its affiliates and others
* Copyright (c) 2021, 2022 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -28,6 +28,8 @@ public class KuraLogLineParserTest {
private static final String EXAMPLE_STACKTRACE = "java.lang.UnsupportedOperationException: null\n at java.util.Collections$UnmodifiableMap.put(Collections.java:1459) ~[?:1.8.0_282]";
private static final String EXAMPLE_KURA_LOG_LINE_WITH_EXCEPTION = "14:01:17.971 [Thread-2] ERROR org.eclipse.kura.log.filesystem.provider.FilesystemLogProvider - Unexpected exception in FilesystemLogProvider.";

private static final String EXAMPLE_KURA_LOG_LINE_WITH_PID_WHITESPACE = "2022-08-10T11:03:30,545 [ConfigurationListener Event Queue] INFO o.e.k.e.p.ExamplePublisher - Activating ExamplePublisher... Done.";

private String logLine;
private String filePath;
private String stacktrace;
Expand Down Expand Up @@ -101,6 +103,23 @@ public void shouldParseExceptionsCorrectly() {
EXAMPLE_STACKTRACE); // stacktrace
}

@Test
public void shouldParseCorrectKuraEntryWithPidWithWhitespace() throws ParseException {
givenKuraLogLine(EXAMPLE_KURA_LOG_LINE_WITH_PID_WHITESPACE);

whenParseLogLine();

thenLogEntryIs(
new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss,S").parse("2022-08-10T11:03:30,545").toInstant()
.getEpochSecond(), // timestamp
"ConfigurationListener Event Queue", // pid
"o.e.k.e.p.ExamplePublisher - Activating ExamplePublisher... Done.", // message
"INFO", // priority
"Kura", // syslogid
this.filePath, // transport
""); // stacktrace
}

/*
* Steps
*/
Expand Down

0 comments on commit 99192b4

Please sign in to comment.