Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ml.neoforex;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Properties;
- /*
- * NeoForex 2015. www.neoforex.ml
- * Only for NeoForex projects.
- * Programmer Name: Daniel Hurtado(Forex) Email: forest_project@hotmail.es/forex.neoforex.ml
- * Contact to Programmer for report bugs/Error's.
- */
- public class Logger {
- // Vars with default values.
- private boolean isEnable = false;
- private boolean fileLogEnable = true;
- private String fileSaveFolder = "logs/";
- // Class information
- private String className = "";
- // System properties
- private Properties systemProperties;
- // Get vars for JVM..
- public Logger(Class<?> classInfo) {
- // Get System Properties...
- this.systemProperties = System.getProperties();
- // Get default properties...
- this.isEnable = Boolean.parseBoolean(systemProperties
- .getProperty("ml.neoforex.sockets.consoleEnable"));
- this.fileLogEnable = Boolean.parseBoolean(systemProperties
- .getProperty("ml.neoforex.sockets.fileLogEnable"));
- // Set Logger url
- if (this.fileLogEnable) {
- if (!this.fileSaveFolder.equals(systemProperties
- .getProperty("ml.neoforex.sockets.fileSaveFolder"))
- && systemProperties
- .getProperty("ml.neoforex.sockets.fileSaveFolder") != null) {
- this.fileSaveFolder = systemProperties
- .getProperty("ml.neoforex.sockets.fileSaveFolder");
- }
- }
- // Check folder
- this.checkSaveFolder();
- // Get the className and save class...
- this.className = classInfo.getCanonicalName();
- // Start writing to log files
- writeFileAlert("- Alert log started at: ".concat(this.getNowTime())
- .concat(" -"));
- }
- // Output the alert and write if is necessary in console and log file.
- public void writeAlert(Object message) {
- StringBuilder messageBuilder = new StringBuilder();
- messageBuilder.append("[".concat(this.className.toUpperCase())
- .concat("][ALERT][").concat(this.getNowTime()).concat("]-> "));
- try {
- messageBuilder.append(message);
- if (this.isEnable)
- System.err.println(messageBuilder.toString());
- if (this.fileLogEnable)
- this.writeFileAlert(messageBuilder.toString());
- } catch (Exception e) {
- e.printStackTrace();
- messageBuilder = null;
- } finally {
- messageBuilder = null;
- }
- }
- // Write to Alert log file.
- private void writeFileAlert(String message) {
- FileWriter alertFile = null;
- try {
- // Start FileWriter -> This put the text in the last line.
- alertFile = new FileWriter(this.fileSaveFolder.concat("alert.log"),
- true);
- // Write, flush and close the FileWrite
- alertFile.write(message + "\r\n");
- alertFile.flush();
- alertFile.close();
- } catch (Exception e) {
- e.printStackTrace();
- alertFile = null;
- } finally {
- // Delete that for clean RAM.
- alertFile = null;
- }
- }
- // Check folder and log files if not exist and create that...
- private void checkSaveFolder() {
- // Get folder
- File saveFolder = new File(this.fileSaveFolder);
- // check if that exist :D
- if (!saveFolder.exists())
- saveFolder.mkdir();
- // Create file if that not exist.
- saveFolder = new File(this.fileSaveFolder.concat("alert.log"));
- if (!saveFolder.exists()) {
- FileWriter alertFile;
- try {
- alertFile = new FileWriter(
- this.fileSaveFolder.concat("alert.log"));
- PrintWriter pw = new PrintWriter(alertFile);
- pw.println("- Alert log started at: ".concat(this.getNowTime())
- .concat(" -"));
- pw.flush();
- pw.close();
- alertFile.close();
- pw = null;
- alertFile = null;
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- saveFolder = null;
- }
- // Get time in string...
- private String getNowTime() {
- Date nowTime = new Date();
- SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/y hh:mm:ss");
- return dateFormat.format(nowTime);
- }
- // This void is called by the gc.
- protected void finalize() {
- // Remove all.
- this.isEnable = false;
- this.fileLogEnable = false;
- this.fileSaveFolder = null;
- this.className = null;
- this.systemProperties = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement