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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1485987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:20:26  来源:igfitidea点击:

Where should I put the log4j.properties file?

javanetbeansglassfishlog4j

提问by mono

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获得

回答by Martin OConnor

The file should be located in the WEB-INF/classes directory. This directory structure should be packaged within the war file.

该文件应位于 WEB-INF/classes 目录中。此目录结构应打包在 war 文件中。

回答by KLE

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. You have more options, get assistance from your IDE and so on...

我建议使用 log4j.xml 而不是 log4j.properties。您有更多选择,从您的 IDE 获得帮助等等...

回答by Makatun

You can specify config file location with VM argument -Dlog4j.configuration="file:/C:/workspace3/local/log4j.properties"

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

回答by Brian

A few technically correct specific answers already provided but in general, it can be anywhere on the runtime classpath, i.e. 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/类作为“魔术”目录。

回答by miss

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

Hope this will help.

希望这会有所帮助。

rgds

rgds

回答by Jhon M

Try:

尝试:

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

回答by sampathpremarathna

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. 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.

它适用于 IDE,但不适用于自己运行 jar 文件。当你自己运行 jar 文件时,只需将 log4j.properties 文件复制到 jar 文件所在的文件夹中。当 jar 和属性文件在同一目录中时,它运行良好。

回答by Durandal00

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");

回答by stivlo

As already stated, log4j.properties should be in a directory included in the classpath, I want to add that in a mavenized project a good place can be src/main/resources/log4j.properties

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

回答by Ali Mamedov

My IDE is NetBeans. I put log4j.property file as shown in the pictures

我的 IDE 是 NetBeans。我把 log4j.property 文件如图所示

Root

project's root folder

项目的根文件夹

Web

网络

web folder

网页文件夹

WEB-INF

网络信息

WEB-INF folder

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 contextfrom another JSP file. Example:

您可以从另一个 JSP 文件定义静态 ServletContext 上下文。例子:

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

Now you can use log4j.property file in your projects.

现在您可以在您的项目中使用 log4j.property 文件。