Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write log messages to file using Spring Boot?

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.

like image 978
Qasim Avatar asked May 28 '15 07:05

Qasim


People also ask

Where is log file in spring boot application?

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

How do I get spring boot logs?

Spring boot allows us to see the logs in the console even if we do not provide any specific configuration for it. This is because spring boot uses Logback for its default logging. Spring boot's internal logging provider is Apache Commons which provides support for Java Util Logging ,Log4j2, and Logback.


3 Answers

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.

like image 137
ci_ Avatar answered Sep 23 '22 02:09

ci_


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.

like image 22
Sandip Adisare Avatar answered Sep 27 '22 02:09

Sandip Adisare


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

like image 31
Hardik Patel Avatar answered Sep 23 '22 02:09

Hardik Patel