Spring boot logging with log4.properties file in not working - spring

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

Related

External log4j2.xml file path in application.properties

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.

Unable to serve JSP in Spring Boot applications

I have tried several tutorials to serve JSP pages using Spring Boot. They all return a 404 page not found error.
To overcome the known limitations, I'm using a WAR packaging, with the following dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
I have defined the path where JSP pages are in application.properties:
spring.mvc.view.prefix= /WEB-INF/jsp/
spring.mvc.view.suffix= .jsp
When requesting a JSP page, the following WARN is displayed:
WARN 10251 --- [io-8080-exec-11] o.s.w.s.r.ResourceHttpRequestHandler : Path with "WEB-INF" or "META-INF": [WEB-INF/jsp/hello.jsp]
Have JSP been deprecated in Spring Boot 2? Do you have any Spring Boot 2 working example with JSP ?
can you please try adding scope in your dependency just like this
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
I'm using Intellij IDEA. I found out that we cannot just run the SpringBootApplication main class directly. We need to let the maven do the work.
Short solution: maven run you application by the command below from your module root directory
mvn clean package spring-boot:run
You can also add a run configuration using Maven so that you don't have to type the command every time.
Some said JSPs folder should be put under src/main/resources/META-INF/resources/WEB-INF/jsp. This indeed solves the spring boot application run problem though, it will fail when you run the application using tomcat. So we still need to keep the structure if we are going to deploy the application to Tomcat.
Unfortunately, I couldn't find any Spring Boot 2 example able to serve JSP pages as they all return a 404 error. As a workaround I have configured the application to be deployed on WildFly, as described in this tutorial and run my application with JSP on WildFly.
If you want example here it is.
This also help you.

how to choose which dependency resources will be used in spring boot application

I have fat-jar spring boot application in jar A.jar
in this fat-jar I have folder lib with two jar files (dependencies)
let's say B.jar and D.jar in each of these jar file (B and D) we have application.properties (resource) which is loaded automatically during initialization of application.
Unfortunately application.propertiesfrom D.jar is loaded before application.properties from B.jar
How to load application.properties from B.jar instead of application.properties from D.jar ?
Depends on location on classpath always first application.properties will be loaded. Order of jar files on classpath in my case (maven pom.xml) depends on order of registering dependencies in pom.xml
If I use
<dependencies>
<dependency>
//B.jar
</dependency>
<dependency>
//D.jar
</dependency>
</dependencies>
then application.properties from B.jar will be loaded.

Move Elasticsearch data folder to Maven target

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

Not Able to Resolve View Using Spring boot

I have put all jsp on classpath in views folder.
Folder structure is :
src/main/resources/
src/main/resources/static/
src/main/resources/static/views/
src/main/resources/static/views/*.jsp
src/main/resources/static/views/*.png
I am able to retrieve images file from static folder. But When It comes to jsp I am getting following error.
While hitting url:http://localhost:9001/login
There was an unexpected error (type=Not Found, status=404).
/views/login/login.jsp
However login.jsp exists on given location.
You can place your JSP files in "src/main/resources/META-INF/resources" folder location.
I have created a "WEB-INF/jsp" folder under "src/main/resources/META-INF/resources" and placed the JSP file there.It works. Check the screenshot.
https://i.stack.imgur.com/P3jUI.png
Alternatively, You can create "WEB-INF/jsp" folder under "src/main/webapp" and placed the JSP file there.But for this you have to package it as ".war" extension.
Springboot default tomcat container does not handle JSP. so we need to use below dependency to handle it :
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
And if you are using JSTL tags in JSP then add below jar as well :
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
A template such as a JSP is the exact opposite of static content such as an image file. Put your template in src/main/resources/templates.
Put them in folder :
src/main/resources/templates
2.These three dependencies could help you:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
I believe by default Spring Boot looks for html files. To change that behavior you have to change your application.properties as following to use jsp-s: spring.view.suffix: .jsp
Maybe even change spring.view.prefix as well to point to static resources.
I would personally suggest to use html-s with Thymeleaf though.
I was supposed to use war as packaging. Jar packaging does not work in case of jsp.

Resources