Simple Logger

Discussion in 'Resources' started by Uniclaw, Nov 7, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Uniclaw

    Hi!

    Here the Code for a super easy Logger, and a example how to use it.

    Code (Just copy it in a Class named Logger - Don't forget to add the package!):
    Show Spoiler

    Code:
     
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
     
    public class Logger {
     
     
    /**
    *
    * SimpleLogger - Class
    * @author Uniclaw
    *
    *
    * How using it?
    *
    * Just declare in you'r onEnable() Method a new Instance of "Logger" (Logger yournameforlogger = new Logger(All the Arguments);
    * The Arguments:
    * boolean d - On Logging, would you log the date? (Like in the Bukkit Console)
    * DateFormat df - If you use the Date, give here the DateFormat
    * All the Strings wich starts with bracket: There are the Brackets for the Date and the Name.
    */
     
    protected static File f;
    protected static boolean d;
    protected static DateFormat dateF;
    protected static String bod;
    protected static String bop;
    protected static String bcd;
    protected static String bcp;
    protected static ArrayList<String> prelog = new ArrayList<String>();
     
     
    public Logger(boolean d, DateFormat df, File f, String bracketOD, String bracketCD, String bracketOP, String bracketCP){
    Logger.d = d;
    Logger.dateF = df;
    Logger.f = f;
    Logger.bod = bracketOD;Logger.bop = bracketOP;
    Logger.bcd = bracketCD;Logger.bcp = bracketCP;
    createLog(f);
    }
     
    public Logger(File f, String bracketOD, String bracketCD, String bracketOP, String bracketCP){
    Logger.d = true;
    Logger.dateF = new SimpleDateFormat("dd/MM/YY HH:mm:ss");
    Logger.f = f;
    Logger.bod = bracketOD;Logger.bop = bracketOP;
    Logger.bcd = bracketCD;Logger.bcp = bracketCP;
    createLog(f);
    }
     
    public void log(String logMsg, String name){
            BufferedWriter bw;
     
            DateFormat dateFormat;
            if(dateF == null){
          dateFormat = new SimpleDateFormat("HH:mm:ss dd/MM");
            }else{
          dateFormat = dateF;
            }
     
            Date date = new Date();
            String datum = bod + dateFormat.format(date) + bcd;
     
    try {
    bw = new BufferedWriter(new FileWriter(f, true));
    if(d){
    bw.write(datum + bop + name+ bcp + ": " + logMsg);
    }else{
    bw.write(bop + name+ bcp + ": " + logMsg);
    }
          bw.newLine();
          bw.close();
    } catch (IOException e) {
    }
    }
     
    public ArrayList<String> getList(){
    return prelog;
    }
     
    public void logLC(String logMsg, String name){
    BufferedWriter bw;
     
            DateFormat dateFormat;
            if(dateF == null){
          dateFormat = new SimpleDateFormat("HH:mm:ss dd/MM");
            }else{
          dateFormat = dateF;
            }
     
            Date date = new Date();
            String datum = bod + dateFormat.format(date) + bcd;
     
    try {
    bw = new BufferedWriter(new FileWriter(f, true));
    if(d){
    bw.write(datum + bop + name+ bcp + ": " + logMsg);
    System.out.println(datum + bop + name+ bcp + ": " + logMsg);
    }else{
    bw.write(bop + name+ bcp + ": " + logMsg);
    System.out.println(bop + name+ bcp + ": " + logMsg);
    }
          bw.newLine();
          bw.close();
    } catch (IOException e) {
    }
    }
     
    public void memorize(String logMsg, String name){
            DateFormat dateFormat;
            if(dateF == null){
          dateFormat = new SimpleDateFormat("HH:mm:ss dd/MM");
            }else{
          dateFormat = dateF;
            }
     
            Date date = new Date();
            String datum = bod + dateFormat.format(date) + bcd;
            String n;
    if(d){
    n = datum + bop + name+ bcp + ": " + logMsg;
    }else{
    n = bop + name+ bcp + ": " + logMsg;
    }
    prelog.add(n);
    }
     
    public void logMemory(){
            BufferedWriter bw;
     
    try {
    bw = new BufferedWriter(new FileWriter(f, true));
    for(String s:prelog){
    bw.write(s);
    bw.newLine();
    }
          bw.close();
    } catch (IOException e) {
    }
    }
     
    protected static void createLog(File f){
    try {
      File file = f;
     
      if(! file.exists()){
      file.createNewFile();
      }
    } catch (IOException e) {
    }
    }
     
    }
    



    How to use it?

    Its very simple!
    Just make a new instance of the Logger in your main class, in the onEnable Method:
    Code:
    Logger nameOfYourLogger = new Logger(Boolean b, DateFormat  df, File f, String bracketOD, String bracketCD, String bracketOP, String bracketCP);
    Explanation:

    If the Boolean b is true, the Logger will use the Date in the Logfile.

    DateFormat df: If you use the Date, and would have a specific DateFormat, just give it - If you make it "null", the Logger will automatically use "HH:mm:ss dd/MM"

    File f is the File wich the Logger must create.

    The Strings bracketOD, bracketCD, bracketOP, bracketCP are the brackets for the Date and the logged user - just look in the example.


    Now, with
    Code:
    nameOfYourLogger.log(Stringtolog, Stringloggedname); , you will logg a msg.

    Example in use:

    In the Main (in the example he is named "Main"):
    Code:
     
    public static Logger;
    public void onEnable(){
        Logger = new Logger(true, null, new File(this.getDataFolder(), "Log.log"), "[", "]", "{", "}");
    }
    
    In a Listener:
    Code:
    @EventHandler
    public void break(BlockBreakEvent e){
    Player p = e.getPlayer();
    Main.Logger.log("Has destroyed a Block!", p.getName();
    }
    
    Those two code will output this on every Block-destroying in the FIle log.log :

    Code:
    [22:48:07 19/10]{Playername} :  Has destroyed a Block!
    

    And there are other Methods.. :

    Code:
    logLC(String logMsg, String name);
    - The same as logg, just that this logg in the file and in the console.

    Code:
    memorize(String logMsg, String name);
    - Memorize a logString.

    Code:
    logMemory();
    - Log all memorized Strings

    Code:
    getList();
    - returns a ArrayList wich all memorized Strings in it

    Greet

    #Added 4 Methods -

    #1. loggLC(String msg, String loggedname); - Same as logg(String msg, String loggedname), but this logg the message in the console and in the file

    #2. memorize(String logMsg, String name); - memorize the LogString

    #3. loggMemory(); - Logg all memorized Strings

    #4. getList(); - return a ArrayList with all memorized strings in it

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  2. Offline

    Icyene

    Nice! But why is the method called logg?
     
    Uniclaw likes this.
  3. Offline

    Uniclaw

    Thanks! Oh, fail xD

    #changed
     
    Icyene likes this.
  4. Offline

    thehutch

    You should add other constructors for example the majority of people will just use the standard date format ("dd/MM/YY HH:mm:ss") <== I believe this is correct :D
     
  5. Offline

    Uniclaw

    Yeah, you are right :D !

    #added :)
     
Thread Status:
Not open for further replies.

Share This Page