在2.2.1 RELEASE.Everything中开发一个spring引导应用程序很好,除了使用log4j.properties进行Loging之外。
在apoplication.properies中,添加了logging.config,如下所示
logging.config =${external.config}/log4j.properties.Pom.xml文件,排除spring-boot-starter-logging并添加spring-boot-starter-log4j,如下所示
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>log4j.properties
log4j.rootLogger=error,Service
# Direct log messages to a log file
log4j.appender.Service=org.apache.log4j.RollingFileAppender
log4j.appender.Service.File=C:/log/Service.log
log4j.appender.Service.MaxFileSize=1MB
log4j.appender.Service.MaxBackupIndex=1
log4j.appender.Service.layout=org.apache.log4j.PatternLayout
log4j.appender.Service.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - [%X{AUDIT_USER}] %m%n我在堆栈中引用了以下链接
1.在SpringBoot1.5.7中不使用log4j.properties打印记录器
2.带有log4.properties文件的Spring引导日志记录无法工作
编辑1
作为Andy Wilkinson注释,我修改了log4j.properties文件内容和文件名,文件名应该是log4j2.properties。
我尝试将一些字符串记录到日志文件中,如下所示
public class ServiceMain {
private static final Logger logger = LogManager.getLogger(ServiceMain.class);
public static void main(String[] args) {
SpringApplication.run(ServiceMain.class, args);
logger.debug("----------------------Stating spring booot----------------------");
}
} 调试日志"----------------------Stating spring booot----------------------"不会写入c:/log/service.log
日志文件
2019-11-22 05:20:14,631 INFO o.s.b.StartupInfoLogger [main] Starting ServiceMain v7.0.0.0 on host-4 with PID 119176 (D:\Service\target\Service-1.0.jar started by Administrator in D:\Service\target)
2019-11-22 05:20:14,634 DEBUG o.s.b.StartupInfoLogger [main] Running with Spring Boot v2.2.1.RELEASE, Spring v5.2.1.RELEASE
2019-11-22 05:20:14,635 INFO o.s.b.SpringApplication [main] The following profiles are active: Service
2019-11-22 05:20:24,898 INFO o.s.b.StartupInfoLogger [main] Started ServiceMain in 12.312 seconds (JVM running for 14.189)我错过了任何log4j2.properties配置吗?
发布于 2019-11-22 07:17:19
您使用的是Log4j 2(因为版本1不再受支持),但是似乎使用Log4j 1配置文件来配置它。
您可以在Log4j 2的文档中了解更多有关其配置属性的信息。它包括以下示例:
status = error
dest = err
name = PropertiesConfig
property.filename = target/rolling/rollingtest.log
filter.threshold.type = ThresholdFilter
filter.threshold.level = debug
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %m%n
appender.console.filter.threshold.type = ThresholdFilter
appender.console.filter.threshold.level = error
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = target/rolling2/test1-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d %p %C{1.} [%t] %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 2
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=100MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5
logger.rolling.name = com.example.my.app
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile
rootLogger.level = info
rootLogger.appenderRef.stdout.ref = STDOUThttps://stackoverflow.com/questions/58988248
复制相似问题