简体   繁体   English

我应该把 log4j.properties 文件放在哪里?

[英]Where should I put the log4j.properties file?

I wrote a web service project using netbeans 6.7.1 with glassfish v2.1, put log4j.properties to the root dir of project and use:我使用 netbeans 6.7.1 和 glassfish v2.1 编写了一个 Web 服务项目,将 log4j.properties 放入项目的根目录并使用:

static Logger logger = Logger.getLogger(MyClass.class);

in Constructor:在构造函数中:

PropertyConfigurator.configure("log4j.properties");

and in functions:并在函数中:

logger.info("...");
logger.error("...");
// ...

but, it is error info(actually, I have tried to put it almost every dir that I could realize):但是,这是错误信息(实际上,我试图将它放在几乎所有我能意识到的目录中):

log4j:ERROR Could not read configuration file [log4j.properties].
java.io.FileNotFoundException: log4j.properties (The system cannot find the file specified)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:106)
        at java.io.FileInputStream.<init>(FileInputStream.java:66)
        at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:297)
        at org.apache.log4j.PropertyConfigurator.configure(PropertyConfigurator.java:315)
        at com.corp.ors.demo.OrsDemo.main(OrisDemo.java:228)
log4j:ERROR Ignoring configuration file [log4j.properties].
log4j:WARN No appenders could be found for logger (com.corp.ors.demo.OrsDemo).
log4j:WARN Please initialize the log4j system properly.

the example project could be get from http://www.91files.com/?N3F0QGQPWMDGPBRN0QA8示例项目可以从http://www.91files.com/?N3F0QGQPWMDGPBRN0QA8获得

I know it's a bit late to answer this question, and maybe you already found the solution, but I'm posting the solution I found (after I googled a lot) so it may help a little:我知道现在回答这个问题有点晚了,也许你已经找到了解决方案,但我正在发布我找到的解决方案(在我搜索了很多之后),所以它可能会有所帮助:

  1. Put log4j.properties under WEB-INF\\classes of the project as mentioned previously in this thread.如前所述,将 log4j.properties 放在项目的 WEB-INF\\classes 下。
  2. Put log4j-xx.jar under WEB-INF\\lib将 log4j-xx.jar 放在 WEB-INF\\lib 下
  3. Test if log4j was loaded: add -Dlog4j.debug @ the end of your java options of tomcat测试是否加载了 log4j:在 tomcat 的 java 选项末尾添加-Dlog4j.debug @

Hope this will help.希望这会有所帮助。

rgds rgds

如前所述,log4j.properties 应该在类路径中包含的目录中,我想补充一点,在 mavenized 项目中,一个好地方可以是src/main/resources/log4j.properties

您可以使用 VM 参数 -Dlog4j.configuration="file:/C:/workspace3/local/log4j.properties" 指定配置文件位置

You have to put it in the root directory, that corresponds to your execution context.您必须将其放在根目录中,该目录对应于您的执行上下文。

Example:例子:

    MyProject
       src
           MyClass.java
           log4j.properties

If you start executing from a different project, you need to have that file in the project used for starting the execution.如果您从不同的项目开始执行,则需要在用于开始执行的项目中包含该文件。 For example, if a different project holds some JUnit tests, it needs to have also its log4j.properties file.例如,如果一个不同的项目包含一些 JUnit 测试,它也需要有它的 log4j.properties 文件。


I suggest using log4j.xml instead of the log4j.properties.我建议使用 log4j.xml 而不是 log4j.properties。 You have more options, get assistance from your IDE and so on...您有更多选择,从您的 IDE 获得帮助等等...

For a Maven Based Project keep your log4j.properties in src/main/resources.对于基于 Maven 的项目,请将 log4j.properties 保存在 src/main/resources 中。 Nothing else to do!没有别的事情可做!

If you put log4j.properties inside src, you don't need to use the statement -如果将 log4j.properties 放在 src 中,则不需要使用该语句 -

PropertyConfigurator.configure("log4j.properties");

It will be taken automatically as the properties file is in the classpath.当属性文件位于类路径中时,它将被自动获取。

