Close

Spring Boot - Setting log file by using logging.file and logging.path properties

[Last Updated: Nov 6, 2017]

By default Spring Boot does not output logs to any file. If we want to have logs written in a file (in addition to the console output) then we should use either of logging.file or logging.path properties (not both).

Let's understand what rules are applied with examples:

  • (1)logging.file=my-file.txt This will write logs to my-file.txt at the location where application is running (the working directory). Within an application, the working directory can be found by System.getProperty("user.dir").

  • (2)logging.file=/my-folder/my-file.txt In this case, the location will be the root folder in the current partition. For example if we are running in windows and application is running somewhere in the directory D: then the log file will be in D:\my-folder\my-file.txt. In Linux, it will be created under the root directory where only root user has the permission (sudo commands applied). In both cases, the user who is running the application should have the permission to create files, otherwise no log files will be created. In windows, we can also use paths like D:/my-folder/my-file.txt or D:\\my-folder\\my-file.txt (not recommended).

  • (3)logging.path=/my-folder/ It will write logs to /myfolder/spring.log in the root directory.
    If we use both logging.path and logging.file then logging.path property will be ignored.

Example

src/main/resources/application.properties

spring.main.banner-mode=off
logging.file=/my-logs/app.log
#logging.path=/my-logs/ will create spring.log
@SpringBootApplication
public class ExampleMain {
  private static final Logger logger = LoggerFactory.getLogger(ExampleMain.class);

  public static void main(String[] args) throws InterruptedException {
      System.out.println("Current Directory = " + System.getProperty("user.dir"));
      SpringApplication.run(ExampleMain.class, args);
      logger.info("just a test info log");
  }
}

Output

Running application via spring-boot plugin:

D:\example-projects\spring-boot\boot-logging-file-example>mvn -q spring-boot:run
Current Directory = D:\example-projects\spring-boot\boot-logging-file-example
2017-11-05 23:35:07.819 INFO 18872 --- [ main] com.logicbig.example.ExampleMain : Starting ExampleMain on JoeMchn with PID 18872 (D:\example-projects\spring-boot\boot-logging-file-example\target\classes started by Joe in D:\example-projects\spring-boot\boot-logging-file-example)
2017-11-05 23:35:07.822 INFO 18872 --- [ main] com.logicbig.example.ExampleMain : No active profile set, falling back to default profiles: default
2017-11-05 23:35:07.856 INFO 18872 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@9bc901: startup date [Sun Nov 05 23:35:07 CST 2017]; root of context hierarchy
2017-11-05 23:35:08.295 INFO 18872 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-11-05 23:35:08.305 INFO 18872 --- [ main] com.logicbig.example.ExampleMain : Started ExampleMain in 0.75 seconds (JVM running for 3.334)
2017-11-05 23:35:08.306 INFO 18872 --- [ main] com.logicbig.example.ExampleMain : just a test info log
2017-11-05 23:35:08.311 INFO 18872 --- [ Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@9bc901: startup date [Sun Nov 05 23:35:07 CST 2017]; root of context hierarchy
2017-11-05 23:35:08.312 INFO 18872 --- [ Thread-1] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown

The log file is written at following location:

D:>dir my-logs

Directory of D:\my-logs

11/05/2017 11:42 PM <DIR> .
11/05/2017 11:42 PM <DIR> ..
11/05/2017 11:42 PM 1,482 app.log
1 File(s) 1,482 bytes
2 Dir(s) 41,900,355,584 bytes free
D:>type my-logs\app.log
2017-11-05 23:42:02.507 INFO 2520 --- [main] com.logicbig.example.ExampleMain : Starting ExampleMain on JoeMchn with PID 2520 (D:\example-projects\spring-boot\boot-logging-file-example\target\classes started by Joe in D:\example-projects\spring-boot)
2017-11-05 23:42:02.522 INFO 2520 --- [main] com.logicbig.example.ExampleMain : No active profile set, falling back to default profiles: default
2017-11-05 23:42:02.576 INFO 2520 --- [main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5f71c76a: startup date [Sun Nov 05 23:42:02 CST 2017]; root of context hierarchy
2017-11-05 23:42:03.145 INFO 2520 --- [main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-11-05 23:42:03.145 INFO 2520 --- [main] com.logicbig.example.ExampleMain : Started ExampleMain in 0.87 seconds (JVM running for 1.177)
2017-11-05 23:42:03.145 INFO 2520 --- [main] com.logicbig.example.ExampleMain : just a test info log
2017-11-05 23:42:03.145 INFO 2520 --- [Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@5f71c76a: startup date [Sun Nov 05 23:42:02 CST 2017]; root of context hierarchy
2017-11-05 23:42:03.145 INFO 2520 --- [Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown

Example Project

Dependencies and Technologies Used:

  • Spring Boot 1.5.8.RELEASE
    Corresponding Spring Version 4.3.12.RELEASE
  • spring-boot-starter : Core starter, including auto-configuration support, logging and YAML.
  • JDK 1.8
  • Maven 3.3.9

Spring Boot - logging.properties example Select All Download
  • boot-logging-file-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources
          • application.properties

    See Also