gpt4 book ai didi

java - 如何以编程方式设置 Spring log4j 属性文件路径

转载 作者:行者123 更新时间:2023-12-01 06:11:53 24 4
gpt4 key购买 nike

我就是这样使用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 调试。控制台输出:

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.

谢谢,射线。

最佳答案

我通过将此参数添加到可执行 jar 来修复它:

-Dlog4j.configurationFile=file:/path.to/log4j.properties

关于java - 如何以编程方式设置 Spring log4j 属性文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32863690/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com