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>
Related
The purpose of a custom Spring Boot starter is to provide a default Logback XML configuration, e.g.:
<?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/console-appender.xml"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml"/>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
</configuration>
It also provides a default logging.pattern.level defined in the starter's properties loaded by EnvironmentPostProcessor. For instance, let's assume it appends spring.application.name to the logging level:
logging.pattern.level=%5p [${spring.application.name}]
The problems that occur:
Property spring.application.name is not recognized:
2023-02-07 09:44:00.704 INFO [spring.application.name_IS_UNDEFINED] 70495 ---...
The default logging.pattern.level cannot be overridden in the application properties. For example, if the application.properties contains the property:
logging.pattern.level=%5p stackoverflow
#Bean
public String test(#Value("${logging.pattern.level}") String loggingPattern) {
Logger logger = Logger.getLogger("LogDemo");
logger.info(loggingPattern);
return loggingPattern;
}
2023-02-07 09:57:50.124 INFO [spring.application.name_IS_UNDEFINED] 70880 --- [ main] LogDemo : %5p stackoverflow
If logback.xml is not defined in the custom starter the spring.application.name is recognized successfully. If logging.pattern.level is moved from the starter's properties to the application.properties it also works.
It seems like logging is being configured right after loading the starter's properties but before loading the application properties, in case the starter's properties contain the logging.pattern.level.
Is there a way to define a default logging configuration without the problems listed above?
I managed to define a default logging configuration in a custom starter using Spring Boot's extensions to Logback, without additional properties and EnvironmentPostProcessor.
Because the standard logback.xml configuration file is loaded too
early, you cannot use extensions in it. You need to either use
logback-spring.xml or define a logging.config property.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<springProperty scope="context" name="SPRING_APPLICATION_NAME" source="spring.application.name"/>
<springProperty scope="context" name="SPRING_LOG_LEVEL_PATTERN" source="logging.pattern.level"/>
<property name="DEFAULT_LOG_LEVEL_PATTERN"
value="%5p [${SPRING_APPLICATION_NAME}]"/>
<property name="LOG_LEVEL_PATTERN"
value="${SPRING_LOG_LEVEL_PATTERN:-${DEFAULT_LOG_LEVEL_PATTERN}}"/>
<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/console-appender.xml"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml"/>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
</configuration>
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.
As per this guidelines, one has to specify application name in bootstrap.properties if using custom logback.xml. I was wondering if there's any way where I can hardcode the application name in logback.xml instead of creating the bootstrap.properties file with that property ?
I have bootstrap.properties with property spring.application.name and slueth recognizes that and things are fine. But I was wondering if there's any way where I can specify any logback property and sleuth will pick the application name up!?
Please note I am using my own custom logging format. Following is how my logback.xml file looks like.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="CONSOLE_LOG_PATTERN" value="%date{ISO8601}
${LOG_LEVEL_PATTERN:-%5p} ${PID:- } [%15.15t] %-40.40logger{39} :
%m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>
</configuration>
The <springProperty> tag lets you expose properties from the Spring Environment for use within Logback.
i.e.
<springProperty scope="context" name="fluentHost" source="myapp.fluentd.host" defaultValue="localhost"/>
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.
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.