现在,当我升级到log4j2之后, Statement1 将不起作用,因为Log4JLogger()构造函数期望类型为org.apache.log4j.Logger类型的参数.那么,如何在Log4j2中使用公共日志? 解决方案您需要将log4j-jcl-2.7依赖项添加到类路径中.请参阅常见问题解答中的"哪个罐子问题 .在您的代码中,使用import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;public class MyClass { private Log log = LogFactory.getLog(MyClass.class); ...您不应明确使用Log4JLogger.此外,请注意Log4j2与Log4j 1的不同之处在于,它在API和实现之间提供了清晰的分隔.因此,现在使用包装器库的好处比十年前使用Log4j 1的好处要少得多.考虑直接使用Log4j2 API:它为您提供API与实现之间的相同分离,并且比公共记录或slf4j具有更多的功能.请注意,直接使用Log4j2 API的风险很小:如果您改变主意并决定对直接使用Log4j2 API的应用程序使用Logback(或其他slf4j实现),则始终会使用log4j-to-slf4j-2.x模块. I am using log4j 1.2 with commons-logging. Now I am trying to upgrade it to log4j2.But how to use log4j2 with commons-logging to initialize log4j2.I tried to initialize commons logging in the below way. Its working fine **Statement1**: static Log log = new Log4JLogger(Logger.getLogger(Example.class));**Statement2**:log.debug("debug statement");Here I am using object of type org.apache.commons.logging.Log initialized with object of org.apache.log4j.Logger.(org.apache.log4j.Logger is the class from log4j 1.2 where as from log4j2 is changed to org.apache.logging.log4j.Logger)Now after I upgrade to log4j2, Statement1 will not work as Log4JLogger() constructor expects argument of type org.apache.log4j.Logger type.So, how do I use commons logging with Log4j2? 解决方案 You need to add the log4j-jcl-2.7 dependency to your classpath.See the "which jars" question in the FAQ.In your code, use import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;public class MyClass { private Log log = LogFactory.getLog(MyClass.class); ...You should not explicitly use Log4JLogger.Also, be aware that Log4j2 is different from Log4j 1 in that it offers a clean separation between its API and its implementation. So the benefits of using a wrapper library are much less now than they were 10 years ago with Log4j 1.Consider using the Log4j2 API directly: it gives you the same separation between API and implementation and is more feature rich than commons logging or slf4j.Note that there is little risk in using the Log4j2 API directly: the log4j-to-slf4j-2.x module is always there in case you change your mind and decide to use Logback (or another slf4j implementation) with an application that directly uses the Log4j2 API. 这篇关于使用log4j2进行公共日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-29 19:40