Removed the old log manager & integrated the new log manager
This commit is contained in:
parent
2f396093d1
commit
ca3e4d180c
@ -7,6 +7,8 @@ import de.gnmyt.SQLToolkit.manager.ResultManager;
|
|||||||
import de.gnmyt.SQLToolkit.manager.UpdateManager;
|
import de.gnmyt.SQLToolkit.manager.UpdateManager;
|
||||||
import de.gnmyt.SQLToolkit.types.LogLevelType;
|
import de.gnmyt.SQLToolkit.types.LogLevelType;
|
||||||
import de.gnmyt.SQLToolkit.types.LoginParam;
|
import de.gnmyt.SQLToolkit.types.LoginParam;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
@ -15,15 +17,17 @@ import java.sql.ResultSet;
|
|||||||
|
|
||||||
public class MySQLConnection {
|
public class MySQLConnection {
|
||||||
|
|
||||||
|
public static final Logger LOG = LoggerFactory.getLogger("MySQL-Logger");
|
||||||
|
|
||||||
private final String hostname;
|
private final String hostname;
|
||||||
private final String username;
|
private final String username;
|
||||||
private final String password;
|
private final String password;
|
||||||
private final String database;
|
private final String database;
|
||||||
|
|
||||||
private String tablePrefix = "";
|
private String tablePrefix = "";
|
||||||
private String tablePrefixVariable = "";
|
private String tablePrefixVariable = "";
|
||||||
private String connectString = "";
|
private String connectString = "";
|
||||||
private Connection con;
|
private Connection con;
|
||||||
private final SqlLogManager sqlLogManager;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the Connection String
|
* Update the Connection String
|
||||||
@ -85,14 +89,6 @@ public class MySQLConnection {
|
|||||||
return (con != null);
|
return (con != null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the SQL Log Manager and adjust it how you like it
|
|
||||||
* @return the SQL Log Manager
|
|
||||||
*/
|
|
||||||
public SqlLogManager getLogManager() {
|
|
||||||
return sqlLogManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Basic constructor for the Connection
|
* Basic constructor for the Connection
|
||||||
* @param hostname MySQL server hostname
|
* @param hostname MySQL server hostname
|
||||||
@ -107,9 +103,7 @@ public class MySQLConnection {
|
|||||||
this.username = username;
|
this.username = username;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
this.database = database;
|
this.database = database;
|
||||||
this.sqlLogManager = new SqlLogManager();
|
|
||||||
updateConnectString(loginParams);
|
updateConnectString(loginParams);
|
||||||
setLogLevelType(logLevel);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -125,19 +119,7 @@ public class MySQLConnection {
|
|||||||
this.username = username;
|
this.username = username;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
this.database = database;
|
this.database = database;
|
||||||
this.sqlLogManager = new SqlLogManager();
|
|
||||||
updateConnectString(loginParams);
|
updateConnectString(loginParams);
|
||||||
setLogLevelType(LogLevelType.NONE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set a new logging level
|
|
||||||
* @param logLevelType New logging level
|
|
||||||
* @return this class
|
|
||||||
*/
|
|
||||||
public MySQLConnection setLogLevelType(LogLevelType logLevelType) {
|
|
||||||
this.sqlLogManager.setLogLevelType(logLevelType);
|
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -153,7 +135,7 @@ public class MySQLConnection {
|
|||||||
for (Object current : params) { ps.setObject(start, current);start++; }
|
for (Object current : params) { ps.setObject(start, current);start++; }
|
||||||
return ps.executeQuery();
|
return ps.executeQuery();
|
||||||
} catch (Exception err) {
|
} catch (Exception err) {
|
||||||
sqlLogManager.sendError(err.getMessage());
|
LOG.error(err.getMessage());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -165,7 +147,7 @@ public class MySQLConnection {
|
|||||||
* @return ResultManager
|
* @return ResultManager
|
||||||
*/
|
*/
|
||||||
public ResultManager getResult(String query, Object... params) {
|
public ResultManager getResult(String query, Object... params) {
|
||||||
return new ResultManager(getResultSet(query, params), sqlLogManager);
|
return new ResultManager(getResultSet(query, params));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -255,18 +237,19 @@ public class MySQLConnection {
|
|||||||
*/
|
*/
|
||||||
public MySQLConnection connect() {
|
public MySQLConnection connect() {
|
||||||
if (!isConnected()) { try {
|
if (!isConnected()) { try {
|
||||||
sqlLogManager.sendInfo("Connecting to " + hostname + " with user " + username + " to database " + database);
|
LOG.info("Connecting to " + hostname + " with user " + username + " to database " + database);
|
||||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||||
con = DriverManager.getConnection("jdbc:mysql://" + hostname + "/" + database + connectString, username, password);
|
con = DriverManager.getConnection("jdbc:mysql://" + hostname + "/" + database + connectString, username, password);
|
||||||
sqlLogManager.sendInfo("Connection established");
|
LOG.info("Connection established");
|
||||||
} catch (Exception exception) {
|
} catch (Exception exception) {
|
||||||
if (exception.getMessage().contains("jdbc.Driver"))
|
if (exception.getMessage().contains("jdbc.Driver"))
|
||||||
sqlLogManager.sendError("MySQL driver not found! Check your dependencies or install the JDBC Mysql Driver from https://dev.mysql.com/downloads/file/?id=498587");
|
LOG.error("MySQL driver not found! Check your dependencies or install the JDBC Mysql Driver from https://dev.mysql.com/downloads/file/?id=498587");
|
||||||
else if (exception.getMessage().contains("has not received any packets")) sqlLogManager.sendError("No packets received from the server.");
|
else if (exception.getMessage().contains("has not received any packets"))
|
||||||
else if (exception.getMessage().contains("denied for user")) sqlLogManager.sendError("Wrong username/password");
|
LOG.error("No packets received from the server.");
|
||||||
else sqlLogManager.sendError(exception.getMessage());
|
else if (exception.getMessage().contains("denied for user")) LOG.error("Wrong username/password");
|
||||||
|
else LOG.error(exception.getMessage());
|
||||||
}
|
}
|
||||||
} else sqlLogManager.sendWarning("Already connected");
|
} else LOG.warn("Already connected");
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -275,9 +258,13 @@ public class MySQLConnection {
|
|||||||
* @return this class
|
* @return this class
|
||||||
*/
|
*/
|
||||||
public MySQLConnection disconnect() {
|
public MySQLConnection disconnect() {
|
||||||
if (isConnected()) { try { con.close();con = null;sqlLogManager.sendInfo("Connection closed");
|
if (isConnected()) {
|
||||||
} catch (Exception err) { sqlLogManager.sendError(err.getMessage()); }
|
try {
|
||||||
} else sqlLogManager.sendWarning("Not connected. Please connect first");
|
con.close();
|
||||||
|
con = null;
|
||||||
|
LOG.info("Connection closed");
|
||||||
|
} catch (Exception err) { LOG.error(err.getMessage()); }
|
||||||
|
} else LOG.warn("Not connected. Please connect first");
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,87 +0,0 @@
|
|||||||
package de.gnmyt.SQLToolkit.drivers;
|
|
||||||
|
|
||||||
import de.gnmyt.SQLToolkit.types.LogLevelType;
|
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.time.format.DateTimeFormatter;
|
|
||||||
|
|
||||||
public class SqlLogManager {
|
|
||||||
|
|
||||||
private LogLevelType logLevelType;
|
|
||||||
private String logFormat = "[%date » MySQL %type] %msg";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the Log Level
|
|
||||||
* @param logLevelType Logging Level
|
|
||||||
*/
|
|
||||||
public void setLogLevelType(LogLevelType logLevelType) {
|
|
||||||
this.logLevelType = logLevelType;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Format for the Message
|
|
||||||
* @param logFormat The Formatted String
|
|
||||||
*/
|
|
||||||
public void setLogFormat(String logFormat) {
|
|
||||||
this.logFormat = logFormat;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Getting the Logging Level
|
|
||||||
* @return LogLevelType
|
|
||||||
*/
|
|
||||||
public LogLevelType getLogLevelType() {
|
|
||||||
return logLevelType;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Send an information
|
|
||||||
* @param msg The provided message
|
|
||||||
*/
|
|
||||||
public void sendInfo(String msg) {
|
|
||||||
if (logLevelType == LogLevelType.ALL)
|
|
||||||
sendLog("Info", msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Send an Warning
|
|
||||||
* @param msg The provided message
|
|
||||||
*/
|
|
||||||
public void sendWarning(String msg) {
|
|
||||||
if (logLevelType == LogLevelType.ALL || logLevelType == LogLevelType.LOW)
|
|
||||||
sendLog("Warning", msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Send an Error
|
|
||||||
* @param msg The provided message
|
|
||||||
*/
|
|
||||||
public void sendError(String msg) {
|
|
||||||
if (logLevelType == LogLevelType.ALL || logLevelType == LogLevelType.LOW) {
|
|
||||||
StackTraceElement[] st = Thread.currentThread().getStackTrace();
|
|
||||||
StackTraceElement stack = st[st.length-1];
|
|
||||||
sendLog("Error", "<"+stack.getFileName()+":"+stack.getLineNumber()+"> "+msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Intern Logging Feature
|
|
||||||
* @param prefix Logging Prefix
|
|
||||||
* @param msg The provided message
|
|
||||||
*/
|
|
||||||
private void sendLog(String prefix, String msg) {
|
|
||||||
String formatted = logFormat.replace("%type", prefix).replace("%date", getDate()).replace("%msg", msg);
|
|
||||||
System.out.println(formatted);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Getting the date
|
|
||||||
* @return Datetime
|
|
||||||
*/
|
|
||||||
private String getDate() {
|
|
||||||
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("H:m:s");
|
|
||||||
LocalDateTime dt = LocalDateTime.now();
|
|
||||||
return dtf.format(dt);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,6 +1,7 @@
|
|||||||
package de.gnmyt.SQLToolkit.manager;
|
package de.gnmyt.SQLToolkit.manager;
|
||||||
|
|
||||||
import de.gnmyt.SQLToolkit.drivers.MySQLConnection;
|
import de.gnmyt.SQLToolkit.drivers.MySQLConnection;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -10,6 +11,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
|||||||
|
|
||||||
public class DataBaseSelection {
|
public class DataBaseSelection {
|
||||||
|
|
||||||
|
private final Logger LOG = MySQLConnection.LOG;
|
||||||
|
|
||||||
private final MySQLConnection connection;
|
private final MySQLConnection connection;
|
||||||
private final HashMap<String, Object> whereList;
|
private final HashMap<String, Object> whereList;
|
||||||
private final ArrayList<Object> databaseParameters;
|
private final ArrayList<Object> databaseParameters;
|
||||||
@ -121,8 +124,7 @@ public class DataBaseSelection {
|
|||||||
getTempParams().forEach(v -> { messageBuilder.append((added.get()) ? ", " : "").append(v);added.set(true); });
|
getTempParams().forEach(v -> { messageBuilder.append((added.get()) ? ", " : "").append(v);added.set(true); });
|
||||||
StackTraceElement[] st = Thread.currentThread().getStackTrace();
|
StackTraceElement[] st = Thread.currentThread().getStackTrace();
|
||||||
StackTraceElement stack = st[st.length-1];
|
StackTraceElement stack = st[st.length-1];
|
||||||
connection.getLogManager()
|
LOG.debug("DEBUG <" + stack.getFileName()+":"+stack.getLineNumber() + "> Statement: " + messageBuilder.toString());
|
||||||
.sendInfo("DEBUG <" + stack.getFileName()+":"+stack.getLineNumber() + "> Statement: " + messageBuilder.toString());
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package de.gnmyt.SQLToolkit.manager;
|
package de.gnmyt.SQLToolkit.manager;
|
||||||
|
|
||||||
import de.gnmyt.SQLToolkit.drivers.SqlLogManager;
|
import de.gnmyt.SQLToolkit.drivers.MySQLConnection;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
@ -9,17 +10,16 @@ import java.util.HashMap;
|
|||||||
|
|
||||||
public class ResultManager {
|
public class ResultManager {
|
||||||
|
|
||||||
private ResultSet resultSet;
|
private final Logger LOG = MySQLConnection.LOG;
|
||||||
private SqlLogManager logManager;
|
|
||||||
|
private final ResultSet resultSet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Basic constructor for the ResultManager
|
* Basic constructor for the ResultManager
|
||||||
* @param resultSet Existing ResultSet
|
* @param resultSet Existing ResultSet
|
||||||
* @param logManager Existing LogManager
|
|
||||||
*/
|
*/
|
||||||
public ResultManager(ResultSet resultSet, SqlLogManager logManager) {
|
public ResultManager(ResultSet resultSet) {
|
||||||
this.resultSet = resultSet;
|
this.resultSet = resultSet;
|
||||||
this.logManager = logManager;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -37,7 +37,7 @@ public class ResultManager {
|
|||||||
*/
|
*/
|
||||||
public Object getObject(String column) {
|
public Object getObject(String column) {
|
||||||
try { while (resultSet.next()) { return resultSet.getObject(column); }
|
try { while (resultSet.next()) { return resultSet.getObject(column); }
|
||||||
} catch (Exception err) { logManager.sendError(err.getMessage()); }
|
} catch (Exception err) { LOG.error(err.getMessage()); }
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,7 +48,7 @@ public class ResultManager {
|
|||||||
*/
|
*/
|
||||||
public String getString(String column) {
|
public String getString(String column) {
|
||||||
try { while (resultSet.next()) { return resultSet.getString(column); }
|
try { while (resultSet.next()) { return resultSet.getString(column); }
|
||||||
} catch (Exception err) { logManager.sendError(err.getMessage()); }
|
} catch (Exception err) { LOG.error(err.getMessage()); }
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,7 +59,7 @@ public class ResultManager {
|
|||||||
*/
|
*/
|
||||||
public Integer getInteger(String column) {
|
public Integer getInteger(String column) {
|
||||||
try { while (resultSet.next()) { return resultSet.getInt(column); }
|
try { while (resultSet.next()) { return resultSet.getInt(column); }
|
||||||
} catch (Exception err) { logManager.sendError(err.getMessage()); }
|
} catch (Exception err) { LOG.error(err.getMessage()); }
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,13 +70,13 @@ public class ResultManager {
|
|||||||
*/
|
*/
|
||||||
public Boolean getBoolean(String column) {
|
public Boolean getBoolean(String column) {
|
||||||
try { while (resultSet.next()) { return resultSet.getBoolean(column); }
|
try { while (resultSet.next()) { return resultSet.getBoolean(column); }
|
||||||
} catch (Exception err) { logManager.sendError(err.getMessage()); }
|
} catch (Exception err) { LOG.error(err.getMessage()); }
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Timestamp getTimestamp(String column) {
|
public Timestamp getTimestamp(String column) {
|
||||||
try { while (resultSet.next()) { return resultSet.getTimestamp(column); }
|
try { while (resultSet.next()) { return resultSet.getTimestamp(column); }
|
||||||
} catch (Exception err) { logManager.sendError(err.getMessage()); }
|
} catch (Exception err) { LOG.error(err.getMessage()); }
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,7 +86,7 @@ public class ResultManager {
|
|||||||
*/
|
*/
|
||||||
public Integer getRowCount() {
|
public Integer getRowCount() {
|
||||||
int count=0;
|
int count=0;
|
||||||
try { while (resultSet.next()) { count++; } } catch (Exception err) { logManager.sendError(err.getMessage()); }
|
try { while (resultSet.next()) { count++; } } catch (Exception err) { LOG.error(err.getMessage()); }
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,7 +98,7 @@ public class ResultManager {
|
|||||||
public ArrayList<String> getList(String column) {
|
public ArrayList<String> getList(String column) {
|
||||||
ArrayList<String> results = new ArrayList<>();
|
ArrayList<String> results = new ArrayList<>();
|
||||||
try { while (resultSet.next()) { results.add(resultSet.getString(column)); }
|
try { while (resultSet.next()) { results.add(resultSet.getString(column)); }
|
||||||
} catch (Exception err) { logManager.sendError(err.getMessage()); }
|
} catch (Exception err) { LOG.error(err.getMessage()); }
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,7 +112,7 @@ public class ResultManager {
|
|||||||
HashMap<String, String> results = new HashMap<>();
|
HashMap<String, String> results = new HashMap<>();
|
||||||
try { while (resultSet.next()) {
|
try { while (resultSet.next()) {
|
||||||
results.put(resultSet.getString(column), resultSet.getString(column2)); }
|
results.put(resultSet.getString(column), resultSet.getString(column2)); }
|
||||||
} catch (Exception err) { logManager.sendError(err.getMessage()); }
|
} catch (Exception err) { LOG.error(err.getMessage()); }
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,7 +131,7 @@ public class ResultManager {
|
|||||||
}
|
}
|
||||||
results.add(result);
|
results.add(result);
|
||||||
}
|
}
|
||||||
} catch (Exception err) { logManager.sendError(err.getMessage()); }
|
} catch (Exception err) { LOG.error(err.getMessage()); }
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ package de.gnmyt.SQLToolkit.manager;
|
|||||||
|
|
||||||
import de.gnmyt.SQLToolkit.drivers.MySQLConnection;
|
import de.gnmyt.SQLToolkit.drivers.MySQLConnection;
|
||||||
import de.gnmyt.SQLToolkit.generator.TableGenerator;
|
import de.gnmyt.SQLToolkit.generator.TableGenerator;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -9,6 +10,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
|||||||
|
|
||||||
public class UpdateManager {
|
public class UpdateManager {
|
||||||
|
|
||||||
|
private final Logger LOG = MySQLConnection.LOG;
|
||||||
|
|
||||||
private MySQLConnection connection;
|
private MySQLConnection connection;
|
||||||
private String tableName;
|
private String tableName;
|
||||||
private HashMap<String, Object> whereList;
|
private HashMap<String, Object> whereList;
|
||||||
@ -67,8 +70,7 @@ public class UpdateManager {
|
|||||||
whereList.values().forEach(v -> { messageBuilder.append((added.get()) ? ", " : "").append(v);added.set(true); });
|
whereList.values().forEach(v -> { messageBuilder.append((added.get()) ? ", " : "").append(v);added.set(true); });
|
||||||
StackTraceElement[] st = Thread.currentThread().getStackTrace();
|
StackTraceElement[] st = Thread.currentThread().getStackTrace();
|
||||||
StackTraceElement stack = st[st.length-1];
|
StackTraceElement stack = st[st.length-1];
|
||||||
connection.getLogManager()
|
LOG.debug("DEBUG <" + stack.getFileName()+":"+stack.getLineNumber() + "> Statement: " + messageBuilder.toString());
|
||||||
.sendInfo("DEBUG <" + stack.getFileName()+":"+stack.getLineNumber() + "> Statement: " + messageBuilder.toString());
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,8 +86,7 @@ public class UpdateManager {
|
|||||||
getTempParams().forEach(v -> { messageBuilder.append((added.get()) ? ", " : "").append(v);added.set(true); });
|
getTempParams().forEach(v -> { messageBuilder.append((added.get()) ? ", " : "").append(v);added.set(true); });
|
||||||
StackTraceElement[] st = Thread.currentThread().getStackTrace();
|
StackTraceElement[] st = Thread.currentThread().getStackTrace();
|
||||||
StackTraceElement stack = st[st.length-1];
|
StackTraceElement stack = st[st.length-1];
|
||||||
connection.getLogManager()
|
LOG.debug("DEBUG <" + stack.getFileName()+":"+stack.getLineNumber() + "> Statement: " + messageBuilder.toString());
|
||||||
.sendInfo("DEBUG <" + stack.getFileName()+":"+stack.getLineNumber() + "> Statement: " + messageBuilder.toString());
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user