From c5f35ee817d0ae9a1843d1850ac0894dd29e0d60 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Tue, 27 Apr 2021 12:46:42 +0200 Subject: [PATCH] Added extract command feature. Fixed not exist file check. --- src/fr/devinsy/logar/app/Logar.java | 89 ++++++++++++++++++++++++++- src/org/april/logar/cli/LogarCLI.java | 8 +++ 2 files changed, 94 insertions(+), 3 deletions(-) diff --git a/src/fr/devinsy/logar/app/Logar.java b/src/fr/devinsy/logar/app/Logar.java index 8539e81..8d2d6de 100644 --- a/src/fr/devinsy/logar/app/Logar.java +++ b/src/fr/devinsy/logar/app/Logar.java @@ -397,7 +397,7 @@ public final class Logar { System.out.println("Undefined source."); } - else if (source.exists()) + else if (!source.exists()) { System.out.println("Missing source to check."); } @@ -425,7 +425,7 @@ public final class Logar { System.out.println("Undefined source."); } - else if (source.exists()) + else if (!source.exists()) { System.out.println("Missing source to check."); } @@ -495,6 +495,89 @@ public final class Logar } } + /** + * Extract user agent. + * + * @param source + * the source + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static void extract(final File source, final ExtractOption option) throws IOException + { + if (source == null) + { + System.out.println("Undefined source."); + } + else if (option == null) + { + System.out.println("Undefined option."); + } + else if (!source.exists()) + { + System.out.println("Missing source to sort."); + } + else + { + Files files = FilesUtils.search(source, LOGFILE_PATTERN).removeHidden().keep(".*access.*").sortByName(); + System.out.println(files.size()); + for (File file : files) + { + // System.out.println("== Extract userAgent for [" + + // file.getName() + "]"); + try + { + LineIterator iterator = new LineIterator(file); + while (iterator.hasNext()) + { + String line = iterator.next(); + + try + { + Log log = LogParser.parseAccessLog(line); + String extract; + switch (option) + { + case IP: + extract = log.getIp(); + break; + + case DATETIME: + extract = log.getDatetimeValue(); + break; + + case USERAGENT: + extract = log.getUserAgent(); + break; + + default: + extract = null; + } + + System.out.println(log.getUserAgent()); + } + catch (IllegalArgumentException exception) + { + System.out.println("Bad format line: " + line); + exception.printStackTrace(); + } + catch (DateTimeParseException exception) + { + System.out.println("Bad datetime format: " + line); + } + } + } + catch (IOException exception) + { + System.err.println("Error with file [" + file.getAbsolutePath() + "]"); + exception.printStackTrace(); + } + + LogFile.sortLogFile(file); + } + } + } + /** * Sort. * @@ -509,7 +592,7 @@ public final class Logar { System.out.println("Undefined source."); } - else if (source.exists()) + else if (!source.exists()) { System.out.println("Missing source to sort."); } diff --git a/src/org/april/logar/cli/LogarCLI.java b/src/org/april/logar/cli/LogarCLI.java index c9bc923..0a2e26b 100644 --- a/src/org/april/logar/cli/LogarCLI.java +++ b/src/org/april/logar/cli/LogarCLI.java @@ -26,6 +26,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import fr.devinsy.logar.app.DryOption; +import fr.devinsy.logar.app.ExtractOption; import fr.devinsy.logar.app.Logar; import fr.devinsy.strings.StringList; @@ -223,6 +224,13 @@ public final class LogarCLI Logar.checkSort(source); } + else if (isMatching(args, "extract", "\\s*(ip|datetime|useragent)\\s*", "\\s*\\S+\\s*")) + { + ExtractOption token = ExtractOption.of(args[1]); + File source = new File(args[2]); + + Logar.extract(source, token); + } else if (isMatching(args, "sort", "\\s*\\S+\\s*")) { File source = new File(args[1]);