Spring boot: disable logger - spring

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>

Related

Is it compatible rsyslog with Logback-SLF4J?

I'm using spring-boot-starter-web like framework. These, use Logback (without implement other libraries) to manage logs with SLF4J like a facade. I need to use it with rsyslog but the official doc refers only to Syslog.
I tried to use the Syslog implementation since Syslog inherit for rsyslog but not found ( i attach my logback-spring.xml below ).
Any idea?
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
<property resource="application.properties" />
<!-- Syslog , make sure that syslog are enabled in the OS-->
<appender name="RSYSLOG" class="ch.qos.logback.classic.net.SyslogAppender">
<syslogHost>127.0.0.1</syslogHost>
<facility>LOCAL0</facility>
<port>514</port>
<throwableExcluded>true</throwableExcluded>
<suffixPattern>%package.yes.rest %m thread:%t priority:%p category:%c
exception:%exception:%msg</suffixPattern>
</appender>
<logger name="package.yes.rest" level="info" additivity="false">
<appender-ref ref="RSYSLOG"/>
</logger>
</configuration>
Bonus clip: I see the choose of change Logback to Log4j2 too, but it's more stable use the inherit Logback.

How to set max number of archived logs in spring boot

I tried following settings in application.properties:
logging.file=foo/bar.log
logging.file.max-history=2
logging.file.max-size=1KB
Still, its not limiting the number of archive logs to 2.
As per application properties documentation reference, only supported when you setup logback.
logging.file.max-history=0 # Maximum of archive log files to keep. Only supported with the default logback setup.
So to add support of logback please see section 79.1 Configure Logback for Logging & 79.1.1 Configure Logback for File-only Output of Spring Boot Logging Guide
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>

Spring - Exceptions do not log to a file

I'm currently using SLF4J API for logging.
Whenever an exception is thrown during runtime, the full error stack trace does not log to file, it is only printed to the console. I'm using eclipse.
Here is my code for logback.xml (currently located in classes folder under WEB-INF)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>
<!-- Specify here the path of the folder you want to save your logs -->
<property name="LOGFILE_PATH" value="C:/Logs" />
<!-- All logging will be redirected/ printed to console. -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{yyyy-MM-dd hh:mm:ss a} [%thread] %-5level %logger{50} - %rEx %msg%n </Pattern>
</layout>
</appender>
<!-- Send log to file -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOGFILE_PATH}/spring-mybatis-log.log</File>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{yyyy-MM-dd hh:mm:ss a} [%thread] %-5level %logger - %rEx %msg%n</pattern>
</layout>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOGFILE_PATH}/spring-mybatis-log-%d{yyyy-MM-dd}-%i.txt
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>2MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
Is there something missing/wrong with the above file??
Is it possible to log (to a file) all the text that will be printed to console?
How does spring(or the project itself) read the logback.xml file? What if I rename it and place it in another folder?
How to create one root containing all the levels (INFO, DEBUG, ERROR, WARN, etc..) ?
To answer your questions:
Nothing looks obviously wrong to me in the file you've posted, although I didn't try actually running it.
Just the way you did it is fine, with two appenders, one that goes to the file and the other to go to the console.
Logback by default looks in the classpath for the logback.xml file. Refer to the configuration page of the manual for the details. The way it gets there depends on your build system. When using Maven, I'd suggest putting it in src/main/resources. But if it ends up in WEB-INF/classes when deployed in your web app, that should work. If no matter what you put in your logback.xml file you are only getting console output (try adding a syntax error or renaming the file to test), that's what I'd look at first, to ensure that Logback is picking up the file right. It will default to showing everything just on the console if it can't find the file, though I think it shows a warning at the beginning that it's doing so. If it is picking up the file, you can try putting debug="true" in the <configuration> element, to see if there's an error that it's picking up that's causing it to not use the appender the way you're expecting.
Specifying the "DEBUG" level of logging, as you've done, will also get all higher levels as well.
If the issue is that logging from Spring isn't going where you want, while your application's logging is working fine, you may need to redirect Spring (which uses Apache Commons Logging) to use SLF4J instead. To do that, remove the commons-logging dependency and add the jcl-over-slf4j library. This will emulate the commons-logging calls and have them point to SLF4J instead. See the "Using SLF4J" section of this blog post for more details.
Your configuration looks OK, but you can try use your root level INFO instead DEBUG and if you have frameworks like spring, hibernate etc. Logback allow you uses another levels to them something like:
<logger name="org.hibernate" level="OFF" />
<logger name="org.springframework" level="INFO" />
<logger name="org.springframework.web.servlet.mvc" level="INFO" />

