Added multi-options feature for extract command.
This commit is contained in:
parent
214451155b
commit
c00b1ddcd8
@ -29,6 +29,13 @@ public enum ExtractOption
|
||||
DATETIME,
|
||||
USERAGENT;
|
||||
|
||||
/**
|
||||
* Of.
|
||||
*
|
||||
* @param source
|
||||
* the source
|
||||
* @return the extract option
|
||||
*/
|
||||
public static ExtractOption of(final String source)
|
||||
{
|
||||
ExtractOption result;
|
||||
|
120
src/fr/devinsy/logar/app/ExtractOptions.java
Normal file
120
src/fr/devinsy/logar/app/ExtractOptions.java
Normal file
@ -0,0 +1,120 @@
|
||||
/*
|
||||
* 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 class ExtractOptions
|
||||
{
|
||||
OnOffOption ip;
|
||||
OnOffOption user;
|
||||
OnOffOption datetime;
|
||||
OnOffOption userAgent;
|
||||
|
||||
/**
|
||||
* Instantiates a new extract options.
|
||||
*/
|
||||
public ExtractOptions()
|
||||
{
|
||||
this.ip = OnOffOption.OFF;
|
||||
this.user = OnOffOption.OFF;
|
||||
this.datetime = OnOffOption.OFF;
|
||||
this.userAgent = OnOffOption.OFF;
|
||||
}
|
||||
|
||||
public OnOffOption getDatetime()
|
||||
{
|
||||
return this.datetime;
|
||||
}
|
||||
|
||||
public OnOffOption getIp()
|
||||
{
|
||||
return this.ip;
|
||||
}
|
||||
|
||||
public OnOffOption getUser()
|
||||
{
|
||||
return this.user;
|
||||
}
|
||||
|
||||
public OnOffOption getUserAgent()
|
||||
{
|
||||
return this.userAgent;
|
||||
}
|
||||
|
||||
public void setDatetime(final OnOffOption datetime)
|
||||
{
|
||||
this.datetime = datetime;
|
||||
}
|
||||
|
||||
public void setIp(final OnOffOption ip)
|
||||
{
|
||||
this.ip = ip;
|
||||
}
|
||||
|
||||
public void setUser(final OnOffOption user)
|
||||
{
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public void setUserAgent(final OnOffOption userAgent)
|
||||
{
|
||||
this.userAgent = userAgent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Of.
|
||||
*
|
||||
* @param source
|
||||
* the source
|
||||
* @return the extract option
|
||||
*/
|
||||
public static ExtractOptions of(final String source)
|
||||
{
|
||||
ExtractOptions result;
|
||||
|
||||
result = new ExtractOptions();
|
||||
|
||||
if (StringUtils.contains(source, "ip"))
|
||||
{
|
||||
result.setIp(OnOffOption.ON);
|
||||
}
|
||||
|
||||
if (StringUtils.contains(source, "remoteuser"))
|
||||
{
|
||||
result.setUser(OnOffOption.ON);
|
||||
}
|
||||
|
||||
if (StringUtils.contains(source, "datetime"))
|
||||
{
|
||||
result.setDatetime(OnOffOption.ON);
|
||||
}
|
||||
|
||||
if (StringUtils.contains(source, "useragent"))
|
||||
{
|
||||
result.setUserAgent(OnOffOption.ON);
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
}
|
@ -39,6 +39,7 @@ import fr.devinsy.logar.app.anonymizer.Anonymizer;
|
||||
import fr.devinsy.logar.app.log.Log;
|
||||
import fr.devinsy.logar.app.log.LogFile;
|
||||
import fr.devinsy.logar.app.log.LogParser;
|
||||
import fr.devinsy.strings.StringList;
|
||||
|
||||
/**
|
||||
* The Class Logar.
|
||||
@ -115,7 +116,7 @@ public final class Logar
|
||||
* the target
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void archive(final File source, final File target, final DryOption dry) throws IOException
|
||||
public static void archive(final File source, final File target, final OnOffOption dry) throws IOException
|
||||
{
|
||||
archiveAccessFiles(source, target, dry);
|
||||
archiveErrorFiles(source, target, dry);
|
||||
@ -128,7 +129,7 @@ public final class Logar
|
||||
* the source
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void archiveAccessFiles(final File source, final File target, final DryOption dry) throws IOException
|
||||
public static void archiveAccessFiles(final File source, final File target, final OnOffOption dry) throws IOException
|
||||
{
|
||||
if (source == null)
|
||||
{
|
||||
@ -228,7 +229,7 @@ public final class Logar
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
public static void archiveErrorFiles(final File source, final File target, final DryOption dry) throws IOException
|
||||
public static void archiveErrorFiles(final File source, final File target, final OnOffOption dry) throws IOException
|
||||
{
|
||||
if (source == null)
|
||||
{
|
||||
@ -503,13 +504,13 @@ public final class Logar
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
*/
|
||||
public static void extract(final File source, final ExtractOption option) throws IOException
|
||||
public static void extract(final File source, final ExtractOptions options) throws IOException
|
||||
{
|
||||
if (source == null)
|
||||
{
|
||||
System.out.println("Undefined source.");
|
||||
}
|
||||
else if (option == null)
|
||||
else if (options == null)
|
||||
{
|
||||
System.out.println("Undefined option.");
|
||||
}
|
||||
@ -535,26 +536,29 @@ public final class Logar
|
||||
try
|
||||
{
|
||||
Log log = LogParser.parseAccessLog(line);
|
||||
String extract;
|
||||
switch (option)
|
||||
|
||||
StringList extract = new StringList();
|
||||
if (options.getIp().isOn())
|
||||
{
|
||||
case IP:
|
||||
extract = log.getIp();
|
||||
break;
|
||||
|
||||
case DATETIME:
|
||||
extract = log.getDatetimeValue();
|
||||
break;
|
||||
|
||||
case USERAGENT:
|
||||
extract = log.getUserAgent();
|
||||
break;
|
||||
|
||||
default:
|
||||
extract = null;
|
||||
extract.append(log.getIp());
|
||||
}
|
||||
|
||||
System.out.println(extract);
|
||||
if (options.getUser().isOn())
|
||||
{
|
||||
extract.append(log.getUser());
|
||||
}
|
||||
|
||||
if (options.getDatetime().isOn())
|
||||
{
|
||||
extract.append(log.getDatetime());
|
||||
}
|
||||
|
||||
if (options.getUserAgent().isOn())
|
||||
{
|
||||
extract.append(log.getUserAgent());
|
||||
}
|
||||
|
||||
System.out.println(extract.toStringSeparatedBy(" "));
|
||||
}
|
||||
catch (IllegalArgumentException exception)
|
||||
{
|
||||
|
@ -21,7 +21,7 @@ package fr.devinsy.logar.app;
|
||||
/**
|
||||
* The Enum DryOption.
|
||||
*/
|
||||
public enum DryOption
|
||||
public enum OnOffOption
|
||||
{
|
||||
ON,
|
||||
OFF;
|
@ -25,9 +25,9 @@ import org.april.logar.util.BuildInformation;
|
||||
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.ExtractOptions;
|
||||
import fr.devinsy.logar.app.Logar;
|
||||
import fr.devinsy.logar.app.OnOffOption;
|
||||
import fr.devinsy.strings.StringList;
|
||||
|
||||
/**
|
||||
@ -56,12 +56,13 @@ public final class LogarCLI
|
||||
message.appendln("Usage:");
|
||||
message.appendln(" logar [ -h | -help | --help ]");
|
||||
message.appendln(" logar [ -v | -version | --version ]");
|
||||
message.appendln(" logar anonymize fileordirectory [mapfile] anonymize ip and user");
|
||||
message.appendln(" logar archive [-dry] source target archive previous month from /var/log/nginx/ tree");
|
||||
message.appendln(" logar check fileordirectory check line format in log file");
|
||||
message.appendln(" logar checksort fileordirectory check sort in log file");
|
||||
message.appendln(" logar sort fileordirectory sort log files by datetime");
|
||||
message.appendln(" logar testconcate fileordirectory test line concate in log file");
|
||||
message.appendln(" logar anonymize <fileordirectory> [<mapfile>] anonymize ip and user");
|
||||
message.appendln(" logar archive [-dry] <source> <target> archive previous month from /var/log/nginx/ tree");
|
||||
message.appendln(" logar check <fileordirectory> check line format in log files");
|
||||
message.appendln(" logar checksort <fileordirectory> check sort in log files");
|
||||
message.appendln(" logar extract <fields> <fileordirectory> extract one or more fields (ip,user,datetime,useragent) from log files");
|
||||
message.appendln(" logar sort <fileordirectory> sort log files by datetime");
|
||||
message.appendln(" logar testconcate <fileordirectory> test line concate in log files");
|
||||
|
||||
logger.info(message.toString());
|
||||
}
|
||||
@ -203,14 +204,14 @@ public final class LogarCLI
|
||||
File source = new File(args[1]);
|
||||
File target = new File(args[2]);
|
||||
|
||||
Logar.archive(source, target, DryOption.OFF);
|
||||
Logar.archive(source, target, OnOffOption.OFF);
|
||||
}
|
||||
else if (isMatching(args, "archive", "-dry", "\\s*\\S+\\s*", "\\s*\\S+\\s*"))
|
||||
{
|
||||
File source = new File(args[2]);
|
||||
File target = new File(args[3]);
|
||||
|
||||
Logar.archive(source, target, DryOption.ON);
|
||||
Logar.archive(source, target, OnOffOption.ON);
|
||||
}
|
||||
else if (isMatching(args, "check", "\\s*\\S+\\s*"))
|
||||
{
|
||||
@ -224,12 +225,12 @@ public final class LogarCLI
|
||||
|
||||
Logar.checkSort(source);
|
||||
}
|
||||
else if (isMatching(args, "extract", "\\s*(ip|datetime|useragent)\\s*", "\\s*\\S+\\s*"))
|
||||
else if (isMatching(args, "extract", "\\s*((ip)?(,)?(remoteuser)?(,)?(datetime)?(,)?(useragent)?)\\s*", "\\s*\\S+\\s*"))
|
||||
{
|
||||
ExtractOption token = ExtractOption.of(args[1]);
|
||||
ExtractOptions options = ExtractOptions.of(args[1]);
|
||||
File source = new File(args[2]);
|
||||
|
||||
Logar.extract(source, token);
|
||||
Logar.extract(source, options);
|
||||
}
|
||||
else if (isMatching(args, "sort", "\\s*\\S+\\s*"))
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user