title: Write logs to a file and console in spring boot project using log4j2.xml
body: <p>I  tried to create simple spring boot application in which the log should be printed in both console and to a file which is outside the project folder.The log is able to print in console but not able to write into file . I specified the file location(the file is not created,I think the file automatically generate) </p>
  
  <p>Here is the dependency added</p>
  
  <pre><code>&lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Add Log4j2 Dependency --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-log4j2&lt;/artifactId&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  </code></pre>
  
  <p>Here is the log4j.xml file(location src/main/resources/log4j2.xml)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Configuration status="WARN" monitorInterval="30"&gt;
      &lt;Properties&gt;
          &lt;Property name="LOG_PATTERN"&gt;
              %d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} --- [%15.15t] %-40.40c{1.} : %m%n%ex
          &lt;/Property&gt;
      &lt;/Properties&gt;
  
      &lt;Appenders&gt;
          &lt;Console name="ConsoleAppender" target="SYSTEM_OUT"
              follow="true"&gt;
              &lt;PatternLayout pattern="${LOG_PATTERN}" /&gt;
          &lt;/Console&gt;
  
          &lt;!-- Rolling File Appender --&gt;
          &lt;RollingFile name="FileAppender"
              fileName="/home/ubuntu/Documents/eclipse/logs/log4j2-demo.log"
              filePattern="logs/log4j2-demo-%d{yyyy-MM-dd}-%i.log"&gt;
              &lt;PatternLayout&gt;
                  &lt;Pattern&gt;${LOG_PATTERN}&lt;/Pattern&gt;
              &lt;/PatternLayout&gt;
              &lt;Policies&gt;
                  &lt;TimeBasedTriggeringPolicy interval="1" /&gt;
                  &lt;SizeBasedTriggeringPolicy size="10MB" /&gt;
              &lt;/Policies&gt;
              &lt;DefaultRolloverStrategy max="10" /&gt;
          &lt;/RollingFile&gt;
      &lt;/Appenders&gt;
  
      &lt;Loggers&gt;
  
          &lt;Root level="debug"&gt;
              &lt;AppenderRef ref="ConsoleAppender" /&gt;
              &lt;AppenderRef ref="FileAppender" /&gt;
          &lt;/Root&gt;
      &lt;/Loggers&gt;
  &lt;/Configuration&gt;
  </code></pre>
  
  <p>I added the logging feature in main class</p>
  
  <pre><code>    import org.apache.logging.log4j.LogManager;
  
      import org.apache.logging.log4j.Logger;
  
      import org.springframework.boot.SpringApplication;
  
      import org.springframework.boot.autoconfigure.SpringBootApplication;
  
  @SpringBootApplication
  public class LoggingApplication {
  
      private static final Logger logger = LogManager.getLogger(LoggingApplication.class);
  
  
      public static void main(String[] args) {
          logger.info("in main class");
          logger.info("info logging is printed");
          logger.debug("logger debud is worked");
          logger.warn("logging warn is worked");
          SpringApplication.run(LoggingApplication.class, args);
      }
  
  }
  </code></pre>
  
  <p>Here is the Console :
  <a href="https://i.stack.imgur.com/rhkN8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rhkN8.png" alt="enter image description here"></a></p>