diff --git a/.gitignore b/.gitignore index 4e0aa06..a94fe95 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,5 @@ bin/ .DS_Store /API +/logs +/src/main/java/test \ No newline at end of file diff --git a/src/main/java/pl/nightdev701/OpenAPI.java b/src/main/java/pl/nightdev701/OpenAPI.java index d3628d5..9a7548f 100644 --- a/src/main/java/pl/nightdev701/OpenAPI.java +++ b/src/main/java/pl/nightdev701/OpenAPI.java @@ -19,33 +19,75 @@ import pl.nightdev701.key.UniqueValueKey; import pl.nightdev701.key.ValueKey; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; import java.util.UUID; public class OpenAPI { - /** encrypt and decrypt strings */ + /** + * encrypt and decrypt strings + */ public static CryptoManager getCryptoManager(String value) { return new CryptoManager(value); } - /** manage database connections and command easy */ + /** + * manage database connections and command easy + */ public static DatabaseConnector getDatabaseManager(DatabaseFormular formular, DatabaseType type) { return new DatabaseConnector(formular, type); } - /** request http and handle it */ + /** + * request http and handle it + */ public static HttpRequestHandler getRequestHandler(String url) { return new HttpRequestHandler(url); } - /** key to save values */ - public static BaseKey getValueKey(String value){ + /** + * key to save values + */ + public static BaseKey getValueKey(String value) { return ValueKey.getKey(value); } - /** key to save uuid */ - public static BaseKey getValueKey(UUID value){ + /** + * key to save uuid + */ + public static BaseKey getValueKey(UUID value) { return UniqueValueKey.getKey(value); } + /** + * print date and clock + */ + public static String getCurrentClockDate() { + SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); + Date date = new Date(System.currentTimeMillis()); + return sdf.format(date); + } + + /** + * print only date + */ + public static String getCurrentDay() { + SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy"); + Date date = new Date(System.currentTimeMillis()); + return sdf.format(date); + } + + /** + * show only clock + */ + public static String getClockTime() { + DateFormat dff = new SimpleDateFormat("HH:mm:ss"); + Date today = Calendar.getInstance().getTime(); + return dff.format(today); + } + + } diff --git a/src/main/java/pl/nightdev701/base/BaseKey.java b/src/main/java/pl/nightdev701/base/BaseKey.java index 9dd0d48..cdee007 100644 --- a/src/main/java/pl/nightdev701/base/BaseKey.java +++ b/src/main/java/pl/nightdev701/base/BaseKey.java @@ -10,7 +10,9 @@ */ -/** base for keys */ +/** + * base for keys + */ public interface BaseKey { Object baseValue(); diff --git a/src/main/java/pl/nightdev701/crypto/CryptoManager.java b/src/main/java/pl/nightdev701/crypto/CryptoManager.java index b3b8e71..2fa52f1 100644 --- a/src/main/java/pl/nightdev701/crypto/CryptoManager.java +++ b/src/main/java/pl/nightdev701/crypto/CryptoManager.java @@ -23,7 +23,9 @@ public CryptoManager(String encryptionKey) { this.encryptionKey = encryptionKey; } - /** encrypt data */ + /** + * encrypt data + */ public String encrypt(String plainText) throws Exception { SecretKeySpec secretKey = new SecretKeySpec(this.encryptionKey.getBytes(StandardCharsets.UTF_8), "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); @@ -32,7 +34,9 @@ public String encrypt(String plainText) throws Exception { return Base64.getEncoder().encodeToString(encryptedBytes); } - /** decrypt data */ + /** + * decrypt data + */ public String decrypt(String encryptedText) throws Exception { SecretKeySpec secretKey = new SecretKeySpec(this.encryptionKey.getBytes(StandardCharsets.UTF_8), "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); diff --git a/src/main/java/pl/nightdev701/database/DatabaseConnector.java b/src/main/java/pl/nightdev701/database/DatabaseConnector.java index 5a54745..e00f505 100644 --- a/src/main/java/pl/nightdev701/database/DatabaseConnector.java +++ b/src/main/java/pl/nightdev701/database/DatabaseConnector.java @@ -61,9 +61,11 @@ public DatabaseConnector(DatabaseFormular formular, DatabaseType databaseType, A } } - /** connect to database */ + /** + * connect to database + */ public void connect() throws SQLException { - logger.log(Level.INFO,"Connect to database, with the type \"" + databaseType.name() + "\"!"); + logger.log(Level.INFO, "Connect to database, with the type \"" + databaseType.name() + "\"!"); if (databaseType == DatabaseType.MYSQL) { connection = DriverManager.getConnection("jdbc:mysql://" + ip + ":" + port + "/" + database + "?autoReconnect=true" + @@ -78,15 +80,19 @@ public void connect() throws SQLException { logger.log(Level.INFO, "Connected to database!"); } - /** close connection */ + /** + * close connection + */ public void close() throws SQLException { if (isConnected()) { connection.close(); - logger.log(Level.INFO,"Connection closed"); + logger.log(Level.INFO, "Connection closed"); } } - /** read value from database */ + /** + * read value from database + */ public Object getDatabaseStatement(String command, String data) throws SQLException { PreparedStatement statement = connection.prepareStatement(command); ResultSet result = statement.executeQuery(); @@ -94,13 +100,17 @@ public Object getDatabaseStatement(String command, String data) throws SQLExcept return result.getObject(data); } - /** execute action to database */ + /** + * execute action to database + */ public void executeDatabaseStatement(String command) throws SQLException { PreparedStatement statement = connection.prepareStatement(command); statement.executeUpdate(); } - /** check if connected */ + /** + * check if connected + */ public boolean isConnected() throws SQLException { if (connection == null) { return false; @@ -108,12 +118,16 @@ public boolean isConnected() throws SQLException { return !connection.isClosed(); } - /** get port */ + /** + * get port + */ public int getPort() { return port; } - /** set port */ + /** + * set port + */ public void setPort(int port) { this.port = port; } diff --git a/src/main/java/pl/nightdev701/http/HttpRequestHandler.java b/src/main/java/pl/nightdev701/http/HttpRequestHandler.java index f1cfdfa..a5a2e1c 100644 --- a/src/main/java/pl/nightdev701/http/HttpRequestHandler.java +++ b/src/main/java/pl/nightdev701/http/HttpRequestHandler.java @@ -15,7 +15,9 @@ public HttpRequestHandler(String url) { this.url = url; } - /** fire request */ + /** + * fire request + */ public void request() { HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() @@ -31,7 +33,9 @@ public void request() { } } - /** read body */ + /** + * read body + */ public String getBody() { return body; } diff --git a/src/main/java/pl/nightdev701/key/UniqueValueKey.java b/src/main/java/pl/nightdev701/key/UniqueValueKey.java index 5a2e46a..fd06c01 100644 --- a/src/main/java/pl/nightdev701/key/UniqueValueKey.java +++ b/src/main/java/pl/nightdev701/key/UniqueValueKey.java @@ -14,17 +14,23 @@ import java.util.UUID; -/** uuid key */ +/** + * uuid key + */ public class UniqueValueKey implements BaseKey { T key; - /** create key */ + /** + * create key + */ private UniqueValueKey(T key) { this.key = key; } - /** read value */ + /** + * read value + */ public static UniqueValueKey getKey(UUID key) { return new UniqueValueKey<>(key); } diff --git a/src/main/java/pl/nightdev701/key/ValueKey.java b/src/main/java/pl/nightdev701/key/ValueKey.java index a743cd2..2d60a8a 100644 --- a/src/main/java/pl/nightdev701/key/ValueKey.java +++ b/src/main/java/pl/nightdev701/key/ValueKey.java @@ -12,7 +12,9 @@ import pl.nightdev701.base.BaseKey; -/** string key */ +/** + * string key + */ public class ValueKey implements BaseKey { T key; @@ -23,16 +25,18 @@ private ValueKey(T key) { /** * create key + * * @return key value - * */ + */ public static ValueKey getKey(Object key) { return new ValueKey<>(key.toString()); } /** * read value + * * @return key value - * */ + */ @Override public Object baseValue() { return key; diff --git a/src/main/java/pl/nightdev701/logger/standard/DefaultLogger.java b/src/main/java/pl/nightdev701/logger/standard/DefaultLogger.java index c66e9ff..1da52d2 100644 --- a/src/main/java/pl/nightdev701/logger/standard/DefaultLogger.java +++ b/src/main/java/pl/nightdev701/logger/standard/DefaultLogger.java @@ -10,8 +10,11 @@ */ +import pl.nightdev701.OpenAPI; import pl.nightdev701.logger.AbstractLogger; +import java.io.*; +import java.nio.charset.StandardCharsets; import java.util.logging.Level; import java.util.logging.Logger; @@ -19,8 +22,31 @@ public class DefaultLogger extends AbstractLogger { @Override public void log(Level level, String msg) { + + File logFolder = new File("logs"); + + if (!logFolder.exists()) { + logFolder.mkdirs(); + } + Logger logger = Logger.getLogger("openapi"); logger.log(level, msg); + + String result = OpenAPI.getClockTime() + " | " + level + " | " + msg; + + try { + File file = new File("logs/events-" + OpenAPI.getCurrentDay() + ".log"); + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), StandardCharsets.UTF_8)); + writer.write(result + "\n"); + writer.flush(); + + System.out.println(result); + } catch (IOException e) { + System.err.println("Failed to log! " + e.getMessage()); + e.printStackTrace(); + System.out.println("(" + OpenAPI.getCurrentClockDate() + ") Log data: " + level + ", " + msg); + } + } }