Util Simple & Efficient Logger

Discussion in 'Resources' started by The Gaming Grunts, Dec 31, 2015.

Thread Status:
Not open for further replies.
  1. A simple logger. Nothing more, nothing less. The main feature of this is that it allows you to dynamically change where the data is saved. For example, if I wanted to log a command that a player issued, I'd do:

    Code:
    Logger.log("Player Notch issued command /some command", LogType.COMMAND);
    And this will save it to the "commands.txt" along with the date and time.

    This is a really simple class that can easily be customized however you want and hopefully you guys find it useful.


    Code:
    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.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public abstract class Logger {
    
        private static SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy hh:mm:ss");
        private static ExecutorService pool = Executors.newCachedThreadPool();
        public final static String PATH = Settlements.getPlugin().getDataFolder().getAbsolutePath() + File.separator + "Logs"; //Replace with your path
        private static File f;
    
        public enum LogType{
            SETTLEMENT, CHUNK, COMMAND, PLAYER; //Change these to whatever  you want
        }
    
        public static void log(final String text, LogType logType){
            switch(logType){
                case CHUNK:
                    f = new File(PATH, "chunks.txt"); //change file names
                    break;
                case SETTLEMENT:
                    f = new File(PATH, "settlements.txt");
                    break;
                case COMMAND:
                    f = new File(PATH, "commands.txt");
                    break;
                case PLAYER:
                    f = new File(PATH, "players.txt");
                    break;
            }
        
            if (!f.exists()){
                try {
                    f.createNewFile();
                } catch(IOException e) {
                    e.printStackTrace();
                }
            }
            pool.submit(new LogWriter(text, f));
        }
    
        private static class LogWriter implements Runnable{
    
            private String text;
            private File file;
        
            public LogWriter(String text, File file){
                this.text = text;
                this.file = file;
            }
        
            @Override
            public void run() {
                try{
                    PrintWriter w = new PrintWriter(new FileWriter(file, true));
                    w.write("[" + sdf.format(new Date()) + "] " + text);
                    w.println();
                    w.close();
                    return;
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
     
    Matthewenderle and Regablith like this.
  2. Offline

    Regablith

    Cool, good work!
     
    The Gaming Grunts likes this.
Thread Status:
Not open for further replies.

Share This Page