logback: FileNamePattern from application.properties - spring-boot

I would like to get the file name of my logback file from a property defined in the
application.properties
but I don't know if it is possible
<fileNamePattern>${user.home}/logs/>${logFileName}.%i.log.zip</fileNamePattern>

You can register your application.properties file in the logback.xml configuration file. logback will then recognize the properties defined in that file.
logback.xml:
<property resource="application.properties"/>
More details: https://logback.qos.ch/manual/configuration.html#definingProps

Related

Logback destination configuration from application.yml

I'm trying to configure my logback in XML import destination configuration from application.yml.
When I set static destination in XML then everything is fine but if not then I'm getting this exception on startup:
Could not invoke method addDestination in class net.logstash.logback.appender.LogstashTcpSocketAppender with parameter of type java.lang.String java.lang.reflect.InvocationTargetException
ERROR in net.logstash.logback.appender.LogstashTcpSocketAppender[STASH] - No destination was configured. Use <destination> to add one or more destinations to the appender
My dependencies:
spring-boot: '1.5.4.RELEASE'
net.logstash.logback:logstash-logback-encoder:4.9
logback-spring.xml
<springProperty name="LOGBACK_URL" source="logback.destination.url"/>
<springProperty name="LOGBACK_PORT" source="logback.destination.port"/>
<appender name="STASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>${LOGBACK_URL}:${LOGBACK_PORT}</destination>
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">...
</encoder>
</appender>
applicaiton.yml
logback:
destination:
url: kibana.test
port: 1234
I'm trying to avoid migrating this config to java and hope it's unnecessary. Thanks in advance !
EDIT
I've resolved this problem by adding default value which is space in xml configuration like below:
<destination>${LOGBACK_URL:- }:${LOGBACK_PORT:- }</destination>
just leaving it here for anyone facing similar issue, in my case, I had to remove the http:// from the url inside destination tag.

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.

including spring properties in logback pattern

I would like to include some spring properties in my logback.xml pattern, but until I know it is not possible due to application.properties is loaded after logback.xml
There is a way to include a property from application.properties or from project pom.xml?
This is my logback code:
%d{"yyyy-MM-dd HH:mm:ss,SSSZ"} [%p] %c ${project.artifactId} - %msg%n
There is a dedicated property called springProperty which you can use in your Logback configuration file.
<configuration>
<springProperty name="artifactId" source="project.artifactId"/>
...
<fileNamePattern> ... ${artifactId} ... </fileNamePattern>
...
</configuration>
The source attribute should match the key in your application.properties. The name attribute is used to refer the value inside the 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 application.properties

I'm developing a Spring Boot application and I used application.properties to configure db connections ,server port etc..
# ===============================
# = SERVER CONFIGURATION
# ===============================
server.port=8173
# ===============================
# = DATABASE CONFIGURATION
# ===============================
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springBootApps
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
# ===============================
# = SPRING CONFIG
# ===============================
server.error.whitelabel.enabled = false
spring.view.prefix =/WEB-INF/jsp/
spring.view.suffix = .jsp
Can I use an application.properties file instead of spring-configuration.xml or do I need to use both configurations inside my project?
Can I write all my Spring configuration in the application.properties file?
(in previous spring versions I did this using springConfiguration file)
As a example how can I implement the following XML configuration in application.properties
<bean id="daoImpl" class="com.mycompany.loginapp.dao.UserDaoImpl"/>
<bean id="data" class="org.springframework.jdbc.core.JdbcTemplate" >
<property name="dataSource" ref="dataSource" />
</bean>
Spring's application.properties are meant for externalizing your properties such as JNDI names, file system paths, etc. This property file is not meant to replace earlier XML based the bean definition and bean wiring.
For bean definitions, you can use either XML based bean definitions or Spring annotations (like #Autowired, #ComponentScan, etc) to get rid of XMLs.
You are correct, everything can be done within the application.properties.
Full list here docs.spring.io
In some cases if you are overriding Spring Boot's auto configuration capabilities the properties may not work.

Resources