Custom Log File?

Discussion in 'BungeeCord Plugin Development' started by jay275475, Apr 1, 2015.

  1. Can someone show me where to find how to make custom log files?
     
  2. You can always use getConfig() if your not going to end up using it for anything else in the plugin.
     
    • Optimistic Optimistic x 1
  3. I used this in one of my plugins.
    Here is the code of the class:
    Code (Text):
    Reporter plugin;
        File file;
       
        public FileManager(Reporter plugin) {
            this.plugin = plugin;
            System.out.println(plugin.getDataFolder()+"");
            file = new File(plugin.getDataFolder() + "/reports.txt");
        }
       
        public void addMessage(String message){
            try {
                BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
                bw.append(message);
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
       
        public void setup(){
            if(!file.exists()){
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
       
        public void formatMessage(String msg, String name){
            DateFormat dateFormat = new SimpleDateFormat("dd/MM HH:mm:ss");
            String date = dateFormat.format(new Date());
            String finalMessage = date + " - "+ name + ": " + msg + "\n";
            addMessage(finalMessage);
        }
    Yes, it is on a text file, but it is similar at what you would like.
     
  4. PHP:
     Handler fileHandler = new FileHandler(getDataFolder() + File.separator + "MyLog.log.lck", true);
                fileHandler.setFormatter(new MyFormatter());
                for (Handler h : logger.getHandlers()){
                  logger.removeHandler(h);
            }
                logger.setUseParentHandlers(false);
       
                logger.addHandler(fileHandler);
       
                logger.setLevel(Level.ALL);
    For some reason this doesn't log everything but the startup.
     
  5. Just add the file handler to getLogger(). Don't remove anything and don't change anything else. Like the example I provided above.