简体   繁体   English

在 maven 项目中将 log4j.properties 放在哪里?

[英]Where to put log4j.properties in a maven project?

I know it's a cliché question and I readed all the documents and I tried many solutions but always no solution worked for me.!我知道这是一个陈词滥调的问题,我阅读了所有文件,并尝试了许多解决方案,但始终没有解决方案对我有用。! but I put the log4j.properties in the src/main/resources and also in src/main/java and I've added PropertyConfigurator.configure("log4j.properties");但我将 log4j.properties 放在src/main/resourcessrc/main/java中,并且我添加了PropertyConfigurator.configure("log4j.properties"); to my class but when I turn my project to a Runnable JAR it shows me:到我的 class 但是当我将我的项目转到可运行的JAR时,它向我显示:

C:\Users\eya\Desktop>java -jar RestCallAutomation.jar
log4j:WARN No appenders could be found for logger (com.infosquare.restautomation.AppController).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
log4j:ERROR Could not read configuration file [log4j.properties].
java.io.FileNotFoundException: log4j.properties (Le fichier spécifié est introuvable)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:372)
        at org.apache.log4j.PropertyConfigurator.configure(PropertyConfigurator.java:403)
        at com.infosquare.restautomation.GenerateProcessors.start(GenerateProcessors.java:34)
        at com.infosquare.restautomation.AppController$2.run(AppController.java:98)
        at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
        at java.lang.Thread.run(Unknown Source)
log4j:ERROR Ignoring configuration file [log4j.properties].

here is the architecture of my project: maven project Architecture这是我项目的架构: maven project Architecture

and here is the log4j.properties:这是 log4j.properties:

# Root logger option
log4j.rootLogger=INFO, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logging.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

here ismy pom.xml:这是我的 pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.OpenText</groupId>
  <artifactId>GenerateurCodes</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>

  <dependency>
  <groupId>org.apache.maven</groupId>
  <artifactId>maven-model</artifactId>
  <version>2.2.1</version>
</dependency>
  <dependency>
    <groupId>org.jdom</groupId>
    <artifactId>jdom</artifactId>
    <version>1.1</version>
   </dependency>
 <dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1</version>
 </dependency>
 <dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.5</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.5.2</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</version>
</dependency>
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity</artifactId>
    <version>1.7</version>
    </dependency>
<dependency>
     <groupId>org.apache.velocity</groupId>
     <artifactId>velocity-tools</artifactId>
     <version>2.0</version>
</dependency>
<dependency>
    <groupId>org.w3c</groupId>
    <artifactId>dom</artifactId>
    <version>2.3.0-jaxb-1.0.6</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

 </dependencies>
</project>

**UPDATE: ** **更新: **

when I open the jar using WinRar it shows me that it contains the log4j.properties and log4j jar so it s not a resources problem! when I open the jar using WinRar it shows me that it contains the log4j.properties and log4j jar so it s not a resources problem!

first of ALL: It called: log4j.properties not Log4j.properties !!首先:它叫: log4j.properties不是 Log4j.properties !

Second: instead of PropertyConfigurator.configure("log4j.properties");第二:而不是PropertyConfigurator.configure("log4j.properties"); it should be: PropertyConfigurator.configure(getClass().getResource("/log4j.properties"));它应该是: PropertyConfigurator.configure(getClass().getResource("/log4j.properties"));

Check your resource directory in maven build.检查 maven 构建中的资源目录。 It should be something like this.它应该是这样的。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
.......
     <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
     </build>
</project>

This way log4j.properties file will be in the classpath directory.这样 log4j.properties 文件将位于类路径目录中。

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

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