Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Spring Boot from printing logs to console

I am using spring boot for my application and I am using default spring boot logging.

In my application.properties, I have added file path for logging.file,

        logging.file= ${logger_path}

and my pom.xml contains

       <logger_path>/tmp/app.log</logger_path>

When I start the application, it prints the logging messages to the file at /tmp/app.log, but the problem is it also prints the log messages on the console. I really don't understand why it is printing on console (though it is printing them to the specified file) when I have specified a log file.

Is there any configuration to prevent spring boot from printing the log messages to console?

like image 599
Karthik Avatar asked Aug 11 '15 06:08

Karthik


People also ask

How do I disable Logback logging?

Logback does not allow logging to be disabled from the command line. However, if the configuration file allows it, you can set the level of loggers on the command line via a Java system property.


4 Answers

Spring boot comes with build-in logback logger, which is configured to print to the console by default.

You need to overwrite the logback configuration (providing your own logback.xml on the classpath). This is described here - - section 66.1

How to disable logback logging read here -

as you can see you have to provide the value OFF...something like:

<configuration>
  <include resource="base.xml" />
   .
   .
  <property name="root.level.console" value="OFF" />
</configuration>

Note: Keep in mind that the general idea here is that the logback configuration coming from spring-boot is minimal. The intention is that you provide your own logback configuration and completely overwrite the existing one - e.g. providing your own logback configuration with only log file-appender configured - this should be your general approach.

like image 181
hovanessyan Avatar answered Oct 17 '22 00:10

hovanessyan


Include file-appender.xml and not console-appender with following configuration in logback-spring.xml:

<?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <include resource="org/springframework/boot/logging/logback/defaults.xml" />
        <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
        <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
        <root level="INFO">
            <appender-ref ref="FILE" />
        </root>
    </configuration>

You also need to add logging.file to your application.properties

This is in compliance to what is mentioned in spring boot documentation - http://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html

like image 20
panksy2k Avatar answered Oct 17 '22 01:10

panksy2k


I tried removing console configuration from logback.xml. But, It was still logging in console. So What I did is, I just removed the appender being added in the logging configuration by springboot. Thereby, we can stop springboot logging in console and separate log file. Add the below lines at the end of your application specific appenders are added. Your custom appender should not match any of these appender names. It worked for me.

// get instance of your log4j instance
Logger logger = LogManager.getRootLogger();
logger.removeAppender("CONSOLE"); // stops console logging
logger.removeAppender("LOGFILE"); // stops file logging
like image 45
Kaliappan Avatar answered Oct 17 '22 02:10

Kaliappan


A similar discussion can be found here.

My logback.xml looks like that:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <include resource="org/springframework/boot/logging/logback/defaults.xml" />
  <include resource="org/springframework/boot/logging/logback/file-appender.xml" />

  <root level="INFO">
    <appender-ref ref="FILE" />
  </root>
</configuration>

Just in case you still experience an issue: I have read many discussions about that topic and it was not working at all for me. Unfortunately I have made a really stupid mistake when I have created the file logback.xml for the very first time: A space was added at the beginning of the filename. Therefore the file was never used. So just have another look on the filename, add the file in "src/main/resources" and remove any "logging.config" entries from your property files.

like image 22
Maximilian Wollnik Avatar answered Oct 17 '22 01:10

Maximilian Wollnik