尝试:

PropertyConfigurator.configure(getClass().getResource("/controlador/log4j.properties"));

The file should be located in the WEB-INF/classes directory.该文件应位于 WEB-INF/classes 目录中。 This directory structure should be packaged within the war file.此目录结构应打包在 war 文件中。

My IDE is NetBeans.我的 IDE 是 NetBeans。 I put log4j.property file as shown in the pictures我把 log4j.property 文件如图所示

Root

项目的根文件夹

Web网络

网页文件夹

WEB-INF网络信息

WEB-INF 文件夹

To use this property file you should to write this code:要使用此属性文件,您应该编写以下代码:

package example;

import java.io.File;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.Logger;
import javax.servlet.*;
public class test {

public static ServletContext context;
static Logger log = Logger.getLogger("example/test");

public test() {

        String homeDir = context.getRealPath("/");
        File propertiesFile = new File(homeDir, "WEB-INF/log4j.properties");
        PropertyConfigurator.configure(propertiesFile.toString());
        log.info("This is a test");
    }
}

You can define static ServletContext context from another JSP file.您可以从另一个 JSP 文件定义静态 ServletContext 上下文 Example:例子:

test.context = getServletContext(); 
test sample = new test(); 

Now you can use log4j.property file in your projects.现在您可以在您的项目中使用 log4j.property 文件。

A few technically correct specific answers already provided but in general, it can be anywhere on the runtime classpath, ie wherever classes are sought by the JVM.已经提供了一些技术上正确的特定答案,但总的来说,它可以位于运行时类路径上的任何位置,即 JVM 寻找类的任何位置。

This could be the /src dir in Eclipse or the WEB-INF/classes directory in your deployed app, but it's best to be aware of the classpath concept and why the file is placed in it, don't just treat WEB-INF/classes as a "magic" directory.这可能是 Eclipse 中的 /src 目录或已部署应用程序中的 WEB-INF/classes 目录,但最好了解类路径概念以及为什么将文件放在其中,不要只处理 WEB-INF/类作为“魔术”目录。

I've spent a great deal of time to figure out why the log4j.properties file is not seen.我花了很多时间来弄清楚为什么没有看到log4j.properties文件。
Then I noticed it was visible for the project only when it was in both MyProject/target/classes/ and MyProject/src/main/resources folders.然后我注意到它只有在MyProject/target/classes/MyProject/src/main/resources文件夹中时才对项目可见。
Hope it'll be useful to somebody.希望它对某人有用。
PS: The project was maven-based. PS:该项目是基于 maven 的。

I found that Glassfish by default is looking at [Glassfish install location]\\glassfish\\domains[your domain]\\ as the default working directory... you can drop the log4j.properties file in this location and initialize it in your code using PropertyConfigurator as previously mentioned...我发现 Glassfish 默认将 [Glassfish 安装位置]\\glassfish\\domains[您的域]\\ 作为默认工作目录...您可以将 log4j.properties 文件放在此位置并使用 PropertyConfigurator 在您的代码中对其进行初始化就像之前提到的...

Properties props = System.getProperties();
System.out.println("Current working directory is " + props.getProperty("user.dir"));
PropertyConfigurator.configure("log4j.properties");

Your standard project setup will have a project structure something like:您的标准项目设置将具有类似于以下内容的项目结构:

src/main/java
src/main/resources

You place log4j.properties inside the resources folder, you can create the resources folder if one does not exist您将 log4j.properties 放在资源文件夹中,如果资源文件夹不存在,您可以创建资源文件夹

Put log4j.properties in classpath.将 log4j.properties 放在类路径中。 Here is the 2 cases that will help you to identify the proper location- 1. For web application the classpath is /WEB-INF/classes.以下是帮助您确定正确位置的两种情况- 1. 对于 Web 应用程序,类路径为 /WEB-INF/classes。

\WEB-INF      
    classes\
        log4j.properties
  1. To test from main / unit test the classpath is source directory要从主/单元测试进行测试,类路径是源目录

    \\Project\\ src\\ log4j.properties

There are many ways to do it:有很多方法可以做到:

