0
0
SpringbootHow-ToBeginner · 3 min read

How to Log to File in Spring Boot: Simple Setup Guide

In Spring Boot, you can log to a file by configuring the logging.file.name or logging.file.path property in application.properties or application.yml. This tells Spring Boot to write logs to the specified file or directory automatically without extra code.
📐

Syntax

Spring Boot uses properties to control logging output. The main properties are:

  • logging.file.name: Specifies the exact file path for the log file.
  • logging.file.path: Specifies a directory where Spring Boot creates a default log file named spring.log.
  • logging.level.*: Controls the log level (e.g., INFO, DEBUG) for packages or classes.

These properties go into application.properties or application.yml in your project.

properties
# application.properties
logging.file.name=logs/myapp.log
logging.level.root=INFO
💻

Example

This example shows how to configure Spring Boot to log to a file named myapp.log inside a logs folder. The log level is set to INFO.

When you run the Spring Boot application, logs will be saved to logs/myapp.log automatically.

java + properties
package com.example.loggingdemo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.CommandLineRunner;

@SpringBootApplication
public class LoggingDemoApplication implements CommandLineRunner {

    private static final Logger logger = LoggerFactory.getLogger(LoggingDemoApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(LoggingDemoApplication.class, args);
    }

    @Override
    public void run(String... args) {
        logger.info("Application started successfully.");
        logger.debug("This debug message will not appear because level is INFO.");
    }
}

# application.properties
logging.file.name=logs/myapp.log
logging.level.root=INFO
Output
[INFO ] 2024-06-01 12:00:00.000 [main] com.example.loggingdemo.LoggingDemoApplication - Application started successfully.
⚠️

Common Pitfalls

1. Log file not created: This happens if the directory does not exist or the path is incorrect. Make sure the folder exists or Spring Boot has permission to create it.

2. Logs not appearing in file: Check that logging.file.name or logging.file.path is set correctly and not overridden elsewhere.

3. Using both logging.file.name and logging.file.path: Avoid setting both at the same time as it can cause conflicts.

properties
# Wrong: setting both properties
logging.file.name=logs/app.log
logging.file.path=logs

# Right: use only one
logging.file.name=logs/app.log
📊

Quick Reference

PropertyDescriptionExample Value
logging.file.nameFull path to the log filelogs/myapp.log
logging.file.pathDirectory for default log file spring.loglogs
logging.level.rootSet global log levelINFO
logging.level.com.exampleSet log level for packageDEBUG

Key Takeaways

Set logging.file.name or logging.file.path in application.properties to enable file logging.
Ensure the log directory exists or Spring Boot can create it to avoid missing log files.
Avoid setting both logging.file.name and logging.file.path simultaneously to prevent conflicts.
Use logging.level.* properties to control the verbosity of logs in your file.
Spring Boot automatically handles file creation and log rotation with these settings.