我这样使用spring-boot;
public static void main(String[] args) throws Exception {
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);
////
@Configuration
@ComponentScan("com.mycompnay")
@PropertySources({
@PropertySource("classpath:application.properties")})
public class SpringConfiguration {
@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
我想了解如何以这种方式设置log4j.properties文件?我找到了一种方法来使用application.properties。但我们如何以编程方式为log4j.properties设置它?
log4j.properties
log4j.rootLogger=INFO, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.conversionPattern=%d %p [%c] - <%m>%n
#Debug logging
log4j.appender.DebugAppender=org.apache.log4j.RollingFileAppender
log4j.appender.DebugAppender.Threshold=DEBUG
log4j.appender.DebugAppender.File=vertx.log
log4j.appender.DebugAppender.MaxFileSize=200KB
log4j.appender.DebugAppender.MaxBackupIndex=5
log4j.appender.DebugAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.DebugAppender.layout.ConversionPattern=%d %p [%c] - <%m>%n
log4j.logger.org.springframework=ERROR,console
log4j.logger.org.apache=ERROR,console
但是在控制台上我仍然会看到这样的记录:
13:41:59.525 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
添加了log4j debug。控制台输出:
log4j: Reading configuration from URL file:/Users/idan/IdeaProjects/myservice-service/web/build/resources/main/log4j.properties
log4j: Parsing for [root] with value=[INFO, console].
log4j: Level token is [INFO].
log4j: Category root set to INFO
log4j: Parsing appender named "console".
log4j: Parsing layout options for "console".
log4j: Setting property [conversionPattern] to [%d %p [%c] - <%m>%n].
log4j: End of parsing for "console".
log4j: Parsed "console" options.
log4j: Parsing for [org.apache] with value=[ERROR, console].
log4j: Level token is [ERROR].
log4j: Category org.apache set to ERROR
log4j: Parsing appender named "console".
log4j: Appender "console" was already parsed.
log4j: Handling log4j.additivity.org.apache=[null]
log4j: Parsing for [org.springframework] with value=[ERROR, console].
log4j: Level token is [ERROR].
log4j: Category org.springframework set to ERROR
log4j: Parsing appender named "console".
log4j: Appender "console" was already parsed.
log4j: Handling log4j.additivity.org.springframework=[null]
log4j: Parsing for [io.netty] with value=[INFO, console].
log4j: Level token is [INFO].
log4j: Category io.netty set to INFO
log4j: Parsing appender named "console".
log4j: Appender "console" was already parsed.
log4j: Handling log4j.additivity.io.netty=[null]
log4j: Parsing for [com.mycompany] with value=[DEBUG, console].
log4j: Level token is [DEBUG].
log4j: Category com.mycompany set to DEBUG
log4j: Parsing appender named "console".
log4j: Appender "console" was already parsed.
log4j: Handling log4j.additivity.com.mycompany=[false]
log4j: Setting additivity for "com.mycompany" to false
log4j: Parsing for [org.neo4j.jdbc.level] with value=[DEBUG, console].
log4j: Level token is [DEBUG].
log4j: Category org.neo4j.jdbc.level set to DEBUG
log4j: Parsing appender named "console".
log4j: Appender "console" was already parsed.
log4j: Handling log4j.additivity.org.neo4j.jdbc.level=[null]
log4j: Finished configuring.
感谢, 射线。
答案 0 :(得分:0)
我通过将此参数添加到可执行jar:
来修复它-Dlog4j.configurationFile=file:/path.to/log4j.properties