Added extract command feature.

This commit is contained in:
Christian P. MOMON 2021-04-28 05:15:15 +02:00
parent c5f35ee817
commit 7b13dafbdc
3 changed files with 71 additions and 9 deletions

View File

@ -0,0 +1,64 @@
/*
* Copyright (C) 2021 Christian Pierre MOMON <christian@momon.org>
*
* This file is part of Logar, simple tool to manage http log files.
*
* Logar is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Logar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Logar. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.devinsy.logar.app;
import org.apache.commons.lang3.StringUtils;
/**
* The Enum DryOption.
*/
public enum ExtractOption
{
IP,
DATETIME,
USERAGENT;
public static ExtractOption of(final String source)
{
ExtractOption result;
if (source == null)
{
result = null;
}
else
{
String token = source.toLowerCase().trim();
if (StringUtils.equals(token, "ip"))
{
result = IP;
}
else if (StringUtils.equals(token, "dateime"))
{
result = DATETIME;
}
else if (StringUtils.equals(token, "useragent"))
{
result = USERAGENT;
}
else
{
result = null;
}
}
//
return result;
}
}

View File

@ -520,18 +520,18 @@ public final class Logar
else else
{ {
Files files = FilesUtils.search(source, LOGFILE_PATTERN).removeHidden().keep(".*access.*").sortByName(); Files files = FilesUtils.search(source, LOGFILE_PATTERN).removeHidden().keep(".*access.*").sortByName();
System.out.println(files.size()); // System.out.println(files.size());
System.err.println(files.size());
for (File file : files) for (File file : files)
{ {
// System.out.println("== Extract userAgent for [" + System.err.println("== Extract userAgent for [" + file.getName() + "]");
// file.getName() + "]");
try try
{ {
LineIterator iterator = new LineIterator(file); LineIterator iterator = new LineIterator(file);
while (iterator.hasNext()) while (iterator.hasNext())
{ {
String line = iterator.next(); String line = iterator.next();
// System.out.println(line);
try try
{ {
Log log = LogParser.parseAccessLog(line); Log log = LogParser.parseAccessLog(line);
@ -554,7 +554,7 @@ public final class Logar
extract = null; extract = null;
} }
System.out.println(log.getUserAgent()); System.out.println(extract);
} }
catch (IllegalArgumentException exception) catch (IllegalArgumentException exception)
{ {
@ -572,8 +572,6 @@ public final class Logar
System.err.println("Error with file [" + file.getAbsolutePath() + "]"); System.err.println("Error with file [" + file.getAbsolutePath() + "]");
exception.printStackTrace(); exception.printStackTrace();
} }
LogFile.sortLogFile(file);
} }
} }
} }

View File

@ -88,7 +88,7 @@ public final class LogParser
} }
catch (DateTimeParseException exception) catch (DateTimeParseException exception)
{ {
throw new IllegalArgumentException("Bad line format (date): " + line); throw new IllegalArgumentException("Bad line format (date): " + line, exception);
} }
// //
@ -144,7 +144,7 @@ public final class LogParser
} }
catch (DateTimeParseException exception) catch (DateTimeParseException exception)
{ {
throw new IllegalArgumentException("Bad line format (date): " + line); throw new IllegalArgumentException("Bad line format (date): " + line, exception);
} }
// //