简体   繁体   中英

How to add logging properties to spring boot application

I'm trying to add log4j2 properties to my spring boot application but no log file is created with the given log file name and no errors are thrown when running the application.

There is nothing i added to application.properties file.
Below are the dependencies added to my pom.xml file

<parent>
 <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
    <start-class>com.boot.test.SpringApp</start-class>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.7</java.version>
</properties>
<dependencies>
 <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-web</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>
 <dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-api</artifactId>
  <version>2.7</version><!--$NO-MVN-MAN-VER$-->
 </dependency>
 <dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-core</artifactId>
  <version>2.7</version><!--$NO-MVN-MAN-VER$-->
 </dependency>
 <dependency>
</dependencies>

Below is the log4j2.xml file

<Configuration status="INFO">
 <Properties>
   <Property name="filename">c:\temp\spring-boot-example.log</Property>
   <Property name="defaultpattern">%d{ISO8601} %thread level=%level %logger{36} %msg%n</Property>
 </Properties>
 <Filter type="ThresholdFilter" level="trace"/>

 <Appenders>
    <Appender type="File" name="File" fileName="${filename}">
        <Layout type="PatternLayout" pattern="${defaultpattern}" />
    </Appender>
 </Appenders>

 <Loggers>
    <Logger name="com.att.sdnmon.odl" level="info" additivity="false">
        <AppenderRef ref="File"/>
    </Logger>
    <Root level="warn">
        <AppenderRef ref="File"/>
    </Root>
 </Loggers>
</Configuration>

Your maven configuration is ok, you can leave out the <exclusions> part from the spring-boot-starter-web dependency, putting this exclusion in the spring-boot-starter dependency is enough.

In the log4j2.xml file you are using the strict configuration syntax, so your first line must be:

<Configuration status="info" strict="true">

Where have you put the config file? It must be in the root of the classpath.

It just tested this with a sample spring boot project having the log4j2.xml file in src/main/resources and it worked without problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM