Foreign
Foreign

Reputation: 405

Run spring app with --debug doesn't affect my loggers

private static final Logger myLogger = LoggerFactory.getLogger("NAME");

I have a couple of loggers created like the example above, the thing is, when I run my Spring app with --debug, myLogger.debug("something") isn't logged, only spring default/internal loggers acctually make use of the --debug args.

How can I make my loggers also use --<level> args?

Upvotes: 4

Views: 1816

Answers (2)

Depanker Sharma
Depanker Sharma

Reputation: 72

Hi Following is a sample logback.xml, to be placed in the resource folder

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <logger name="<PACKAGE ON WHICH YOU WOULD LIKE TO ENABLE DEBUGGING (EXAMPLE: com.mypackage)>" level="DEBUG" />
   <root level="ERROR">
    <appender-ref ref="STDOUT" />
    <appender-ref ref="DAILY_ROLLING" />
    <appender-ref ref="SYSLOG" />
  </root>
</configuration>

Change <PACKAGE ON WHICH YOU WOULD LIKE TO ENABLE DEBUGGING (EXAMPLE: com.mypackage))> with your own package on which you would like to enable DEBUG.

Alternatively, if you would like to enable root level on all the packages replace <root level="ERROR" > with <root level="DEBUG" >. And it will print debug messages of all the code even if it's printed in dependencies. Also, you can remove <logger name="<PACKAGE ON WHICH YOU WOULD LIKE TO ENABLE DEBUGGING (EXAMPLE: com.mypackage)>" level="debug" /> if the log level of your package is the same as root level.

Upvotes: 0

s7vr
s7vr

Reputation: 75934

This is by design. It is not possible to make the other loggers use —debug or any other level.

From the docs,

The default log configuration echoes messages to the console as they are written. By default, ERROR-level, WARN-level, and INFO-level messages are logged. You can also enable a “debug” mode by starting your application with a --debug flag.

$ java -jar myapp.jar --debug

[Note] You can also specify debug=true in your application.properties.

When the debug mode is enabled, a selection of core loggers (embedded container, Hibernate, and Spring Boot) are configured to output more information. Enabling the debug mode does not configure your application to log all messages with DEBUG level.

Alternatively, you can enable a “trace” mode by starting your application with a --trace flag (or trace=true in your application.properties). Doing so enables trace logging for a selection of core loggers (embedded container, Hibernate schema generation, and the whole Spring portfolio).

However you can use below key value to log all debug messages in application properties file

logging.level.root=debug

More on the implementation here https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java

Upvotes: 4

Related Questions