簡體   English   中英

Spring 3和log4J的控制台應用程序

[英]Console Application with Spring 3 and log4J

我正在嘗試使用Spring初始化log4J,並使log4j少一些健談。 但是我無法在日志記錄中關閉彈簧。

我把它放在了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.

我參加了春季助教課程:

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運行,運行方法如下:

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

我錯過了什么?


感謝您的輸入,以下是新元素:

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

@Ralph我的意思是:

<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,以避免重復輸入日志。

/**
 * 初始化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);
}

它沒有選擇您的log4j配置; 請求解決方案是將其放在類路徑中,並讓log4j自己找到它。

還要確保您的運行配置正在使用啟動目錄,從中指定的路徑配置有意義,或者提供了絕對路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM