javaloggingoutputundefined-symbol

Java: Logger.addHandler is undefined


Like the title says, the addHandler(Handler handler) void for the Logger class appears to be undefined in my script.

For context, I am running the program on IntelliJ IDEA. I am trying to use the addHandler command in order to specify a file destination for the output log file that the Logger class writes to.

I've been told that the output log file should be in the Sun\Java\Deployment\log directory but the folder is completely empty.

In all honesty, I could really care less where the log file is written to. But, seeing as it's not in the folder it's supposed to be in, I thought specifying a different destination would do the trick.

This problem also seems to occur with other voids such as getHandlers() so the issue might be more serious than I imagined.

A snippet of my code:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Main {
     private final static Logger LOGGER = LogManager.getLogger(Main.class.getName());

     public static void main(String... args){
        FileHandler fileHandler;

        try {
            fileHandler = new FileHandler("log.txt");
            SimpleFormatter formatter = new SimpleFormatter();
            LOGGER.addHandler(fileHandler); // error
            fileHandler.setFormatter(formatter);
        } catch (SecurityException | IOException e) {
            e.printStackTrace();
        }
}

And when I run the program, the build error that follows:

java: cannot find symbol
  symbol:   method addHandler(java.util.logging.FileHandler)
  location: variable LOGGER of type org.apache.logging.log4j.Logger

Maven dependency setup

<dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.17.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.17.2</version>
        </dependency>
    </dependencies>

Any and all help is greatly appreciated.


Solution

  • Check imports of logger. Java.util.logging.* provides addHandler(). Check this article for more information