Spring Boot logging to file in Windows PC with application.yml

Viewed 2269

I have searched the internet for some hours now. I cannot find this trivial thing. I am using a Windows PC. I am using Spring boot and using all the defaults. I want to log to a file and am using YAML application format. My application.ymlis in src/main/resources. Its contents are exactly those:

spring:
  logging:
    file: logs/app.log
    pattern:
      console: "%d [%t] %-5level %logger{36} - %msg%n"
      file: "%d [%t] %-5level %logger{36} - %msg%n"
    level:
      com.m2evorah: DEBUG
      org.springframework: DEBUG
      org.hibernate: DEBUG

I can see the logging normally in console within Eclipse IDE, but then when I go and search my PC for an app.log file, I cannot find it. So, even if it goes according to classpath etc, I should still be able to see it in my searches. What is wrong with my yml file?

3 Answers

Wow! I finally got it working. The successful application.yml for me was this:

logging:
  file.name: logs/app.log
  pattern:
    console: "%d [%t] %-5level %logger{36} - %msg%n"
    file: "%d [%t] %-5level %logger{36} - %msg%n"
  level:
    com.m2evorah: DEBUG
    org.springframework: DEBUG
    org.hibernate: DEBUG

Notice the file.name was used.

logging.file property is not under spring property tree.

In your case path is defined as spring.logging.file.

Yaml should look like this:

logging:
  file.name: logs/app.log
  pattern:
    console: "%d [%t] %-5level %logger{36} - %msg%n"
    file: "%d [%t] %-5level %logger{36} - %msg%n"
  level:
    com.m2evorah: DEBUG
    org.springframework: DEBUG
    org.hibernate: DEBUG

EDIT:

Instead of logging.file property logging.file.name should be used

Instead of putting the logging configuration in application.yml create log4j.properties/log4j.yml file in the same src/main/resources folder and put the below configurations

# Root logger option
log4j.rootLogger=INFO, file

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.datePattern='_'yyyy-MM-dd'.log'

log4j.appender.file.File=logs/Facts.log

log4j.appender.file.layout=org.apache.log4j.PatternLayout

log4j.appender.file.layout.ConversionPattern=timestamp=%d{yyyy-MM-dd HH:mm:ss} | level=%-5p | source=%c{1}:%L | logMessage=%m%n

Note: logs folder will be created in your project which contains Facts.log file and every day new log files will be generated and keep track of your project on daily basis

Just place below code in your specific class where you want to apply the logger

Logger logger = (Logger) LoggerFactory.getLogger(<classname>.class);
Related