I have the log4j-api-2.0.0.jar
and log4j-core-2.0.2.jar
import into my build path. But somehow the following code were fail:
我有log4j-api-2.0.0。jar和log4j-core-2.0.2。jar导入到我的构建路径。但不知何故,下面的代码失败了:
import org.apache.logging.log4j.core.Logger;
public class TheClass {
private static Logger log = Logger.getLogger(TheClass.class);
...
And the error message shows that:
错误信息显示:
The method getLogger(Class<TheClass>) is undefined for the type Logger
方法getLogger(类
I am just so curious is getLogger()
no longer a valid method in Logger?
我很好奇getLogger()不再是Logger中的有效方法吗?
7 个解决方案
#1
19
You'll notice Logger
no longer declares such a method.
您将注意到Logger不再声明这样的方法。
log4j version 2 has made some drastic changes. Here's the change log. getLogger
seems to have been moved to a LogManager
class.
log4j版本2做了一些重大的改变。这是更改日志。getLogger似乎已经被转移到LogManager类。
Here's how they suggest making the migration.
这是他们建议的迁移方式。
#2
6
I'm giving an example for better understanding.
我举个例子,让大家更好地理解。
private static Logger logger;
static {
try {
// you need to do something like below instaed of Logger.getLogger(....);
logger = LogManager.getLogger(ResourceService.class);
} catch (Throwable th) {
throw new HRException("Cannot load the log property file", th);
}
}
#3
3
with new Log4J 2, you have to add at least (in my case) log4j-core-2.8.2, log4j-api-2.8.2 and in some other case you may need to add also log4j-web-2.8.2. So when you want to get a logging you import import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger;
使用新的Log4J 2,您至少需要添加(在我的情况下)Log4J -core-2.8.2, Log4J -api-2.8.2,在一些其他情况下,您可能需要添加Log4J -web-2.8.2。当你想要获取日志时,你需要导入org。apache。logger。log4j。logmanager;进口org.apache.logging.log4j.Logger;
and finally the usage will be static final Logger LOGGER = LogManager.getLogger(WebService.class.getName());
最后使用的是静态最终日志记录器= LogManager.getLogger(WebService.class.getName());
Note: don't forget to put config file into the root directory of the project, other wise you won't be able to get your logs.
注意:不要忘记将配置文件放在项目的根目录中,否则您将无法获得日志。
Hope this will help someone Kind regards
希望这能帮助别人善意的问候
#4
2
Yes your observation is correct.It does not support getLogger() method.
是的,你的观察是正确的。它不支持getLogger()方法。
Check this Documentation link out: http://logging.apache.org/log4j/2.x/log4j-core/apidocs/index.html
检查这个文档链接:http://logging.apache.org/log4j/2.x/log4j-core/apidocs/index.html
Sample tutorial:http://www.javabeat.net/log4j-2-example/
教程示例:http://www.javabeat.net/log4j-2-example/
#5
1
As noted in other answers, Logger
is now an interface and you can get Logger instances from the LogManager
.
正如在其他答案中提到的,Logger现在是一个接口,您可以从LogManager获得Logger实例。
API is now separate from the implementation, to give the team the freedom to change the implementation without breaking user code. The API will rarely change, and if it does change it will be in a 2.x version, not a 2.0.x version. That said, it is probably a good idea to always use matching log4j-api and log4j-core versions.
API现在与实现分离,使团队能够在不破坏用户代码的情况下更改实现。API几乎不会改变,如果它改变了,它会在2中。x版,不是2.0版。x版本。也就是说,最好总是使用匹配的log4j-api和log4j-core版本。
#6
1
You are using log4j version 2.
您正在使用log4j版本2。
private static final Logger LOGGER = LogManager.getLogger(TheClass.class);
#7
0
To use the getLogger() method on your class, import the Logger class
要在您的类中使用getLogger()方法,导入Logger类。
import java.util.logging.Logger;
进口java.util.logging.Logger;
and use it as follows
并使用如下
public class SpringBoot {
private static final Logger LOGGER = Logger.getClass("SpringBoot");
}
And remember, this method takes a string argument.
记住,这个方法有一个字符串参数。
Or Use Logger from org.apache.log4j package as seen below
或者使用来自org.apache的日志程序。log4j包如下所示。
import org.apache.log4j.Logger;
public class MessageProcessor {
private static final Logger LOGGER = Logger.getLogger(MessageProcessor.class);
public void processMessage(Message<String> msg) {
LOGGER.info("Message is about to be processed");
}
#1
19
You'll notice Logger
no longer declares such a method.
您将注意到Logger不再声明这样的方法。
log4j version 2 has made some drastic changes. Here's the change log. getLogger
seems to have been moved to a LogManager
class.
log4j版本2做了一些重大的改变。这是更改日志。getLogger似乎已经被转移到LogManager类。
Here's how they suggest making the migration.
这是他们建议的迁移方式。
#2
6
I'm giving an example for better understanding.
我举个例子,让大家更好地理解。
private static Logger logger;
static {
try {
// you need to do something like below instaed of Logger.getLogger(....);
logger = LogManager.getLogger(ResourceService.class);
} catch (Throwable th) {
throw new HRException("Cannot load the log property file", th);
}
}
#3
3
with new Log4J 2, you have to add at least (in my case) log4j-core-2.8.2, log4j-api-2.8.2 and in some other case you may need to add also log4j-web-2.8.2. So when you want to get a logging you import import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger;
使用新的Log4J 2,您至少需要添加(在我的情况下)Log4J -core-2.8.2, Log4J -api-2.8.2,在一些其他情况下,您可能需要添加Log4J -web-2.8.2。当你想要获取日志时,你需要导入org。apache。logger。log4j。logmanager;进口org.apache.logging.log4j.Logger;
and finally the usage will be static final Logger LOGGER = LogManager.getLogger(WebService.class.getName());
最后使用的是静态最终日志记录器= LogManager.getLogger(WebService.class.getName());
Note: don't forget to put config file into the root directory of the project, other wise you won't be able to get your logs.
注意:不要忘记将配置文件放在项目的根目录中,否则您将无法获得日志。
Hope this will help someone Kind regards
希望这能帮助别人善意的问候
#4
2
Yes your observation is correct.It does not support getLogger() method.
是的,你的观察是正确的。它不支持getLogger()方法。
Check this Documentation link out: http://logging.apache.org/log4j/2.x/log4j-core/apidocs/index.html
检查这个文档链接:http://logging.apache.org/log4j/2.x/log4j-core/apidocs/index.html
Sample tutorial:http://www.javabeat.net/log4j-2-example/
教程示例:http://www.javabeat.net/log4j-2-example/
#5
1
As noted in other answers, Logger
is now an interface and you can get Logger instances from the LogManager
.
正如在其他答案中提到的,Logger现在是一个接口,您可以从LogManager获得Logger实例。
API is now separate from the implementation, to give the team the freedom to change the implementation without breaking user code. The API will rarely change, and if it does change it will be in a 2.x version, not a 2.0.x version. That said, it is probably a good idea to always use matching log4j-api and log4j-core versions.
API现在与实现分离,使团队能够在不破坏用户代码的情况下更改实现。API几乎不会改变,如果它改变了,它会在2中。x版,不是2.0版。x版本。也就是说,最好总是使用匹配的log4j-api和log4j-core版本。
#6
1
You are using log4j version 2.
您正在使用log4j版本2。
private static final Logger LOGGER = LogManager.getLogger(TheClass.class);
#7
0
To use the getLogger() method on your class, import the Logger class
要在您的类中使用getLogger()方法,导入Logger类。
import java.util.logging.Logger;
进口java.util.logging.Logger;
and use it as follows
并使用如下
public class SpringBoot {
private static final Logger LOGGER = Logger.getClass("SpringBoot");
}
And remember, this method takes a string argument.
记住,这个方法有一个字符串参数。
Or Use Logger from org.apache.log4j package as seen below
或者使用来自org.apache的日志程序。log4j包如下所示。
import org.apache.log4j.Logger;
public class MessageProcessor {
private static final Logger LOGGER = Logger.getLogger(MessageProcessor.class);
public void processMessage(Message<String> msg) {
LOGGER.info("Message is about to be processed");
}