Access spring profile in log4j2 for spring boot application - spring

How can i access the ${spring.profiles.active} value in log4j2 so that i can append the logfile name based on the profile?
log4j2.xml
<Properties>
<Property name="log-path">c:/logs</Property>
<Property name="targetEnv">${spring.profiles.active}</Property>
</Properties>

I tried with the following configuration and it worked
<Property name="targetEnv">${sys:spring.profiles.active}</Property>
This might help someone who is facing the same problem

Related

Spring Boot logging : use of system variables in log4j2.xml

I am running spring boot application as jar.
java -Dlogs.location=<path/to/my/logs> -jar my-app.jar
or
java -Dlogs.location=<path/to/my/logs> -jar my-app.jar --logs.location=<path/to/my/logs>
Here is a sample log4j2.xml configuration file
<?xml version="1.0" encoding="UTF-8"?>
<Configuration >
<Properties>
<Property name="base.log.dir">${sys:logs.location}</Property>
</Properties>
....
</Configuration>
Spring boot app is creating ${sys:logs.location} folder instead of correctly resolving system properties from jvm args.
Same configuration file working fine with Spring application.
I am unable to make logs.location configurable with my custom log4j2.xml file. Any help or suggestion is appreciated.
Please refer this sample project on github
I am using log4j2-spring.xml to configure log4j2.
I have looked at the StackOverflow q's. This answer reads properties bundle. But I want to read sys properties
Define a property like
<Properties>
<Property name="filePathVar"> ${sys:filepath:-/logs/app.log} </Property>
</Properties>
and use filePathVar like "${filePathVar}" in your xml file
and refer this for runtime args - https://stackoverflow.com/a/37439625/5055762
Note - /logs/app.log will be the default value if none is passed as a runtime arg

Setting Spring applicationContext properties from POM file

I have the following configurations in my applicationContext.xml file:
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:application.properties</value>
<value>classpath:database.properties</value>
</list>
</property>
</bean>
<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${smtp.host}" />
</bean>
with smtp.host being set in my POM file like so:
<build>
<defaultGoal>install</defaultGoal>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
...
</build>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<application.env>local</application.env>
<profile.scope>compile</profile.scope>
<skip.test>true</skip.test>
<smtp.host>my.smtp.server</smtp.host>
</properties>
</profile>
Upon deploying my application, I ran into an error message saying that Spring was not able to resolve smtp.host. I added the following mapping to my application.properties file:
smtp.host=${smtp.host}
But Spring started to complain that I had a circular placeholder reference on the property. Is there anything I am missing?
Thanks!
You mix here two things up. The pom.xml is for building the application. Properties you define there has normally nothing to do with your application properties. And Maven profiles has nothing to do with Spring profiles. They are only named equaly.
You should configure your Spring Application as you can read here. You could -- what I would not suggest -- use your pom as property source.
The normal way would be to read it from a externalized configuration. As I do not know if you use Spring Boot, you can have a look at the Spring Boot Way and adapt it, if you use Spring without Boot.
So add a apllication.properties file into src/mein/resources like
smtp.host=my.smtp.server
If you use boot, you are done, else you have to add a
#PropertySource("classpath:/application.properties")
to your #Configuration
Here you are mixing the build and runtime phase of application which are mutually exclusive.
Mavens' role end once the build is complete thus any properties used perishes with it. Moreover application start up is agnostic to the tool / process used to build it and thus there isn't any information shared between them. Thus the idea to use properties specified in pom.xml is not feasible.
Regarding the circular reference the statement smtp.host=${smtp.host} is loosely similar to java code int i = i; which essentially has no effect because i is defined and assigned to itself.

java.lang.ClassNotFoundException: freemarker.template.TemplateHashModelEx

My application is a servlet, spring 4 application where the spring jar is in the tomcat lib.
Freemarker jar is in WEB-INF/lib.
I am getting this error when starting the server
java.lang.ClassNotFoundException: freemarker.template.TemplateHashModelEx
This error disappears when I move the freemarker jar to tomcat lib directory.
It appears to me that spring 4 webmvc jar which contains Configurer class is unable to see WEB-INF/lib freemarker jar. I do not understand why freemarker jar in WEB-INF/lib folder is not visible to spring jar in tomcat lib folder.
What may I do to resolve this situation.
My spring configuration is as follows
<bean id="freemarkerConfiguration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
<!--Turn this off to always load via SpringTemplateLoader-->
<property name="preferFileSystemAccess" value="false"></property>
<property name="templateLoaderPath" value="classpath:/"></property>
</bean>
<bean id="freemarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="configuration" ref="freemarkerConfiguration" />
</bean>
add the following code in pom.xml
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker-gae</artifactId>
<version>2.3.18</version>
</dependency>
This works for me.
I've had similar issues before and I've solved it.
Environment:Idea 、 Maven
Open File->Project Structure->Artifacts
Right click on the Project Library in the "Available Elements" to put in output root(WEB-INF/lib).
Restart tomcat and you'll see the problem was solved.

Spring Security fails on WebXmlMappableAttributesRetriever

I have a Spring-Web-App that fails when I try to deploy it to my local tomcat since I've added Spring-security.
I used the securty-web-auth with a preauth filter. The RolesRetriever fails with fallowing Error-Message
class path resource [WEB-INF/web.xml] cannot be opened because it does not exist
But the web.xml exists and worked before the security setup.
<bean id="j2eeMappableRolesRetriever"
class="org.springframework.security.web.authentication.preauth.j2ee.WebXmlMappableAttributesRetriever">
<property name="resourceLoader" ref="webAppContext" />
</bean>
<bean id="webAppContext"
class="org.springframework.web.context.support.GenericWebApplicationContext" />
Has someone and idea, why it fails? It must be within the WebAppContext-Bean but I have no Idea where to set the right path to web.xml

JPA 2.0 (logging and tracing through) with Glassfish 3.0.1 and NetBeans 6.9.1:

I am using JPA 2.0 (EclipseLink provider) with Glassfish v3.0.1 and NetBeans 6.9.1 and am NOT able to see the queries and other logging information from JPA 2.0. Essentially I want to be able to see all the SQL statements which are being generated by JPA and other related debugging information...
Has anyone successfully been able to configure the logging to provide such feedback? I've tried several things to no avail...
Any help would be greatly appreciated.
Thanks much.
What eventually had done the trick for me was using:
<property name="eclipselink.logging.logger"
value="org.eclipse.persistence.logging.DefaultSessionLog"/> in conjunction with your recommended tag of:
<property name="eclipselink.logging.level" value="FINE" /> This allowed me to see the relevant JPA logs which in NetBeans output window. This also worked in Eclipse. The output was sent do the console window an intermingled with the server's output which was exactly what I wanted.
You must configure logging level in persistence.xml file.
Example:
<persistence-unit name="MY_POOL_NAME" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>MY_JTA_SOURCE</jta-data-source>
<properties>
<property name="eclipselink.logging.level" value="FINE" />
<property name="eclipselink.target-server" value="SunAS9"/>
</properties>
</persistence-unit>
Log Levels:
OFF
SEVERE
WARNING
INFO
CONFIG - Use this for Production
FINE
FINER
FINEST
More info: http://wiki.eclipse.org/EclipseLink/Examples/JPA/Logging
All the queries would be printed in the domain server.log file.

Resources