32

I want to log message in a file and not on the console. I am using Spring Boot and my configuration is as follows:

application.properties:

logging.level: DEBUG
logging.level: ERROR
logging.file: ${HOME}/application.log

I am getting log messages of INFO only in my application.log file but I want ERROR and DEBUG messages as well.

My requirement is that I want ERROR message in error.log file and DEBUG message in debug.log and INFO messages in info.log.

Any help very much appreciated.

4 Answers 4

30

Spring Boot allows you to configure some basic aspects of your logging system using application.properties, but there are limits:

To configure the more fine-grained settings of a logging system you need to use the native configuration format supported by the LoggingSystem in question.

In other words, if you want to do something that isn't specifically supported through properties you won't get around to adding and editing a logback.xml file (assuming you're using logback).

So, let's go through your requirements:

  1. "I want to log message in a file not on console."

According to the docs:

By default, Spring Boot will only log to the console and will not write log files. If you want to write log files in addition (emphasis added) to the console output you need to set a logging.file or logging.path property (for example in your application.properties).

In other words, not logging to the console cannot be done using properties.

  1. "I am getting log messages of info only in my application.log file but I want error as well as debug messages as well."

By default Spring Boot logs on INFO level, which should include ERROR, are you sure you're not getting ERROR logs with the default setting?

Also, you only specify the highest level you want to log, not each level, and you have to specify the logger you want to set the level on.

This won't work:

logging.level: DEBUG
logging.level: ERROR

This is an example how to configure custom log levels according to the docs:

logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR

You can also use the logging.level.* property to set the level of the root logger like this:

logging.level.ROOT: DEBUG

Note that setting DEBUG logging on the ROOT logger will generate a large amount of logs. I've just tested it here and got roughly 13MB of logs just on startup, without doing anything.

  1. "I want error message in error.log file and debug message in debug.log and info messages in info.log."

Again, this cannot be done using properties alone. Spring Boot allows you to configure exactly one logging.file property which will contain all the logs.

For a complete list of logging properties available and sample values see here.

2

If you want to disable console logging and write output only to a file, you need a custom logback-spring.xml that imports file-appender.xml but not console-appender.xml.

It is described in Spring Boot docs: Configure Logback for File-only Output.

1

Here in your case, this won't work as you are trying to set ROOT logging level to multiple levels.

logging.level: DEBUG
logging.level: ERROR

These are different logging levels and its order from minimum << maximum.

OFF << FATAL << ERROR << WARN << INFO << DEBUG << TRACE << ALL

# To set logs level as per your need.
logging.level.org.springframework = debug
logging.level.tech.hardik = trace

# To store logs to external file
# Here use strictly forward "/" slash for both Windows, Linux or any other os, otherwise, your logs it won't work.      
logging.file=D:/spring_app_log_file.log

# To customize logging pattern.
logging.pattern.file= "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"

Please pass through this doc to customize your logs more vividly.

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html

0

Instead of using those properties, try adding a logback.xml in the same folder as your application.properties. Configure logback.xml any way you need it to log.

1
  • thanks for replying but i think we use logback.xml for non-spring boot application , cant we do the configuration in application.properties
    – Qasim
    May 28, 2015 at 7:32

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.