logback.xml change configuration at runtime - spring-boot

is it possible to change the configuration of the logback.xml at runtime with an external file? I dont want to change it programatically. I am using spring boot 1.5
Thanks in advance!

Use logging.config property. Can be a file relative to current working directory or an absolute path.
Option can be passed as argument when running a fat jar (with --logging.config=) or as environment variable (i.e. LOGGING_CONFIG=).
Logging levels can be set dynamically during runtime with actuator, see: https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/actuator-api/html/#loggers.
For a logback config file to get you started, see:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="org.springframework.web" level="DEBUG"/>
</configuration>
From Configure Logback for Logging.

Related

How to force spring to do not zip log files, instead append date at end of log file

Currently I'm using spring boot logs and I'm configuring it through property file
below are the sample logging property
spring.main.banner-mode=off
logging.level.root= INFO,ERROR,DEBUG
logging.level.org.springframework.web= ERROR
logging.level.com.concretepage= DEBUG
logging.pattern.console=
logging.file = D://logTest.log
logging.file.max-size=100MB
spring.output.ansi.enabled=ALWAYS
The problem is that log file backup format is in .gz format
like logTest.log.2019-06-14.0.gz
How do I exclude default zipping ?
I don't want to hard wire configuration in xml file and put it inside resource folder.
I can only put rolling appender configuration xml file, but I want to make logging file path in property file, So I can dynamically set it for different environment.
Is there any way to achieve this configuration?
As an alternative to #simon-martinelli's correct answer, if you did not wish to use a custom logback-spring.xml file, the Spring configuration parameter logging.pattern.rolling-file-name can be set in your application.properties or application.yml file.
For example, to disable the compression used, remove the .gz suffix from the default file name pattern (${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz as per https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-custom-log-configuration).
This would require the addition of the following element to the application.yml file:
logging:
pattern:
rolling-file-name: "${LOG_FILE}.%d{yyyy-MM-dd}.%i"
Or if you are using application.properties, it would be:
logging.pattern.rolling-file-name = ${LOG_FILE}.%d{yyyy-MM-dd}.%i
Create a logback-spring.xml file in src/main/resources
With this content
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<file>${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<cleanHistoryOnStart>${LOG_FILE_CLEAN_HISTORY_ON_START:-false}</cleanHistoryOnStart>
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>${LOG_FILE_MAX_SIZE:-10MB}</maxFileSize>
<maxHistory>${LOG_FILE_MAX_HISTORY:-7}</maxHistory>
<totalSizeCap>${LOG_FILE_TOTAL_SIZE_CAP:-0}</totalSizeCap>
</rollingPolicy>
</appender>
</configuration>
If the fileNamePattern don't end with gz (or any other compression format) logback will not compress the files.

Spring boot set log level for all tests at once (without changing every test)

Is there a way to set the logging level only for tests at once? (Without copying the complete application.yml to test/resources)
Using a specific profile would mean to add this manually to all tests. Using TestPropertySource would also mean to change all tests.
Is there something that i can put into test/resources that just sets logging and take all other properties from default/active profile?
If you're using logback, you can put logback-test.xml in src/test/resources and define all the relevant configurations there
So if you have a regular logback.xml in src/main/resources of the module, the logback-test.xml will override the definitions from the "regular" file
Example:
<?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="com.myapp" level="INFO"/>
</configuration>

logback-spring.xml loaded before spring boot application configuration properties

I have my own logback base.xml file where i define pre-defined file appenders that are used by different applications.
I want the log directory to be configurable per application with a property in application.properties (log.path) and have a default in case none is provided (/var/log), so i have:
base.xml
<included>
<property name="logPath" value="${logPath:-/var/log}"/>
<appender name="TEST" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logPath}/test.log</file>
...
</appender>
logback-spring.xml for spring boot application:
<configuration>
<springProperty scope="context" name="logPath" source="log.path" />
<include resource="base.xml" />
<root level="INFO">
<appender-ref ref="TEST"/>
</root>
</springProfile>
For some reason i end up with two log directories, both /var/log and "log.dir", it seems base.xml is interpreted before spring boot environment is ready.
I'm running spring-boot 1.5.2 comes with logback 1.1.11.
It seems the problem was caused by adding spring-cloud.
During spring cloud's boostraping process, log.dir property is not found and logback creates an logDir_IS_UNDEFINED directory. After the bootstrap process logback is re-initialized with right configuration.
Related spring-cloud issue: issue-197
See Spring Documentation, especially the section about how properties are carried over to logback. Try using logging.path as a property in your application.properties. It should be accessible as LOG_PATH in logback than.
Also the usual way to use the base file is by adding the spring-boot-starter-logging in Maven/Gradle and include it like that:
<include resource="org/springframework/boot/logging/logback/base.xml"/>
I have a similar problem. I use defaultValue. To be honest it's just a smelly workaround.
<springProperty name="configurable.canonical.name" source="canonical.name" defaultValue="${canonical_name}" />
<file>logs/${configurable.canonical.name}.log</file>
canonical_name is defined in default.properties. Maven is going to resolve it during build.

Spring Boot logging : use of system variables in log4j2.xml

I am running spring boot application as jar.
java -Dlogs.location=<path/to/my/logs> -jar my-app.jar
or
java -Dlogs.location=<path/to/my/logs> -jar my-app.jar --logs.location=<path/to/my/logs>
Here is a sample log4j2.xml configuration file
<?xml version="1.0" encoding="UTF-8"?>
<Configuration >
<Properties>
<Property name="base.log.dir">${sys:logs.location}</Property>
</Properties>
....
</Configuration>
Spring boot app is creating ${sys:logs.location} folder instead of correctly resolving system properties from jvm args.
Same configuration file working fine with Spring application.
I am unable to make logs.location configurable with my custom log4j2.xml file. Any help or suggestion is appreciated.
Please refer this sample project on github
I am using log4j2-spring.xml to configure log4j2.
I have looked at the StackOverflow q's. This answer reads properties bundle. But I want to read sys properties
Define a property like
<Properties>
<Property name="filePathVar"> ${sys:filepath:-/logs/app.log} </Property>
</Properties>
and use filePathVar like "${filePathVar}" in your xml file
and refer this for runtime args - https://stackoverflow.com/a/37439625/5055762
Note - /logs/app.log will be the default value if none is passed as a runtime arg

External log with spring boot from an application.yml

I would like have an external log of my application, but I haven't managed to do it. I have the logaback.xml in resources folder:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="org.springframework.web" level="DEBUG"/>
</configuration>
And I have an application.yml with this content:
# ========================
# LOGGING
# ========================
#logging:
file: /tmp/application.log
# Enable this to specify the path and the name of the log file. By default, it creates a
# file named spring.log in the temp directory.
In the folder tmp the only one file is spring.log, with all logs from the application, but I need a file with other name and with a level debug in a different folder.
Anybody can help me?
Thank you!

Resources