Search code examples
javaintellij-idealogbackslf4j

Unable to access logger: "cannot resolve symbol (info|warning|debug)"


I'm implementing SLF4J with logback into my Java project, which I thought was a breeze. It should've been a breeze. It wasn't.

My dilemma is this: IntelliJ doesn't recognise that the logger object I'm creating exists and won't build because of that error. Here's the code below.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main {
    public static final Logger logger = LoggerFactory.getLogger(Main.class);

    // Will replace with config values
    private static final String token  = "bottoken";
    private static final String prefix = "!";

    logger.info("This info to the left is red");
}

I've tried invalidating the cache as suggested by many and deleting the IntelliJ filesystem, but to no avail. It even got to the point where the whole project was deleted then recreated in a different folder. Nothing.

The show up in my libraries list in the Project Settings and reloading Maven project doesn't help either.

I've added SLF4J through Maven as shown in the pom.xml below:

<dependencies>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.22</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.22</version>
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.8</version>
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.1.8</version>
    </dependency>
</dependencies>

It all checks out in the listed libraries and all other dependencies (that aren't listed) show up and compile fine.


Solution

  • The code you have does not wrap the line in a method.

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class Main {
        public static final Logger logger = LoggerFactory.getLogger(Main.class);
    
        // Will replace with config values
        private static final String token  = "bottoken";
        private static final String prefix = "!";
    
        public static void main(String[] args) {
            logger.info("This info to the left is red");
        }
    }