15

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?

Karthik
  • 4,950
  • 6
  • 35
  • 65
  • With latest 1.3.1 release [Answered here](http://stackoverflow.com/questions/26019350/how-to-disable-logback-consoleappender-in-spring-boot/34852737#34852737) – Pratap A.K Jan 18 '16 at 10:45
  • 1
    With latest 1.3.1 release tested here [Answered here](http://stackoverflow.com/questions/26019350/how-to-disable-logback-consoleappender-in-spring-boot/34852737#34852737) – Pratap A.K Jan 18 '16 at 10:46

5 Answers5

16

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.

hovanessyan
  • 30,580
  • 6
  • 55
  • 83
  • 1
    but I specified `logging.file` right? shouldn't it stop printing it to console? – Karthik Aug 11 '15 at 09:42
  • 1
    no. checkout the logback base.xml in spring-boot - find sources on github - you will see two log appenders defined - file and console. The path you supply is passed on to the file-appender - you still have to turn off the console-appender. – hovanessyan Aug 11 '15 at 09:47
  • 1
    The relevant part of the docs is [this](http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-logging-file-output) "If you want to write log files **in addition** to the console output you need to set..." – ci_ Aug 11 '15 at 15:44
  • This solution doesn't work for me :( I had to take all the content of base.xml and redefine the root logger in order to have nothing on console. – user2668735 Sep 15 '17 at 10:51
7

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

K.Mulier
  • 8,069
  • 15
  • 79
  • 141
panksy2k
  • 401
  • 5
  • 3
3

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
Kaliappan
  • 642
  • 9
  • 24
1

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.

Community
  • 1
  • 1
0

Tested with latest 1.3.1 release, with newer releases base.xml has been renamed to defaults.xml

Answer here

Community
  • 1
  • 1
Pratap A.K
  • 4,337
  • 11
  • 42
  • 79