External log with spring boot from an application.yml - spring-boot

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!

Related

Spring boot: disable logger

This is my application.properties under _/config folder:
logging.file=logging.xml
This is the content of _/config folder:
$ tree config
config
├── application-bo.properties
├── application-loc.properties
├── application-pre.properties
├── application.properties
├── application-pro.properties
└── logging.xml
The content of logging.xml is:
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<!-- LOG "com.baeldung*" at TRACE level -->
<logger name="net.gencat.transversal.espaidoc.common.dao.RedisDao" level="OFF" additivity="false" />
</configuration>
As you can see, I'm trying to disable logs generated inside from net.gencat.transversal.espaidoc.common.dao.RedisDao logger.
However, I'm getting log messages on console yet.
Any ideas?
What you are configuring only applies for file appender.
Have you tried defining the console appender and just add your specific configuration for the console?
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<logger name="net.gencat.transversal.espaidoc.common.dao.RedisDao" level="OFF" additivity="false">
<appender-ref ref="console" />
</logger>
In general in spring boot you can edit the application.properties (or yaml) file and define series of definitions for logging, including levels.
This means however that you don't really need to create logging.xml file - spring boot will configure logging only out of the definitions found in the application.properties/yaml file:
logging.level.net.gencat.transversal.espaidoc.common.dao.RedisDao=OFF
One note about logging.file which is just wrong:
If you're configuring loggging via properties/yaml file - you can use this property to specify the output file name where the logs will be written (its like configuring file appender directly in older applications).
If you really want to maintain the XML file, its possible, them you can create: src/main/resources/logback-spring.xml file and put the definitions there.
Again in this case you don't need any properties at the level of application.properties (including logging.file property).
Here is an example of such an approach:
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds">
<logger name="net.gencat.transversal.espaidoc.common.dao.RedisDao" level="OFF"/>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<jmxConfigurator/>
</configuration>

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>

Spring Boot and Logging with External Tomcat only outputs startup log

My Spring boot 2 app is not showing any log message when is running. I can only see the startup log. This app is deployed as WAR in the production server and I configured the log to output to a file:
logging.file = app.log
logging.level.root=WARN
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR
logging.pattern.console= %d{yyyy-MM-dd HH:mm:ss} - %msg%n
In my local I can see whatever debug message I include in my code but in the server I can't. I only see the application startup trace.
My config to generate the file is the provided by official guideance. And the tomcat dependency in the app.war:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
Do you have any idea of what is happening? It is strange. The log file is generating in the server ( we deploy it in a docker container ) but after the app is running, no more log is output to the file.
By default Spring Boot logs on INFO level, which should include ERROR.
As per Spring boot logging guide 79.1.1 Configure Logback for File-only Output
If you want to disable console logging and write output only to a file, you need a custom logback-spring.xml that imports file-appender.xml but not console-appender.xml, as shown in the following example:
<?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 which you have alraedy added.

logback.xml change configuration at runtime

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.

Resources