Creating new Logger

By JordanSasquatchMan

I am trying to create a new logger for my plugin. However it does not write any content to the file. The logging events provide no errors. If I do not supply a handler I can get all the log events in the console.

Subsequently, is there a way to also log what is logged to my “new” logger, to the console as well?

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();
    }
}

Source: Stack Overflow

    

Share it with your friends!

    Fatal error: Uncaught Exception: 12: REST API is deprecated for versions v2.1 and higher (12) thrown in /home/content/19/9652219/html/wp-content/plugins/seo-facebook-comments/facebook/base_facebook.php on line 1273