19

I'm using the default logging configuration of spring-boot.

How can I prevent the console output, while keeping the logging into a logfile configured with logging.file=myfile.log?

My goal is to not having console windows output, but only logging to that file.

Without having to create a specific logback.xml configuration. Because I'm using spring-boot for not having to configure the logging myself.

mat
  • 1,645
  • 15
  • 36
membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • Check this: http://stackoverflow.com/questions/31935151/prevent-spring-boot-from-printing-logs-to-console – Afridi May 11 '17 at 14:36
  • you can create a logback.xml file with your appenders. There is no such option to do just from properties file. – pvpkiran May 11 '17 at 14:37
  • Did you went through this post, looks similar http://stackoverflow.com/questions/31935151/prevent-spring-boot-from-printing-logs-to-console – mhasan May 11 '17 at 14:38
  • Possible duplicate of [Prevent Spring Boot from printing logs to console](http://stackoverflow.com/questions/31935151/prevent-spring-boot-from-printing-logs-to-console) – lbndev May 11 '17 at 14:38
  • I'm looking for a solution where I'm could just like add a property in `application.properties`, and not having to create an explicit logback.xml – membersound May 11 '17 at 14:40
  • Are you using `log4j`? If yes, just configure your `log4j.properties`. – cнŝdk May 11 '17 at 14:59
  • As written, I just rely on the default `spring-boot` configuration. I think it uses logback by default. – membersound May 11 '17 at 15:06

4 Answers4

30

It turned out if I set the following property empty, the console logging is disabled:

logging.pattern.console=

Or commenting in xml if you use it

  <!--<root level="error">-->
        <!--<appender-ref ref="console"/>-->
    <!--</root>-->
shareef
  • 9,255
  • 13
  • 58
  • 89
membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • 1
    Not pretty, but this is pretty much the only way to disable console logging without adding a logback.xml. In my use case, adding the extra file would have been a big deal. – Bogdan Calmac Jul 28 '17 at 03:02
  • Your approach would disable even the printing of logs that one sees while an application starts. Better choice is only to disable web call logs as - "logging.level.org.springframework.web=OFF". It's actually important to see if the application has started successfully. – Amit Kumar Aug 28 '17 at 14:15
  • 5
    error happens in new version: ch.qos.logback.classic.PatternLayout("") - Empty or null pattern – Geln Yang Mar 20 '18 at 06:20
2

I created a file called logback.xml with the content:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    <logger name="org.springframework" level="ERROR"/>
    <logger name="org.hibernate" level="ERROR"/>
</configuration>

See this link for more information: https://www.mkyong.com/spring-boot/spring-boot-test-how-to-stop-debug-logs/

2
  1. Create a file logback-spring.xml in /resources/ folder of your application.
  2. Copy the below content in logback-spring.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<configuration>
    <appender name = "STDOUT" class = "ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%m%n</pattern>
        </encoder>
    </appender>
    <root level = "INFO">
        <appender-ref ref = "STDOUT"/>
    </root>
</configuration>
  • You can add : – skyho Oct 10 '20 at 17:15
  • Thanks for the *"in /resources/ folder of your application"*. For some strange reason, all documentations and tutorials specify the location of `logback-spring.xml ` in an ambiguous and hard to understand manner. – Introspective Jul 19 '23 at 15:33
-1

All the supported logging systems can have the logger levels set in the Spring Environment using ‘logging.level.*=LEVEL’ where ‘LEVEL’ is one of TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF. The root logger can be configured using logging.level.root. Example application.properties:

logging.level.root=WARN
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR

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

You could also overwrite the logback configuration and provide the value OFF to it.

<configuration>
  <!-- turn OFF all logging (children can override) -->
  <root level="OFF">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

You can find more information at the following link: https://logback.qos.ch/manual/configuration.html#rootElement

dtenreiro
  • 168
  • 5