Solved New Logger not logging to file

Discussion in 'Spigot Plugin Development' started by WAS, May 5, 2017.

  1. WAS

    WAS

    I have created a logger to make reading chat events easier, however the instance doesn't seem to be logging to the file. No errors are provided from any of the logging events. Just nothing in the file.

    The logger does seem to be opening the file for writing, as it creates a chatlog.log.lck file. Just no content is ever written.

    This is how I setup my logger.
    Code (Java):
      private void setupLogger() {

         logger = Logger.getLogger(getDescription().getName());
         FileHandler fh;

         File lf = new File(getDataFolder() + File.separator + "logs" + File.separator + "chatlog.log");

      lf.getParentFile().mkdirs();
         if (lf.exists()) {
           if (lf.length() > getConfig().getLong("max-log-size", 20000000)) {
             LocalDateTime ct = LocalDateTime.now();
             byte[] buffer = new byte[1024];
             GZIPOutputStream gzos;
             try {
               gzos = new GZIPOutputStream(new FileOutputStream(
                   getDataFolder() +  File.separator + "logs" + File.separator + "chatlog-" + ct.toLocalDate() + ".log.gz"));
               FileInputStream in = new FileInputStream(lf);

               int len;
               while ((len = in.read(buffer)) > 0) {
                 gzos.write(buffer, 0, len);
               }

               in.close();

               gzos.finish();
               gzos.close();

             } catch (IOException e) {
               e.printStackTrace();
             }
             lf.delete();
          if (!lf.exists()){
          try {
                 lf.createNewFile();
               } catch (IOException e) {
                 e.printStackTrace();
               }
          }
           }
         } else {
        lf.getParentFile().mkdirs();
        if (!lf.exists()){
        try {
               lf.createNewFile();
             } catch (IOException e) {
               e.printStackTrace();
             }
        }
         }

         try {
           fh = new FileHandler(getDataFolder() + File.separator + "logs" + File.separator + "chatlog.log");
           logger.addHandler(fh);
           SimpleFormatter fmt = new SimpleFormatter();
           fh.setFormatter(fmt);
         } catch (SecurityException e) {
           e.printStackTrace();
         } catch (IOException e) {
           e.printStackTrace();
         }

       }
     
    #1 WAS, May 5, 2017
    Last edited: May 5, 2017
  2. WAS

    WAS

    Figured it out. Because I added a Main#getLogger() method, which of course was a logger, it was conflicting with JavaPlugin#getLogger(). The weirdest part is it was using the Main's name, but using the rest of JavaPlugin's settings.

    Final method
    Code (Java):
      private void setupLogger() {

         logger = Logger.getLogger(getDescription().getName());

         File lf = new File(getDataFolder() + File.separator + "logs");

         if (!(lf.exists())){
           lf.mkdirs();
         }

         try {
           FileHandler fh = new FileHandler(lf.getCanonicalPath() + File.separator + "chat.log", true);
           fh.setFormatter(new ConsoleFormatter());
           logger.addHandler(fh);
           logger.setLevel(Level.INFO);
         } catch (SecurityException | IOException e) {
           e.printStackTrace();
         }

       }
     
    #2 WAS, May 5, 2017
    Last edited: May 5, 2017