在Spring Boot中配置log4j2.properties

时间:2018-01-02 15:09:40

标签: spring spring-boot logging log4j log4j2

我是Spring Boot的新手。我有一个正在运行的Spring Boot项目。我想使用log4j2(由于项目限制,我必然会使用log4j2)将不同级别的所有日志重定向到名为' test.log'

的日志文件

问题是 - 尽管包含了所有正确的代码,但我无法在test.log中记录INFO级别日志 (我希望显然会记录错误和调试级别日志,但至少第一个INFO级别日志应该工作正常)

---我已经排除了默认日志记录,并在pom.xml中包含了log4j2依赖项:

<!-- Exclude Spring Boot's Default Logging -->
        <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>

        <!-- Add Log4j2 Dependency -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.10.0</version>
        </dependency> 

---我在application.properties中包含了logging.config:

logging.file=logs/test.log
logging.level.*=INFO
logging.config=src/main/resources/log4j2.properties

---我的log4j2.properties如下所示:

#status = error // do i need this actually??
dest = logs/test.log
name = PropertiesConfig

property.filename = logs/test.log

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = logs/test.log
appender.rolling.filePattern = logs/test1-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d %-5p %-17c{2} (%30F:%L) %3x - %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=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5

logger.rolling.name = com.thp.clinic.allergiesConditions
logger.rolling.level = info
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile
#rootCategory=INFO,rolling,stdout
#logging.level.root=info

#rootLogger.level = debug  //do i necessarily need root Logger????
#rootLogger.appenderRefs = RollingFile
#rootLogger.appenderRef.stdout.ref = STDOUT

logger.rolling.name=org.hibernate.SQL
logger.rolling.level = debug

---我的测试API的控制器还有以下测试日志行:

    //Logger logger = LogManager.getLogger //is included       
logger.debug("This is a debug message");
logger.info("This is an info message");
logger.warn("This is a warn message");
logger.error("This is an error message");
logger.fatal("This is a fatal message");

---根据我的理解,我在代码中包含了所有必需的东西。但我遇到了这个问题 - 当我点击API时,只有hibernate调试记录器被添加到test.log;我已经包含在控制器中的五个测试记录器(甚至使用的其他信息级别日志)没有登录test.log;

控制台看起来如下(控制台上显示五个记录器中的两个,但这里也缺少INFO级记录器):

20:05:42.989 [http-nio-8000-exec-1] ERROR com.test.app.appController - This is an error message
20:05:42.994 [http-nio-8000-exec-1] FATAL com.test.app.appController - This is a fatal message
Hibernate: //used hibernate queries are displayed to console as needed
  

如果有人可以指出我需要改变什么,那将是很有帮助的   在代码中。由于对log4j2的理解不当,我猜   需要在log4j2.properties中更改某些内容

请帮忙!! 提前致谢

1 个答案:

答案 0 :(得分:0)

log4j2从.properties文件切换到yaml或xml。您需要创建一个yaml或xml文件才能加载log4j2。

示例:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Configuration status="WARN">
    <Appenders>
        <Console name="ConsoleAppender" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{dd-MMM-yyyy HH:mm:ss.SSS} [%-5p] %m%n" />
        </Console>
        <RollingFile name="myapp" fileName="${sys:catalina.base}/logs/myapp.out"
            filePattern="${sys:catalina.base}/logs/myapp-%d{yyyy-MM-dd}.log.gz">
            <PatternLayout pattern="%d{dd-MMM-yyyy HH:mm:ss.SSS} [%-5p] %m%n" />
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="10 MB" />
            </Policies>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="TRACE">
            <AppenderRef ref="ConsoleAppender" level="INFO"/>
            <AppenderRef ref="myapp" level="INFO"/>
        </Root>
    </Loggers>
</Configuration>