I am using log4j2 in my project and I have externalized the application.properties and log4j2.xml and I want to provide log4j2.xml file path in application.properties
log4j2.xml and application.properties are in the config folder in the same dir as a jar.
Here is my dependancy
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
Directory Structure :
/tmp/myproj/conf/application.properties
/tmp/myproj/conf/log4j2.xml
/tmp/myproj/my.jar
Any help would be appreciated.
Spring Boot expects the log4j2-spring.xml configuration file to be on the classpath. However, you can store it in a different location and point to it using the logging.config property in application.properties.
Try providing below configuration to you application.properties. Here log4j2-spring.xml is on project classpath. If not then try giving your full path as (suppose) "C:/tmp/myproj/conf/log4j2.xml".
**logging.config=classpath:log4j2-spring.xml**
logging.level.org.springframework.web=INFO
logging.file=logs/spring-boot-logging.log
The configuaration in bold should work for you.
Then you need to configure your log4j2-spring.xml as per your requirement.
(simple and also for appenders, see here https://howtodoinjava.com/log4j2/log4j-2-xml-configuration-example/ ).
Note: I have used log4j2-spring.xml in place of you log4j2.xml. you can replace at your will.
Related
Having a standard configuration for log4j2 and spring property-file on classpath application.property.
log4j2.xml
<Properties>
...
<Property name="APP_LOG_ROOT">${bundle:application:log.root.dir}</Property>
...
</Properties>
application.properties
...
log.root.dir=/opt/tomcat/logs
...
The data is read into the log4j2.xml correctly, but what if I want to get an alternative property when creating an artifact with maven and put diferent application.property:
mvn clean install -Dapplication.properties.path=file:/some_path/application.properties
?
After that, I can correctly read the new properties.
#Value("${log.root.dir}")
private String ololo;
but the log4j2 cannot do this on its own.
If you want to use any value from Spring's Environment in a Log4j2 configuration file, you need to use the Spring Boot Lookup.
After adding log4j-spring-boot to your classpath, you just need to replace
${bundle:application:log.root.dir}
with
${spring:log.root.dir}
I have a hsqldb file in my project resources folrder "src/main/resources/mydb.data"
In my application.properites I need to set the path with spring.datasource.url=
How I can achieve that?
spring.datasource.url=.... ?
One solution would be to use Maven resource filtering. Just use something like this in your application.properties:
spring.datasource.url=jdbc:hsqldb:file:#basedir#/src/main/resources/mydb.data
Instead of using basedir you can define something like darasource-url in your pom.xml and use this property in your application.properties:
pom.xml
<project>
<!-- ... -->
<properties>
<datasource-url>jdbc:hsqldb:file:${basedir}/src/main/resources/mydb.data</datasource-url>
</properties>
<!-- ... -->
</project>
application.properties:
spring.datasource.url=#datasource-url#
Notice: Using the spring-boot-starter-parent as parent, you have to use #..# instead of ${..}, see 2.1.1. Automatic Property Expansion Using Maven in the Spring Boot How-to-Guides.
I use spring-boot and log4j2, how can my application not read log4j2.xml in resource folder. Read my custom location for the log4j2.xml file?
In my Spring Boot 1.5.1 application I have added Elasticsearch Maven dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
During application startup ES created {project.dir}/data/elasticsearch folder.
Please show how to reconfigure ES to create this folder at {project.dir}/target/data
I fond the solution, place the following properties into application.properties file:
#Elasticsearch
spring.data.elasticsearch.properties.path.data=target/data
spring.data.elasticsearch.properties.path.logs=target/logs
What if you configure your elasticsearch.yml by uncommenting the path.data line and changing it to the respective directory:
path.data: target/data
I have log4j.properties file under resources folder. Now I am passing logging.file=/Users/jkuriakos/web/rnt/dr/rnt.log in application.properties file.
How can I use the log4j.properties file to load all information (like smtp server etc) from log4j.properties file?
Exclude the spring-boot-starter-logging from your pom file and add following dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>
This should make spring boot pick up log4j.properties from resources folder.
http://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html#howto-configure-log4j-for-logging