Parse Logfmt Key Value Lines
Split key=value pairs with a small scanner that honors double-quoted values containing spaces, keeping field order in a LinkedHashMap.
Code
import java.util.LinkedHashMap;
import java.util.Map;
public class LogfmtParser {
// logfmt: space-separated key=value pairs; quoted values may contain spaces
static Map<String, String> parseLogfmt(String line) {
Map<String, String> fields = new LinkedHashMap<>();
int i = 0;
while (i < line.length()) {
while (i < line.length() && line.charAt(i) == ' ') i++;
if (i >= line.length()) break;
int eq = line.indexOf('=', i);
if (eq < 0) break; // a trailing fragment without an equals sign is dropped
String key = line.substring(i, eq);
i = eq + 1;
StringBuilder value = new StringBuilder();
if (i < line.length() && line.charAt(i) == '"') {
i++; // step past the opening quote
while (i < line.length() && line.charAt(i) != '"') {
value.append(line.charAt(i++));
}
i++; // step past the closing quote
} else {
while (i < line.length() && line.charAt(i) != ' ') {
value.append(line.charAt(i++));
}
}
fields.put(key, value.toString());
}
return fields;
}
public static void main(String[] args) {
String line = "level=info msg=\"request done\" path=/api/orders status=200 duration_ms=42";
Map<String, String> fields = parseLogfmt(line);
System.out.println("level: " + fields.get("level"));
System.out.println("msg: " + fields.get("msg"));
System.out.println("status: " + fields.get("status"));
System.out.println("fields: " + fields.keySet());
}
}How it works
Structurally, expect the class LogfmtParser and the functions parseLogfmt and main and nothing else.
Repetition is confined to while (i < line.length()) — the rest of the file is straight-line.
Using it
Signature: static Map<String, String> parseLogfmt(String line)
Output lines are prefixed (level:, msg:, status:), making each step’s effect easy to spot.
Start the program and main runs parseLogfmt one after another.
Key statements
if (eq < 0) break; // a trailing fragment without an equals sign is dropped
String key = line.substring(i, eq);
String line = "level=info msg=\"request done\" path=/api/orders status=200 duration_ms=42";
i++; // step past the opening quote
while (i < line.length() && line.charAt(i) != '"') {
value.append(line.charAt(i++));
}
Interactive Tools for This Topic
Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.Tar Archive Command BuilderBuild safe tar create, extract, and list commands for gzip, xz, zstd, and bzip2 archives with excludes, target directory (-C), and flag explanations.
FAQ
- What does Parse Logfmt Key Value Lines print when run?
- No setup needed: run the file and look for labeled lines like `level:`, `msg:`, `status:` in the output.
Report a problem with this exampleOpens a pre-filled issue on GitHub.