java jar 文件运行时未创建 Log4j 日志

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11573510/
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-10-31 05:38:09  来源:igfitidea点击:

Log4j Log is not created when jar file run

javalog4j

提问by Pradeep Gamage

This is my Studentclass that used to test log4j.

这是我用来测试 log4j 的Student类。

public class Student{    
    private static final Logger logger = Logger.getLogger(Student.class.getName());   

    public Student() {  
         PropertyConfigurator.configure("log4j.properties");  
    }

    public static void main(String args[]){  
        logger.log(Level.INFO, "My log4j Test");    
    } 
}

This is my log4j.propertiesfile

这是我的log4j.properties文件

log4j.rootLogger=INFO,Appender1,Appender2
log4j.appender.Appender1=org.apache.log4j.ConsoleAppender
log4j.appender.Appender2=org.apache.log4j.RollingFileAppender
log4j.appender.Appender2.File=C:/Log4j/MyLogExample.log

log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout`  
log4j.appender.Appender1.Target=System.out`   
log4j.appender.Appender1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n`  

log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout`  
log4j.appender.Appender2.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n`  

log4j.appender.Appender2.MaxFileSize=50KB`  
log4j.appender.Appender2.MaxBackupIndex=10` 

When I run this program using Eclipse MyLogExample.logfile gets created. But after I have created a jar file and run it using the command prompt, the log file is not created.

当我使用 Eclipse MyLogExample.log运行此程序时,会创建文件。但是在我创建了一个 jar 文件并使用命令提示符运行它之后,没有创建日志文件。

in console i can see this error.

在控制台中,我可以看到此错误。

log4j:ERROR Could not read configuration file [log4j.properties]. 
java.io.FileNotFoundException: log4j.properties (The system cannot find the file specified) 

After I add the following code example, the log file is created even when the jar file run with command prompt.

添加以下代码示例后,即使 jar 文件在命令提示符下运行,也会创建日志文件。

PropertyConfigurator.configure("C:\eclipeworkspace\Log4jTest\log4j.properties");

How I can give relative path instead of exact path?

我如何给出相对路径而不是确切路径?

采纳答案by kjp

log4j.propertiesshould be in your classpath. Add it to the classpath when running from command line.

log4j.properties应该在你的类路径中。从命令行运行时将其添加到类路径中。

A better alternative is to specify the property file using

更好的选择是使用指定属性文件

-Dlog4j.configuration=relative path/log4j.properties

-Dlog4j.configuration=relative path/log4j.properties

from the command line. In this case you can remove the line PropertyConfigurator.configure("log4j.properties");from your code - you don't need to do anything in the code to specify the property file.

从命令行。在这种情况下,您可以PropertyConfigurator.configure("log4j.properties");从代码中删除该行- 您无需在代码中执行任何操作来指定属性文件。

回答by Rudy

Use this :

用这个 :

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL url = classLoader.getResource("log4j.properties");
File file = new File(url.toURI());

回答by akshay

add this plugin if using maven to make your jar runnable wilth all depenencies

如果使用 Maven 使您的 jar 可运行并具有所有依赖项,请添加此插件

<plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>cnnc.bcyr.nvnvn.nch.MainclassName</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>