Way1: If you are trying in maven project without Using PropertyConfigurator方式1:如果您在不使用 PropertyConfigurator 的情况下尝试在 Maven 项目中

First: check for resources directory at scr/main首先:在 scr/main 检查资源目录

  if available,
       then: create a .properties file and add all configuration details.
  else
       then: create a directory named resources and a file with .properties     
write your configuration code/details.

follows the screenshot:按照截图:

![在此处输入图片说明

Way2: If you are trying with Properties file for java/maven project Use PropertyConfigurator方式2:如果您正在尝试使用 java/maven 项目的属性文件,请使用 PropertyConfigurator

Place properties file anywhere in project and give the correct path.将属性文件放在项目中的任何位置并给出正确的路径。 say: src/javaLog4jProperties/log4j.properties说: src/javaLog4jProperties/log4j.properties

static{静止的{

 PropertyConfigurator.configure("src/javaLog4jProperties/log4j.properties");

} }

在此处输入图片说明

Way3: If you are trying with xml on java/maven project Use DOMConfigurator方法 3:如果您在 java/maven 项目上尝试使用 xml使用 DOMConfigurator

Place properties file anywhere in project and give correct path.将属性文件放在项目中的任何位置并给出正确的路径。 say: src/javaLog4jProperties/log4j.xml说: src/javaLog4jProperties/log4j.xml

static{静止的{

 DOMConfigurator.configure("src/javaLog4jProperties/log4j.xml");

} }

在此处输入图片说明

I don't know this is correct way.But it solved my problem.我不知道这是正确的方法。但它解决了我的问题。 put log4j.properties file in "project folder"/config and use PropertyConfigurator.configure("config//log4j.properties");将 log4j.properties 文件放在“项目文件夹”/config 中并使用 PropertyConfigurator.configure("config//log4j.properties");

it will works with IDE but not when run the jar file yourself.它适用于 IDE,但不适用于自己运行 jar 文件。 when you run the jar file by yourself just copy the log4j.properties file in to the folder that jar file is in.when the jar and property file in same directory it runs well.当你自己运行 jar 文件时,只需将 log4j.properties 文件复制到 jar 文件所在的文件夹中。当 jar 和属性文件在同一目录中时,它运行良好。

Actually, I've just experienced this problem in a stardard Java project structure as follows:实际上,我刚刚在标准 Java 项目结构中遇到了这个问题,如下所示:

\myproject
    \src
    \libs
    \res\log4j.properties

In Eclipse I need to add the res folder to build path, however, in Intellij, I need to mark the res folder as resouces as the linked screenshot shows: right click on the res folder and mark as resources .在 Eclipse 中,我需要添加 res 文件夹来构建路径,但是,在 Intellij 中,我需要将 res 文件夹标记为资源,如链接的屏幕截图所示:右键单击 res 文件夹并标记为 resources

You don't need to specify PropertyConfigurator.configure("log4j.properties");您不需要指定PropertyConfigurator.configure("log4j.properties"); in your Log4J class, If you have already defined the log4j.properties in your project structure.在您的 Log4J 类中,如果您已经在项目结构中定义了 log4j.properties。

In case of Web Dynamic Project: -如果是 Web 动态项目:-

You need to save your log4j.properties under WebContent -> WEB-INF -> log4j.properties您需要将 log4j.properties 保存在 WebContent -> WEB-INF -> log4j.properties 下

I hope this may help you.我希望这可以帮助你。

Open spark-shell打开spark-shell

Then type System.getenv("SPARK_CONF_DIR")然后输入System.getenv("SPARK_CONF_DIR")

That will print where your log4j.properties should go.这将打印您的log4j.properties应该去的位置。

For me it worked when I put the file inside the resources folder.对我来说,当我将文件放在资源文件夹中时它起作用了。 Also it was a war file my project, my recommendation is to ensure that the name of the file is log4j.properties, my project didn't recognized "log4j2.properties"我的项目也是一个战争文件,我的建议是确保文件名是 log4j.properties,我的项目没有识别“log4j2.properties”

enter image description here在此处输入图片说明

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

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