使用 Spring 3 和 log4J 的控制台应用程序

发布于 2024-12-11 20:32:57 字数 2169 浏览 0 评论 0原文

我正在尝试使用 Spring 来初始化 log4J 并让 log4j 变得不那么健谈。但我无法在日志记录中关闭 spring。

我把它放在我的 spring.xml 中:

<bean id="log4jInitialization" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
    <property name="targetMethod" value="initLogging" />
    <property name="arguments">
        <list>
            <value>properties/log4j.properties</value>
        </list>
    </property>
</bean>

在我的 log4J.properties 中:

#log4j.rootLogger=DEBUG, stdout
log4j.logger.com.app=DEBUG, FileAppender

log4J.logger.org.springframework=INFO, stdout
# Skipped all appenders definition.

我有一个 spring 助手类:

public class AppContext
{

private static ApplicationContext ctx;

public static void init()
{
    ctx = new ClassPathXmlApplicationContext(
            "file:properties/ApplicationContext.xml");
}
// Snipped the rest.
}

当我启动我的应用程序时,我得到这个:

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.

我认为静态 bean 已创建并称为 Log4jConfigurer,但没有。

我从 eclipse 运行,这个 runConfig 如下:

-Dlog4j.configuration=file:properties/log4j.properties

我错过了什么?


感谢您的输入,以下是新元素:

@Hellectronic 您的意思是 ClassPathXmlApplicationContext 无法使用 log4j?

@拉尔夫我的意思是:

<bean id="log4jInitialization" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
    <property name="targetMethod" value="initLogging" />
    <property name="arguments">
        <list>
            <value>properties/log4j.properties</value>
        </list>
    </property>

好吧,总结一下: 我必须取消注释 lo4j.rootLogger 才能让他进行配置。 删除我的专用记录器后面的 std 或 FileAppender 以避免双重日志条目。

I'm trying to init log4J with Spring and make log4j a little less talkative. But I fail to shut spring down in logging.

I've put this in my spring.xml:

<bean id="log4jInitialization" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
    <property name="targetMethod" value="initLogging" />
    <property name="arguments">
        <list>
            <value>properties/log4j.properties</value>
        </list>
    </property>
</bean>

In my log4J.properties:

#log4j.rootLogger=DEBUG, stdout
log4j.logger.com.app=DEBUG, FileAppender

log4J.logger.org.springframework=INFO, stdout
# Skipped all appenders definition.

I got a spring helper class:

public class AppContext
{

private static ApplicationContext ctx;

public static void init()
{
    ctx = new ClassPathXmlApplicationContext(
            "file:properties/ApplicationContext.xml");
}
// Snipped the rest.
}

When I start my app, I get this:

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.

I thought that the static bean was created and called the Log4jConfigurer, but no.

I run from my eclipse, this runConfig as this:

-Dlog4j.configuration=file:properties/log4j.properties

What did I miss?


Thanks for your input, here are new elements:

@Hellectronic you mean that ClassPathXmlApplicationContext is not able to use log4j?

@Ralph I mean this:

<bean id="log4jInitialization" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
    <property name="targetMethod" value="initLogging" />
    <property name="arguments">
        <list>
            <value>properties/log4j.properties</value>
        </list>
    </property>

Ok, to sum it up :
I must uncomment the lo4j.rootLogger to enable him to configure.
Remove the std or FileAppender after my dedicated loggers to avoid the double log entry.

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

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

发布评论

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

评论(2

自我难过 2024-12-18 20:32:57
/**
 * 初始化Spring环境, 并初始化采集任务
 */
private void setup() throws FileNotFoundException {
    // Console程序必须使用Spring Log4jConfigurer 初始化Log4j
    Log4jConfigurer.initLogging(Env.getLog4jConfig());
    ApplicationContext context = new ClassPathXmlApplicationContext(DEFAULT_APPLICATION_CONTEXT);
    SpringContextHolder contextHolder = new SpringContextHolder();
    contextHolder.setApplicationContext(context);
}
/**
 * 初始化Spring环境, 并初始化采集任务
 */
private void setup() throws FileNotFoundException {
    // Console程序必须使用Spring Log4jConfigurer 初始化Log4j
    Log4jConfigurer.initLogging(Env.getLog4jConfig());
    ApplicationContext context = new ClassPathXmlApplicationContext(DEFAULT_APPLICATION_CONTEXT);
    SpringContextHolder contextHolder = new SpringContextHolder();
    contextHolder.setApplicationContext(context);
}
甜宝宝 2024-12-18 20:32:57

它没有获取您的 log4j 配置;请求解决方案是将其放在类路径中,让log4j自己找到它。

还要确保您的运行配置使用启动目录,您指定的路径配置从中有意义,或者提供绝对路径。

It's not picking up your log4j config; the request solution is to put it in the classpath and let log4j find it itself.

Also make sure your run configuration is using a startup directory from which the path configuration you specify makes sense, or supply an absolute path.

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