SpringBoot with LogBack creating LOG_PATH_IS_UNDEFINED folder

I am using SpringBoot with LogBack and using the below configuration in my yml file:
logging:
path: C:/var/log/pincode
The logging.path Spring Environment Variable is transferred to the LOG_PATH Environment variable and the log file is placed at the correct place, but there is also a directory called LOG_PATH_IS_UNDEFINED created in the root directory of my project.
This seems to be caused by the different phase used by SpringBoot to configure LogBack with its Environment variables.
17:29:21,325 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
17:29:21,337 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use the pattern LOG_PATH_IS_UNDEFINED/catalina.out.%d{yyyy-MM-dd} for the active file
17:29:21,340 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd' from file name pattern 'LOG_PATH_IS_UNDEFINED/catalina.out.%d{yyyy-MM-dd}'.
17:29:21,340 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
17:29:21,343 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Mon Aug 11 17:24:07 BRT 2014
17:29:21,346 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[serverConsole] - Active log file name: LOG_PATH_IS_UNDEFINED/catalina.out
17:29:21,346 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[serverConsole] - File property is set to [LOG_PATH_IS_UNDEFINED/catalina.out]
...
17:29:21,358 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
And then after that it start configuring logback again but this time using the path i set:
17:29:21,672 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
17:29:21,673 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - No compression will be used
17:29:21,673 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use the pattern C:/var/log/pincode//catalina.out.%d{yyyy-MM-dd} for the active file
17:29:21,674 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd' from file name pattern 'C:/var/log/pincode//catalina.out.%d{yyyy-MM-dd}'.
17:29:21,674 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
17:29:21,674 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Mon Aug 11 17:29:21 BRT 2014
17:29:21,674 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[serverConsole] - Active log file name: C:/var/log/pincode//catalina.out
17:29:21,674 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[serverConsole] - File property is set to [C:/var/log/pincode//catalina.out]
...
17:29:21,685 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
My logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<include resource="org/springframework/boot/logging/logback/basic.xml" />
<property name="FILE_LOG_PATTERN"
value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID:- } [%t] --- %-40.40logger{39} : %m%n%wex" />
<appender name="serverConsole"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<Append>true</Append>
<File>${LOG_PATH}/catalina.out</File>
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/catalina.out.%d{yyyy-MM-dd}
</fileNamePattern>
<maxHistory>15</maxHistory>
</rollingPolicy>
</appender>
<!-- Plain Text Rolling Appender -->
<appender name="server"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<Append>true</Append>
<File>${LOG_PATH}/pincode.log</File>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/pincode.log.%d{yyyy-MM-dd}
</fileNamePattern>
<maxHistory>15</maxHistory>
</rollingPolicy>
</appender>
<!-- Plain Text Rolling Appender -->
<appender name="server-error"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<Append>true</Append>
<File>${LOG_PATH}/pincode-error.log</File>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/pincode-error.log.%d{yyyy-MM-dd}
</fileNamePattern>
<maxHistory>15</maxHistory>
</rollingPolicy>
</appender>
<logger name="com.app" level="INFO">
<appender-ref ref="server" />
<appender-ref ref="server-error" />
</logger>
<root level="INFO">
<appender-ref ref="serverConsole" />
</root>
If I remove my logback.xml file from the project it doesn't create the folder, so somewhere Spring is loading the xml before parsing the yml?
How can I avoid Logback to create this LOG_PATH_IS_UNDEFINED directory?
In your case LOG_PATH is not defined on startup. You should use ${LOG_PATH:-.} instead , See .
But if you define logging.path in your application.properties you will see two log files in . and in ${logging.path} directory.
Spring container set LOG_PATH after Logback initialization... Logback is not supported lazy file creation as far as I know. In this case you should use logback-spring.xml instead logback.xml.
I faced similar issue and its easy to solve it. Basically , concept is that Spring Boot already gives you System property - LOG_PATH for Spring Boot property - logging.path so you define logging.path in your application.properties and simply use LOG_PATH in your logback configuration - logback-spring.xml.
You shouldn't declare logback <property ...> for LOG_PATH , just use it whenever you want.
See at near bottom here
I encountered the same problem.
put an entry in logback.xml
<property resource="application.properties" />
In application.properties
FILE_LOG_PATTERN=###
LOG_FILE=###
when your application starts,the name of the directory created is what you have defined in the properties file.
Might not be your case but if you have bootstrap.properties make sure logging.path is defined there and only there.
if you're on Spring Boot Finchley (2.x), you can define spring.application.name in your application.properties or application.yml file and add the following in your Logback configuration:
<configuration>
<springProperty scope="context" name="springAppName" source="spring.application.name"/>
</configuration>
you will now have ${springAppName} as a variable at your disposal for your log pattern.
Before Spring Boot enviroment is prepared, the Spring Boot main class or the SpringApplication will initialize the loggerfactory, which will detect the default configuration file (logback.groovy, logback.xml, logback-test.xml), but at this time the Spring Boot application is not started yet, which means the variable LOG_PATH is not set. So at first you should alter the name of your logback config file and configure the file name manually in the Spring Boot config as logging.config. By this way the logback will configure a console appender by default instead of creating a file appender, and then when the Spring Boot enviroment is ready it will fire an enviromentready event, which causes a logback reconfig by LoggingApplicationListener. You can find the issue at springboot's page https://github.com/spring-projects/spring-boot/issues/2558
If you upgrade your Spring Boot version into pom.xml, make sure that your replaced
logging.path = your/log/path
by
logging.file.path = your/log/path
into application.properties. That was my case.
I encountered the same problem. I tried to define my own logback.xml and had trouble using the logging.path and logging.file properties defined in my application.properties file. Here is how I resolved (and worked around) the issues.
First, I learned that you cannot define both logging.path and logging.file properties. Since I'm using a RollingFileAppender that will produce multiple files over multiple days, I define logging.file, but use it more like a file prefix.
In application.properties
# Don't add the file type at the end. This will be added in logback.xml
logging.file=logs/my-app-name
In src/main/resources/logback.xml
<configuration>
<property name="FILE_LOG_PATTERN" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<Pattern>${FILE_LOG_PATTERN}</Pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<Pattern>${FILE_LOG_PATTERN}</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
This seems to work for the most part. I defined my own FILE_LOG_PATTERN in the file, which I think is optional. The interesting part is the fileNamePattern. It correctly translates logging.file from my application.properties file into the variable LOG_FILE. The only real ugliness here is that on startup Logback still complains about the log file being undefined and creates an empty file called LOG_FILE_IS_UNDEFINED_XXX in the current directory. But the actual log file in my property is created and correctly appended to.
Andrew
somewhere Spring is loading the xml before parsing the yml
so just rename logback.xml to your-logback.xml and add logging.config=classpath:your-logback.xml in your application.properties
Do below to create dev/log directory only. Do not add log.path in application.properties
Add log.path=dev/logs in your bootstrap.properties.
Add below line in your logback-spring.xml.
<springProperty scope="context" name="LOG_PATH" source="log.path"/>
<property name="LOG_FILE" value="${LOG_PATH}/app/current"/>
Note
Make sure you include the below line only.
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
Do not use the below line else console logs will never be disabled and spring.log file will be created in temp directory(if you dont not provide logging.path in application.properties). The check the code of below file to know more.
<include resource="org/springframework/boot/logging/logback/base.xml"/>
put an entry in logback:
<property name="DEV_HOME" value="c:/application_logs/ps-web" />
and reference it:
<fileNamePattern>${DEV_HOME}.%d{yyyy-MM-dd}.log</fileNamePattern>
I had the same problem since I configured logging.path and logging.file on application.properties but some logs was produced before Spring Boot LogBack configuration and so they were written into LOG_PATH_IS_UNDEFINED/LOG_FILE_IS_UNDEFINED file and then the logs switched to the right location.
I found 2 possible solutions:
1) Configure logging.path and logging.file in bootstrap.properties since the configuration in bootstrap take place before
or
2) Set logging.path and logging.file as system properties with -Dlogging.path=... -Dlogging.file=... when launching the application
Based on Spring Boot common properties,
add the following into your application.yml
logging:
file:
path: logs/dev
if using application.properties, it should be
logging.file.path = logs/dev
Declare the property LOG_PATH in your logback.xml
<property name="LOG_PATH" value="" />
is where you must specify the directory where the log files are created. If this field is left empty, logback will create a new directory in the program execution. The name of the directory created is LOG_PATH_IS_UNDEFINED
Try adding the following to your POM file
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<filesets>
<fileset>
<directory>${basedir}/catalina.base_IS_UNDEFINED</directory>
<includes>
<include>**/*.log</include>
</includes>
<followSymlinks>false</followSymlinks>
</fileset>
</filesets>
</configuration>
</plugin>
To call an logback form an external path in a .yml file, it worked for me as:
logging:
config: C:/folder/logback.xml
I suppose you have included an error file.
Please change
<include resource="org/springframework/boot/logging/logback/basic.xml" />
to
<include resource="org/springframework/boot/logging/logback/base.xml"/>
then have a try.
version: 1.5.9
springcloud:
bootstrap.yml
spring:
application:
name: awesome-app
# profile:
# active: dev
logging:
path: /code/awesome-app
spring-logback.xml
${LOG_PATH}/awesome-app.log
springboot:
application.yml
spring:
application:
name: awesome-app
# profile:
# active: dev
logging:
path: /code/awesome-app
spring-logback.xml
${LOG_PATH}/awesome-app.log
custom-log-configuration:https://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/htmlsingle/#boot-features-custom-log-configuration
I was also having similar issue. I resolved it by renaming logback-spring.xml to logback-whatever.xml and added below in application.properties:
logging.config=classpath:logback-whatever.xml
Also, this issue comes when we use user defined properties for logging purposes such as:
log.path=logs
log.archive.path=archived
What worked for me:
having logback-spring.xml under resources folder (the same as yamls)
setting the environment variable in the application.yaml
after modifications, do a ./gradlew clean build
Happy new year everybody!
So, I've updated my spring boot to version (2.4.1) and I started getting the LOG_PATH_IS_UNDEFINED error.
I did a bit of research and I'm guessing there is a problem with the mapping of the properties from logging.path to LOG_PATH. (I manually debugged the logger and the properties were being load)
My solution/Patch:
I added a manual mapping to the logback-spring.xml at the very top:
<springProperty scope="context" name="LOG_PATH" source="logging.path"/>
Now it is working for me...

Spring and Log4j ver 2 - XML configuration example

I am trying to configure Log4j 2 in Spring XML configuration file for the first time (but unsuccessfully). I need to create two appenders - one for logging into console (>=DEBUG) and another for logging into database via JDBCAppender (>= INFO).
There is a problem because I don't know how to set another log level logger that differs from root logger.
Thank you for sharing some XML configuration sample. Thanks in advance!
You can set the level on the appender ref.
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
<appenders>
<appender name="A">
...
</appender>
<appender name="B">
...
</appender>
</appenders>
<loggers>
<root level="trace">
<appender-ref ref="A" level="info"/>
<appender-ref ref="B" level="debug"/>
</root>
</loggers>
</configuration>

Resources