258 lines
5.4 KiB
Java
258 lines
5.4 KiB
Java
/*
|
|
* 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.log;
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
/**
|
|
* The Class Log.
|
|
*/
|
|
public final class Log
|
|
{
|
|
private static Logger logger = LoggerFactory.getLogger(Log.class);
|
|
|
|
// Generic attributes.
|
|
private String line;
|
|
private LocalDateTime datetime;
|
|
private String datetimeValue;
|
|
|
|
// Specific access log attributes.
|
|
private String ip;
|
|
private String user;
|
|
private String request;
|
|
private String status;
|
|
private String bodyByteSent;
|
|
private String referer;
|
|
private String userAgent;
|
|
|
|
// Specific error log attributes.
|
|
private String level;
|
|
private String message;
|
|
|
|
/**
|
|
* Instantiates a new log.
|
|
*
|
|
* @param log
|
|
* the log
|
|
*/
|
|
public Log(final Log log)
|
|
{
|
|
this.line = log.getLine();
|
|
this.datetime = log.getDatetime();
|
|
this.datetimeValue = log.getDatetimeValue();
|
|
|
|
this.ip = log.getIp();
|
|
this.user = log.getUser();
|
|
this.request = log.getRequest();
|
|
this.status = log.getStatus();
|
|
this.bodyByteSent = log.getBodyByteSent();
|
|
this.referer = log.getReferer();
|
|
this.userAgent = log.getUserAgent();
|
|
|
|
this.level = log.getLevel();
|
|
this.message = log.getMessage();
|
|
}
|
|
|
|
/**
|
|
* Instantiates a new log.
|
|
*/
|
|
public Log(final String line, final LocalDateTime datetime)
|
|
{
|
|
this.line = line;
|
|
this.datetime = datetime;
|
|
this.datetimeValue = null;
|
|
|
|
this.ip = null;
|
|
this.user = null;
|
|
this.request = null;
|
|
this.referer = null;
|
|
this.userAgent = null;
|
|
|
|
this.level = null;
|
|
this.message = null;
|
|
}
|
|
|
|
/**
|
|
* Concate access log.
|
|
*/
|
|
public void concateAccessLog()
|
|
{
|
|
this.line = String.format("%s - %s [%s] \"%s\" %s %s \"%s\" \"%s\"",
|
|
this.ip, this.user, this.datetimeValue, this.request, this.status, this.bodyByteSent, this.referer, this.userAgent);
|
|
}
|
|
|
|
/**
|
|
* Concate error log.
|
|
*/
|
|
public void concateErrorLog()
|
|
{
|
|
this.line = String.format("%s [%s] %s",
|
|
this.datetimeValue, this.level, this.message);
|
|
}
|
|
|
|
public String getBodyByteSent()
|
|
{
|
|
return this.bodyByteSent;
|
|
}
|
|
|
|
public LocalDateTime getDatetime()
|
|
{
|
|
return this.datetime;
|
|
}
|
|
|
|
public String getDatetimeValue()
|
|
{
|
|
return this.datetimeValue;
|
|
}
|
|
|
|
public String getIp()
|
|
{
|
|
return this.ip;
|
|
}
|
|
|
|
public String getLevel()
|
|
{
|
|
return this.level;
|
|
}
|
|
|
|
public String getLine()
|
|
{
|
|
return this.line;
|
|
}
|
|
|
|
public String getMessage()
|
|
{
|
|
return this.message;
|
|
}
|
|
|
|
public String getReferer()
|
|
{
|
|
return this.referer;
|
|
}
|
|
|
|
public String getRequest()
|
|
{
|
|
return this.request;
|
|
}
|
|
|
|
public String getStatus()
|
|
{
|
|
return this.status;
|
|
}
|
|
|
|
public String getUser()
|
|
{
|
|
return this.user;
|
|
}
|
|
|
|
public String getUserAgent()
|
|
{
|
|
return this.userAgent;
|
|
}
|
|
|
|
/**
|
|
* Reduce.
|
|
*/
|
|
public void reduce()
|
|
{
|
|
this.datetimeValue = null;
|
|
this.request = null;
|
|
this.referer = null;
|
|
this.userAgent = null;
|
|
this.status = null;
|
|
this.bodyByteSent = null;
|
|
|
|
this.level = null;
|
|
this.message = null;
|
|
}
|
|
|
|
public void setBodyByteSent(final String bodyByteSent)
|
|
{
|
|
this.bodyByteSent = bodyByteSent;
|
|
}
|
|
|
|
public void setDatetimeValue(final String datetimeValue)
|
|
{
|
|
this.datetimeValue = datetimeValue;
|
|
}
|
|
|
|
public void setIp(final String ip)
|
|
{
|
|
this.ip = ip;
|
|
}
|
|
|
|
public void setLevel(final String level)
|
|
{
|
|
this.level = level;
|
|
}
|
|
|
|
public void setMessage(final String message)
|
|
{
|
|
this.message = message;
|
|
}
|
|
|
|
public void setReferer(final String referer)
|
|
{
|
|
this.referer = referer;
|
|
}
|
|
|
|
public void setRequest(final String request)
|
|
{
|
|
this.request = request;
|
|
}
|
|
|
|
public void setStatus(final String status)
|
|
{
|
|
this.status = status;
|
|
}
|
|
|
|
public void setUser(final String user)
|
|
{
|
|
this.user = user;
|
|
}
|
|
|
|
/**
|
|
* Sets the user agent.
|
|
*
|
|
* @param userAgent
|
|
* the new user agent
|
|
*/
|
|
public void setUserAgent(final String userAgent)
|
|
{
|
|
this.userAgent = userAgent;
|
|
}
|
|
|
|
/* (non-Javadoc)
|
|
* @see java.lang.Object#toString()
|
|
*/
|
|
@Override
|
|
public String toString()
|
|
{
|
|
String result;
|
|
|
|
result = this.line;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
}
|