配置spring日志或应用程序上下文加载

wqnecbli  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(248)

我有一个基于spring框架(3.2.12)的cli工具,它使用logback(1.23)进行日志记录。
我的问题是,日志配置在app cxt加载之前不会生效,从而用init过程的日志污染了命令行。有没有办法强制spring首先加载和使用日志配置?
主要类别:

public class MyCliMain {

private static final JcanLogger LOG = SessionLogFactory.getLog(MyCliMain.class);

public static void main(String[] args) {
    int exitCode;
    ApplicationContext cxt = initApplicationContext();
    exitCode = cxt.getBean(CommandRunner.class).run(args);
    System.exit(exitCode);
}

private static ApplicationContext initApplicationContext() {
    AbstractApplicationContext cxt = new ClassPathXmlApplicationContext("spring-pay-bus-cli-G8Q.xml");
    cxt.registerShutdownHook();
    return cxt;
}}

资源中的logback配置:

<appender name="writeToConsole" class="ch.qos.logback.core.ConsoleAppender">
    <target>System.out</target>
    <encoder>
        <pattern>%level %logger{5} - %msg%n</pattern>
    </encoder>
</appender>

<appender name="writeToFile" class="ch.qos.logback.core.FileAppender">
    <file>${log.output}</file>
    <encoder>
        <pattern>%-30(%d{HH:mm:ss.SSS} [%thread]) %-5level %logger{32} - %msg%n</pattern>
    </encoder>
</appender>

<logger name="org.springframework" level="WARN"/>
<logger name="com..." level="DEBUG" appender-ref="writeToFile"/>
<logger name="com... level="DEBUG"/>

<root level="WARN">
    <appender-ref ref="writeToConsole"/>
    <appender-ref ref="writeToFile" />
</root>

控制台注销输出:

Apr 01, 2021 4:07:23 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7c16905e: startup date [Thu Apr 01 16:07:23 CEST 2021]; root of context hierarchy
Apr 01, 2021 4:07:23 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
...
logging config takes effect: 
WARN o.a.c.j.u.ResourceUtils - No resource methods have been found for resource class org.apache.cxf.jaxrs.client.WebClient
WARN o.a.c.j.u.ResourceUtils - No resource methods have been found for resource class org.apache.cxf.jaxrs.client.WebClient
WARN o.a.c.j.u.ResourceUtils - No resource methods have been found for resource class org.apache.cxf.jaxrs.client.WebClient
INFO c.u.s.v.c.CommandRunner -

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题