Added extract command feature. Fixed not exist file check.
This commit is contained in:
parent
bb25cfa96d
commit
c5f35ee817
2 changed files with 94 additions and 3 deletions
|
@ -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.");
|
||||
}
|
||||
|
|
|
@ -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]);
|
||||
|
|
Loading…
Reference in a new issue