简体   繁体   English

"Log4j2 - 配置"

[英]Log4j2 - configuring

I am trying to adopt Log4j2 in my new project, but I get my logs in catalina.out<\/code> , and the first one is always: ERROR StatusLogger Unable to locate a logging implementation, using SimpleLogger<\/code> .我试图在我的新项目中采用 Log4j2,但我在catalina.out<\/code>获取日志,第一个始终是: ERROR StatusLogger Unable to locate a logging implementation, using SimpleLogger<\/code> 。 It seems that I have done everything according to Log4j2 docs, but still.似乎我已经根据 Log4j2 文档完成了所有工作,但仍然如此。

Here is what I have actually done:这是我实际所做的:

  1. added log4j-api-2.0-beta3.jar<\/code> to my projectlog4j-api-2.0-beta3.jar<\/code>到我的项目中<\/li>
  2. created a log4j2.xml<\/code> file and put it in a location that is on the classpath (currently, in \/usr\/local\/tomcat\/home\/lib<\/code> . In fact, I took a sample file from Log4J2 web-page.创建了一个log4j2.xml<\/code>文件并将其放在类路径上的一个位置(当前,在\/usr\/local\/tomcat\/home\/lib<\/code> 。事实上,我从 Log4J2 网页中获取了一个示例文件。<\/li>
  3. restarted tomcat.重新启动tomcat。<\/li><\/ol>

    I am using Tomcat 7, MacOS X 10.8, Java 7.我正在使用 Tomcat 7、MacOS X 10.8、Java 7。

    What am I missing?我错过了什么?

    Just in case, here is the log4j2.xml I am using:以防万一,这是我正在使用的 log4j2.xml:

    <\/code>

     <?xml version="1.0" encoding="UTF-8"?> <configuration status="warn" name="MyApp" packages=""> <appenders> <File name="MyFile" fileName="logs\/app.log"> <PatternLayout> <pattern>%d %p %C{1.} [%t] %m%n<\/pattern> <\/PatternLayout> <\/File> <\/appenders> <loggers> <root level="trace"> <appender-ref ref="MyFile"\/> <\/root> <\/loggers> <\/configuration><\/code><\/pre>

    "

I had the same problem. 我有同样的问题。 After adding also log4j-core-2.0-beta3.jar to the classpath it worked. log4j-core-2.0-beta3.jar添加到它运行的类路径之后。

In Maven you normally have this logging configuration: Maven中,您通常具有此日志记录配置:

<properties>    
    <slf4j-version>1.7.7</slf4j-version>
    <slf4j-log4j2-version>2.0.1</slf4j-log4j2-version>
</properties>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>${slf4j-version}</version>
</dependency>

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>${slf4j-log4j2-version}</version>
</dependency>

To add the log core lib you have to: 要添加日志核心库,您必须:

  1. Add the 2.0.1 to the properties section 将2.0.1添加到属性部分
  2. Add the log core dependency: 添加日志核心依赖项:

     <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>${log4j2-version}</version> </dependency> 

新图标 Updated to the version 2.7 已更新至2.7版

Add the next jars to your project: 将下一个罐子添加到您的项目中:

If you are using Maven, add the dependency: 如果您使用的是Maven,请添加依赖项:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.7</version>
</dependency>

我使用log4j2.8.2并通过将log4j2.xml放入web classes文件夹(WEB-INF / classes / log4j2.xml)让它成功自动配置

If it's maven project, you have to add the below dependency,如果是 maven 项目,则必须添加以下依赖项,

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

And the log4j2.xml or log4j2.properties file should be under src/main/resources folder. log4j2.xml 或 log4j2.properties 文件应该在 src/main/resources 文件夹下。 In that case you don't need to explicitly add folder or jars in classpath.在这种情况下,您不需要在类路径中显式添加文件夹或 jar。 It should do the trick.它应该可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM