3

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.

It seems the logger is handling the file, as it creates a chatlog.log.lck file.

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("WeChat");

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

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

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

}

Again I can get input in the console, just no the file

[23:48:24 INFO]: [WeChat] [Channel TheLounge] WASasquatch: Hello everyone!?
[23:48:30 INFO]: [WeChat] [Channel TheLounge] WASasquatch: Test test
[23:48:36 INFO]: [WeChat] [Channel TheLounge] WASasquatch: This in the log file now?
  • Try setting the log level on `fh`. Maybe it is too high to print anything. – Thilo May 05 '17 at 05:56
  • @AxelH I do not understand what you mean? @Thilo I am using `logger.info()` and `logger.warning()` which both present their own logging levels, one which is low. – JordanSasquatchMan May 05 '17 at 05:57
  • `logger.info("foo");` after the try catch did what was expected .. this is not reproducible. Please provide a [mcve] – AxelH May 05 '17 at 06:48

1 Answers1

0

There is a constructor in FileHandler to set the file as append or not. (and to set a rolling file system if needed, same constructor without the rolling system exist).

public FileHandler(String pattern,
       int limit,
       int count,
       boolean append)
        throws IOException,
               SecurityException

Defined the level for this handler if needed, defaults to Level.ALL

Here is a quick sample :

public class Main {

    static Logger logger;

    public static void main(String[] args) {
        logger = Logger.getLogger("Main");
        FileHandler fh;

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

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


        try {
            fh = new FileHandler(lf.getCanonicalPath(), 8096, 1, true);
            logger.addHandler(fh);
            logger.setLevel(Level.INFO);
            SimpleFormatter fmt = new SimpleFormatter();
            fh.setFormatter(fmt);
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        logger.info("foo");
        logger.info("bar");
    }
}

Note the update to use the File instance path instead of recreate the String.

I use an none existant folder. Launch the process twice and as expected, the file as the line for both execution.

AxelH
  • 14,325
  • 2
  • 25
  • 55
  • This is causing Spigot to hang at enabling my plugin. Changed code: https://pastebin.com/pFB38iTN – JordanSasquatchMan May 05 '17 at 06:09
  • Eventually ends in a `java.lang.OutOfMemoryError: GC overhead limit exceeded` on the FileHandler constructor. https://pastebin.com/0st6JgJJ – JordanSasquatchMan May 05 '17 at 06:17
  • Ok. So I lowered the limit all the way down to 1mb and it was able to start the server, but this doesn't work. It just outputs to the console now and creates a `chatlog.log.0` and `chatlog.log.0.lck` file and doesn't write to it, or `chatlog.log` – JordanSasquatchMan May 05 '17 at 06:26
  • See my updated post. I get no double entries in my console, just single entries, and nothing in the file. – JordanSasquatchMan May 05 '17 at 06:41
  • @JordanSasquatchMan The double entry is because of the consoleHandler I used (not necessery at all). But if I simply add `logger.info("foo");` at the end of your code, I get a file with a content and the console output. Please provide a [mcve] to reproduce this – AxelH May 05 '17 at 06:44
  • It must be conflicting with me method to retrieve the logger which is `getLogger()` in the same method. Going to test renaming it. – JordanSasquatchMan May 05 '17 at 07:04
  • Yes it was conflicting. I had to cast to my Main. Somehow it was able to use the name from my new logger, but was retrieving Bukkit's logger. – JordanSasquatchMan May 05 '17 at 07:08
  • @JordanSasquatchMan Check your imports, you probably used the wrong one. This is the risk to use already used Class name, permitted but still risky – AxelH May 05 '17 at 07:13
  • It was the method name `getLogger()` which is present in JavaPlugin as well as my main WeChat. I renamed it. The issue now is suddenly, now that it works, it ignores the name "WeChat" and prints the **entire** package, and method it was used in. `May 05, 2017 12:07:07 AM wa.was.wechat.events.Commands onCommand INFO: test` So i no longer get `[WeChat] test` in console but just `test` – JordanSasquatchMan May 05 '17 at 07:22