java - Non-static method 'getLogger' cannot be referenced from a static context

Daredevil :

I am trying to create a simple logging application in Java using log4j2.

This is how it looks:

package com.company;

import java.io.IOException;

import java.util.logging.LogManager;
import java.util.logging.Logger;


    public class Main {

       private static final Logger logger = LogManager.getLogger(Main.class);

        public static void main(String[] args) throws IOException {

            String message = "Hello there!";
            System.out.println(message);
            logger.info(message);

        }
    }

There is an error on getLogger claiming:

 Non-static method 'getLogger' cannot be referenced from a static context

I've looked into various threads on this forum but none of them seemed to work. They seemed to declare the logger the same way as I did above.

What am I doing wrong here?

Mickael :

If you're using log4j2 then you imported the wrong LogManager and Logger. You should have this:

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

You should take a look to the documentation : https://logging.apache.org/log4j/2.x/manual/api.html

EDIT: Maven

According to the documentation Using Log4j in your Apache Maven build, you should have the following dependencies in your pom.xml:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

non static method cannot be referenced from a static context

From Dev

non-static method getIntent() cannot be referenced from a static context

From Dev

"Non-static method cannot be referenced from static context" error

From Dev

Non-static method getSocketFactory cannot be referenced from a static context

From Dev

java: non-static variable this cannot be referenced from a static context

From Dev

Non static variable cannot be referenced from static context java

From Dev

non-static variable cannot be referenced from static context [JAVA]

From Dev

non-static variable cannot be referenced from a static context java

From Dev

Non-static method cannot be referenced from a static context when flattening map java 8

From Java

Non-static method cannot be referenced from a static context in java 8 streams

From Java

Non-static method cannot be referenced from a static context in java 8 streams

From Dev

JAVA - non-static method add(E) cannot be referenced from a static context

From Dev

Non-static method 'getSharedPreferences (java.lang.String, int)' cannot be referenced from a static context

From Dev

Non-static method cannot be referenced from a static context - Java to Kotlin - Android

From Dev

Java non-static method cannot be refenced from a static context

From Dev

Error:non static method 'edit' cannot be referenced in static context

From Dev

Trying to get my show-method to work (Non static method cannot be referenced from static context)

From Dev

non-static variable s cannot be referenced from a static context

From Dev

non static variable this cannot be referenced from a static context

From Java

"non-static variable this cannot be referenced from a static context"?

From Java

non-static class cannot be referenced from a static context

From Dev

Non-static variable filepath cannot be referenced from a static context

From Dev

Non-static edit() cannot be referenced from a static context

From Dev

non static setGravity cannot be referenced from static context

From Dev

Non-static variable cannot be referenced from a static context?

From Java

Java - cannot be referenced from a static context

From Dev

Map.merge .. non-static method cannot be referenced from static context

From Dev

non-static method matcher(CharSequence) cannot be referenced from a static context

From Java

Why does String::isEmpty works when non-static method cannot be referenced from a static context?

Related Related

  1. 1

    non static method cannot be referenced from a static context

  2. 2

    non-static method getIntent() cannot be referenced from a static context

  3. 3

    "Non-static method cannot be referenced from static context" error

  4. 4

    Non-static method getSocketFactory cannot be referenced from a static context

  5. 5

    java: non-static variable this cannot be referenced from a static context

  6. 6

    Non static variable cannot be referenced from static context java

  7. 7

    non-static variable cannot be referenced from static context [JAVA]

  8. 8

    non-static variable cannot be referenced from a static context java

  9. 9

    Non-static method cannot be referenced from a static context when flattening map java 8

  10. 10

    Non-static method cannot be referenced from a static context in java 8 streams

  11. 11

    Non-static method cannot be referenced from a static context in java 8 streams

  12. 12

    JAVA - non-static method add(E) cannot be referenced from a static context

  13. 13

    Non-static method 'getSharedPreferences (java.lang.String, int)' cannot be referenced from a static context

  14. 14

    Non-static method cannot be referenced from a static context - Java to Kotlin - Android

  15. 15

    Java non-static method cannot be refenced from a static context

  16. 16

    Error:non static method 'edit' cannot be referenced in static context

  17. 17

    Trying to get my show-method to work (Non static method cannot be referenced from static context)

  18. 18

    non-static variable s cannot be referenced from a static context

  19. 19

    non static variable this cannot be referenced from a static context

  20. 20

    "non-static variable this cannot be referenced from a static context"?

  21. 21

    non-static class cannot be referenced from a static context

  22. 22

    Non-static variable filepath cannot be referenced from a static context

  23. 23

    Non-static edit() cannot be referenced from a static context

  24. 24

    non static setGravity cannot be referenced from static context

  25. 25

    Non-static variable cannot be referenced from a static context?

  26. 26

    Java - cannot be referenced from a static context

  27. 27

    Map.merge .. non-static method cannot be referenced from static context

  28. 28

    non-static method matcher(CharSequence) cannot be referenced from a static context

  29. 29

    Why does String::isEmpty works when non-static method cannot be referenced from a static context?

HotTag

Archive