外部Log4j.properties在不打印日志中-Spring Boot 2.2.1

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了外部Log4j.properties在不打印日志中-Spring Boot 2.2.1相关的知识,希望对你有一定的参考价值。

[在2.2.1 RELEASE中开发一个Spring Boot应用程序。除使用log4j.properties进行记录外,其他所有操作都正常。

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. Logger not printing with log4j.properties within Spring Boot 1.5.7

2. Spring boot logging with log4.properties file in not working

作为Andy Wilkinson注释,我修改了log4j.properties文件的内容和文件名,文件名应为log4j2.properties。我如何将以下行迁移到log4j2

 private  Logger LOG = Logger.getLogger("Service");
答案

您正在使用Log4j 2(不再支持该版本),但似乎正在使用Log4j 1配置文件对其进行配置。

您可以在documentation中了解有关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 = STDOUT

以上是关于外部Log4j.properties在不打印日志中-Spring Boot 2.2.1的主要内容,如果未能解决你的问题,请参考以下文章

日志——log4j.properties配置文件说明

log4j.properties日志级别debug打印后台sql语句

log4j.properties 打印到控制台 写法

控制台上打印日志文件

配置log4j.properties 详解与配置步骤

log4j程序遇到错误打印日志到文件中