Possible: Override logback logging level with environment variables - spring-boot

I currently have the following logger defined in my logback.xml:
<logger name="Event" level="INFO" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>
Is it possible to override the level here using an environment variable? I am using SpringBoot.
I have tried having these defined as ENV variables:
LOGGING_LEVEL_ROOT=ERROR
LOGGING_LEVEL_EVENT=ERROR
LOGGER_LEVEL_EVENT=ERROR
However none of these worked.

Spring Boot supports springProfile extension for the logback configuration file:
<springProfile name="test"> </springProfile>
Documentation
Also there is another way to do it using LoggingApplicationContextInitializer.

Related

Spring boot - how to reduce log output from specific library?

I am using the library net.iakovlev.timeshape.TimeZoneEngine and it is spewing a lot of [DEBUG] messages on startup which seems to be the default behaviour.
In application.yml I should be able to control logging level with but that's not working somehow.
logging:
level:
net.iakovlev.timeshape: ERROR
Isn't this the correct way to do so?
Set your log configuration file, and add something like this :
...
<logger name="net.iakovlev.timeshape" level ="ERROR" additivity="false">
<!-- YOUR APPENDER -->
<appender-ref ref="CONSOLE" />
</logger>
...

Setting level to a logger in spring-boot

In logback-spring.xml file of a project that is new to me I see this configuration
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
......
</appender>
<logger name="com.myproject.myclass" level="${APP_LOGGING_LEVEL:-INFO}" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>
As far as I understand, all logs from com.myproject.myclass class will be sent to <appender name="STDOUT"> that is console.
Could someone explain me what will be the level of this log with this config level="${APP_LOGGING_LEVEL:-INFO}"? In documentation I see only constants like WARN, DEBUG, etc
In Spring ${APP_LOGGING_LEVEL:-INFO} means APP_LOGGING_LEVEL is a dynamic variable. The value for which can be set from properties file or from command line when running the project or from any other place accessible. If not set whatever after : is selected.
In your case if not set INFO is selected

Spring Profile not available in Logback when run via Maven

I've set up logback.xml to select which appender to use based on the active Spring profile. The technique works perfectly when I run the app using
java -jar -Dspring.profiles.active=local path/to/target/application.war
but not when I run it using the Spring Boot Maven Plugin, e.g.
mvn spring-boot:run -Drun.profiles=local
Here's the relevant section of the logback.xml
<root level="INFO">
<if condition='"${spring.profiles.active}".contains("local")'>
<then>
<appender-ref ref="CONSOLE"/>
</then>
<else>
<appender-ref ref="FILE"/>
</else>
</if>
</root>
I will note that the profile does show up correctly in the application itself, just isn't available when processing logback.xml.
The problem also manifests when running from the IntelliJ IDE.
Is there another way to use the Maven Spring Boot Plugin to cause the profile to be visible to the logback.xml parser, and would it work for IntelliJ, as well?
Have you tried to configure logback through logback-spring extension?
In your case logback-spring.xml could look like this:
<?xml version="1.0" encoding="UTF-8"?>
<include resource="org/springframework/boot/logging/logback/base.xml"/><!-- include this config if you want to use spring-boot's built-in appenders -->
<configuration>
<root level="INFO">
<springProfile name="local">
<appender-ref ref="CONSOLE"/>
</springProfile>
<springProfile name="otherprofile">
<appender-ref ref="FILE"/>
</springProfile>
</root>
</configuration>
More information about available options in logback-spring extensions:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html#boot-features-logback-extensions

how to configure specific package in log4j.properties

I need to have log4j configuration for specific classes. Does anyone know how can we do it with log4j.properties.
As against, we can add logger with class/package in log4j.xml
<logger name="org.springframework.security">
<level value="info" />
<appender-ref ref="console" />
</logger>
I want equivalent configuration in log4j.properties?
The pattern is log4j.logger.<class or package name>=<level>[, appender]
log4j.logger.org.springframework.security=INFO
log4j.logger.org.springframework.security=INFO, console

hibernate logback sql

I want to see the actual parameters of my SQL queries when I use Hibernate. I add this to my logback.xml to see the queries (with question marks):
<logger name="org.hibernate.type" level="TRACE" />
but to no effect.
Is there any special configuration necessary?
OnConsoleStatusListener shows me the correct configuration
23:48:15,246 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.hibernate.type] to TRACE
but no output from org.hibernate.type package.
I'm using Spring with Jpa.
Things you have to make sure:
Are you sure that SLF4J + LogBack is working in your app?
Is your logger pointing to any appender?
Have you configured an appended?
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<!-- "application-name" is a variable -->
<File>c:/logs/${application-name}.log</File>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d %p %t %c - %m%n</Pattern>
</layout>
</appender>
<root level="debug">
<appender-ref ref="FILE"/>
</root>
</configuration>
I'm using this configuration, and it works for me:
<logger name="org.hibernate.type" level="trace" additivity="false">
<appender-ref ref="consoleAppender" />
</logger>
The logger that works for me is the following:
<logger name="org.hibernate.type" level="TRACE" />

Resources