javaspring-bootlogging

Log format on the console is different from the log format in the output file


I'm using Spring Boot 3.5.3 and I'm having a different format between the log from console and the output log file generated.

Here is my log dependency:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

My application.yml logging settings:

logging:
  pattern:
    console: '%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n'
  level:
    com.app.core: info
  file:
    name: C:/AutomaticVideoMaker/logs/app-core.log

In my Java class, I have this code:

import com.dotaytcore.searchDecisionEngine.model.SearchCriteria;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class AutomaticVideoMaker {
    
    private static final Logger logger = LoggerFactory.getLogger(AutomaticVideoMaker.class.getSimpleName());

    private final AutomaticSearchDecisionService searchDecisionService;

    public void generate(){
        logger.info("Starting automatic video generation...");
    }
}

In the console, I have the following format message:

2025-07-20 01:25:12 INFO AutomaticVideoMaker - Starting automatic video generation...

Which is the format that I want. On the other hand, that is the format that I have in the generated log file:

2025-07-20T01:25:12.461-03:00 INFO 25676 --- [main] AutomaticVideoMaker : Starting automatic video generation...

I want to simplify the time, remove the PID, remove the separator and the application-group. For some reason I can't accomplish that in the output file!


Solution

  • OK, the documentation is not so clear about that so after a luck guess I found the solution, inside logging.pattern, all you need to do is add a new entry called file and add the format that you want:

    logging:
      include-application-group: false
      include-application-name: false
      pattern:
        file: '%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n'
        console: '%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n'
      level:
        com.dota.yt.core: info
      file:
        name: C:/AutomaticVideoMaker/logs/dota-core.log
    

    That's it, works like a charm.