Advertisement
Lauda

sLog

Jan 10th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. /***********************************************************************
  2.  * Module:  sLog.java
  3.  * Author:  Goran Todorovic
  4.  * Purpose: Defines the class for event logs (GEER logger class)
  5.  ***********************************************************************/
  6. package geer.app.config;
  7.  
  8. import java.io.File;
  9. import java.io.FileWriter;
  10. import java.io.PrintWriter;
  11. import java.io.StringWriter;
  12. import java.text.DateFormat;
  13. import java.text.SimpleDateFormat;
  14. import java.util.Date;
  15.  
  16. /**
  17.  * Log class
  18.  * @author Goran Todorovic
  19.  *
  20.  */
  21. public class sLog {
  22.     /**
  23.      * Log file path
  24.      */
  25.     private static String logFile = "geer.log";
  26.    
  27.     /**
  28.      * Date format
  29.      */
  30.     private final static DateFormat df = new SimpleDateFormat ("dd.MM.yyyy  hh:mm:ss");
  31.  
  32.     /**
  33.      * Class constructor without params
  34.      */
  35.     private sLog() { }
  36.  
  37.     /**
  38.      * Set log file
  39.      * @param filename
  40.      */
  41.     public static void setLogFile(String filename) {
  42.         logFile = filename;
  43.        
  44.         new File(filename).delete();
  45.  
  46.         try {
  47.             write("Log file initiated : " + filename);
  48.         }
  49.         catch (Exception e) {
  50.             System.out.println(stack2string(e));
  51.         }
  52.  
  53.     }
  54.  
  55.     /**
  56.      * Write string to log file
  57.      * @param msg
  58.      */
  59.     public static void write(String msg) {
  60.         write(logFile, msg);
  61.     }
  62.  
  63.     /**
  64.      * Write console output
  65.      * @param msg
  66.      */
  67.     public static void writeConsole(String msg) {
  68.         Date now = new Date();
  69.         String currentTime = sLog.df.format(now);
  70.         System.out.println("[" + currentTime + "] sLog: " + msg);
  71.     }
  72.    
  73.     /**
  74.      * Write exception to log file
  75.      * @param e
  76.      */
  77.     public static void write(Exception e) {
  78.         write(logFile, stack2string(e));
  79.     }
  80.  
  81.     /**
  82.      * Write string to file chosen by user
  83.      * @param file - Input file
  84.      * @param msg - string message
  85.      */
  86.     public static void write(String file, String msg) {
  87.         try {
  88.             Date now = new Date();
  89.             String currentTime = sLog.df.format(now);
  90.             FileWriter aWriter = new FileWriter(file, true);
  91.             aWriter.write(currentTime + " " + msg + System.getProperty("line.separator"));
  92.             System.out.println(currentTime + " " + msg);
  93.             aWriter.flush();
  94.             aWriter.close();
  95.         }
  96.         catch (Exception e) {
  97.             System.out.println(stack2string(e));
  98.         }
  99.     }
  100.  
  101.     /**
  102.      * Convert exception to string and write to file
  103.      * @param e
  104.      * @return
  105.      */
  106.     private static String stack2string(Exception e) {
  107.         try {
  108.             StringWriter sw = new StringWriter();
  109.             PrintWriter pw = new PrintWriter(sw);
  110.             e.printStackTrace(pw);
  111.             return "------\r\n" + sw.toString() + "------\r\n";
  112.         }
  113.         catch(Exception e2) {
  114.             return "Error: Bad stack2string!";
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement