配置Spring日志或应用程序上下文加载
我有一个CLI工具,它基于Spring框架(3.2.12)并使用logback(1.23)进行日志记录 我的问题是,日志配置在加载app cxt之前不会生效,因此会用init过程的日志污染命令行。 有没有一种方法可以首先强制Spring加载和使用日志配置 主要类别:配置Spring日志或应用程序上下文加载,spring,logging,logback,spring-logback,Spring,Logging,Logback,Spring Logback,我有一个CLI工具,它基于Spring框架(3.2.12)并使用logback(1.23)进行日志记录 我的问题是,日志配置在加载app cxt之前不会生效,因此会用init过程的日志污染命令行。 有没有一种方法可以首先强制Spring加载和使用日志配置 主要类别: public class MyCliMain { private static final JcanLogger LOG = SessionLogFactory.getLog(MyCliMain.class); public s
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>
请在@KajHejer上查看是否有有用的信息,谢谢,但这是针对Spring Boot的。
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 -