log4j 无法使用我的 log4j.properties 进行初始化

发布于 2024-09-27 20:18:32 字数 2627 浏览 0 评论 0原文

我有一个简单的 log4j.property 文件和一个使用 log4j 的简单程序。但是当我运行我的 jar 时,我只看到这个

log4j:WARN No appenders could be found for logger (package com.mycompany.hellolog4j).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

这是我使用记录器的方式:

package com.mycompany.hellolog4j;

import java.io.File;
import java.io.IOException;
import org.apache.log4j.*;

public class SimpleLogging
{

    public static void main(String args[]) throws IOException {

        Logger logger = Logger.getLogger(SimpleLogging.class.getPackage().toString());
       // SimpleLogging demo = new SimpleLogging(rbName);
        //demo.doLogging(name);
        logger.info("printing the message...");
    }
}

这是我的 pom.xml 的一部分

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathLayoutType>custom</classpathLayoutType>
                            <mainClass>com.mycompany.hellolog4j.SimpleLogging</mainClass>
                            <customClasspathLayout>../lib/log4j.jar ../log4j.properties</customClasspathLayout>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

这是我的 log4j.properties

log4j.rootLogger = DEBUG, testAppender

log4j.logger.com.mycompany.hellolog4j=DEBUG, testAppender

log4j.appender.testAppender=org.apache.log4j.ConsoleAppender

log4j.appender.testAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.testAppender.layout.conversionPattern=%p-%m%n

现在从我的项目目录运行 jar 文件。这是我的项目目录

drwxr-xr-x 5 proger users 4096 2010-10-15 22:59 ./
drwxr-xr-x 3 proger users 4096 2010-10-15 22:27 ../
drwxr-xr-x 2 proger users 4096 2010-10-14 22:54 lib/
-rw-r--r-- 1 proger users  291 2010-10-15 23:00 log4j.properties
-rw-r--r-- 1 proger users 1689 2010-10-15 22:59 nbactions.xml
-rw-r--r-- 1 proger users 1987 2010-10-15 22:56 pom.xml
drwxr-xr-x 4 proger users 4096 2010-10-13 23:01 src/
drwxr-xr-x 6 proger users 4096 2010-10-15 22:59 target/

I have a simple log4j.property file and a simple program that use log4j. But when I run my jar I see only this

log4j:WARN No appenders could be found for logger (package com.mycompany.hellolog4j).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

Here is how I use logger:

package com.mycompany.hellolog4j;

import java.io.File;
import java.io.IOException;
import org.apache.log4j.*;

public class SimpleLogging
{

    public static void main(String args[]) throws IOException {

        Logger logger = Logger.getLogger(SimpleLogging.class.getPackage().toString());
       // SimpleLogging demo = new SimpleLogging(rbName);
        //demo.doLogging(name);
        logger.info("printing the message...");
    }
}

And this is part of my pom.xml

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathLayoutType>custom</classpathLayoutType>
                            <mainClass>com.mycompany.hellolog4j.SimpleLogging</mainClass>
                            <customClasspathLayout>../lib/log4j.jar ../log4j.properties</customClasspathLayout>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

And here is my log4j.properties

log4j.rootLogger = DEBUG, testAppender

log4j.logger.com.mycompany.hellolog4j=DEBUG, testAppender

log4j.appender.testAppender=org.apache.log4j.ConsoleAppender

log4j.appender.testAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.testAppender.layout.conversionPattern=%p-%m%n

Now from my project directory I run jar file. This is my project directory

drwxr-xr-x 5 proger users 4096 2010-10-15 22:59 ./
drwxr-xr-x 3 proger users 4096 2010-10-15 22:27 ../
drwxr-xr-x 2 proger users 4096 2010-10-14 22:54 lib/
-rw-r--r-- 1 proger users  291 2010-10-15 23:00 log4j.properties
-rw-r--r-- 1 proger users 1689 2010-10-15 22:59 nbactions.xml
-rw-r--r-- 1 proger users 1987 2010-10-15 22:56 pom.xml
drwxr-xr-x 4 proger users 4096 2010-10-13 23:01 src/
drwxr-xr-x 6 proger users 4096 2010-10-15 22:59 target/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

穿透光 2024-10-04 20:18:33

将 log4j.properties 文件放入 src/main/resources 中,以便将其捆绑到 JAR 文件中,从而将其捆绑到类路径中。

Put the log4j.properties file in src/main/resources so that it is bundled into your JAR file, and thus on the classpath.

后知后觉 2024-10-04 20:18:33

\weblogic\domain1\bin\setDomainEnv.cmd 中添加此行

set PATH=%WL_HOME%\server\native\win\x64;%PATH%

Add this line in \weblogic\domain1\bin\setDomainEnv.cmd

set PATH=%WL_HOME%\server\native\win\x64;%PATH%
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文