My web app is build with spring boot 2.1.2.RELEASE, i want to use context path as log dir, but get error when spring boot run.
ERROR Unable to create file ./logs/agilor/${web:contextPath}/logs.log
It look like web lookup is not work.
I have log4j-web in pom.xml
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.12.1</version>
</dependency>
Application is extend from Log4jServletContainerInitializer.
#SpringBootApplication
public class Application extends Log4jServletContainerInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Below is my log4j2.xml.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="LOG_DIR">./logs/agilor/${web:contextPath}</Property>
<Property name="PATTERN">[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{10} - %msg%n</Property>
</Properties>
<Appenders>
<RollingFile name="RollingFile" fileName="${LOG_DIR}/logs.log" filePattern="${LOG_DIR}/logs_%d{yyyy-MM-dd}.log">
...
The problem is that Log4j is expecting the the LoggerContext's externalContext field will contain the address of the ServletContext. Spring Boot uses that field as a flag to indicate that Logging has been initialized. This is a bug. I have created LOG4J2-2736 to fix it.
Also, Log4j 2.13.0 was just released. It will let you convert your property to
<Property name="LOG_DIR">./logs/agilor/${spring:spring.application.name}</Property>
if that is what you were trying to do. You would need to include the log4j-spring-cloud-config-client module to get the SpringLookup.
Related
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 a spring-boot application running with an embedded tomcat. We are using logback and slf4j for logging.
I am trying to figure ways to generate catalina.out and localhost.log files through spring-boot application. It looks like catalina.out is generated by the start-up script that initiates the tomcat container in a stand-alone mode, catalina.out file is not generated within spring-boot application that is using embedded tomcat.
How about localhost.log file? Does the same apply for localhost.log file?
Also how can I change the log levels for embedded tomcat through logback/slf4j binding in my spring-boot application.
Any advice?
You have to configure the tomcat container in spring boot manually like this
You have to create the bean of EmbeddedServletContainerFactory and configure the log in tomcat container , below are the sample code (I am not tested it , but it may be run).
The tomcat now search the logback-access.xml file in classpath automatically the configure the logging
For Spring boot version < 2.0.0
#SpringBootApplication
public class ABCApplication {
#Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
LogbackValve logbackValve = new LogbackValve();
logbackValve.setFilename("logback-access.xml");
tomcat.addContextValves(logbackValve);
return tomcat;
}
public static void main(String[] args) {
SpringApplication.run(ABCApplication.class, args);
}
}
The EmbeddedServletContainerFactory is replace by TomcatServletWebServerFactory is spring boot version 2.0.0 , so use required factory to configure the tomacat.
Now you can provide your logback-access.xml like this
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%h %l %u %t "%r" %s %b</pattern>
</encoder>
</appender>
<appender-ref ref="STDOUT" />
</configuration>
You can add the appender to xml to log the tomcat logging in file.
You have to look about LogbackValve
You have to add the following dependency for the LogbackValve
<dependency>
<groupId>net.rakugakibox.spring.boot</groupId>
<artifactId>logback-access-spring-boot-starter</artifactId>
</dependency>
Hope it may be help jagamot !
To define Embedded Tomcat Log Path add these line in application.properties
server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be absolute or relative to the Tomcat base dir.
server.tomcat.accesslog.enabled=false # Enable access log.
server.tomcat.accesslog.file-date-format=.yyyy-MM-dd # Date format to place in the log file name.
for configuring log levels for embedded tomcat through logback
use this link
https://dzone.com/articles/configuring-logback-with-spring-boot
1.add logging.config=classpath:logback.xml logging.path=${your log path}in your "application.properties".
create a "logback.xml" config file in your classpath(please search the contents of the configuration file by yourself).
You can already create a log file by the above two steps,If you launch your application via the "java -jar" command, you may also need the "-Djava.io.tmpdir=${your log path}"(same as "logging.path" config) parameter to specify the log storage path.
I'm attempting to deploy a Spring Boot (2.0.2) application on JBoss EAP 7.1 server.
The code that's causing the problem is:
import javax.validation.constraints.NotBlank;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
#Component
#Validated
public class AppProperties {
#NotBlank
private String name;
When the application is deployed on JBoss I get the following exception:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
16:44:25,861 ERROR [org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter] (ServerService Thread Pool -- 6 7)
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to bind properties under 'app' to com.example.security.config.AppProperties:
Property: app.contextpath
Value: /api
Origin: class path resource [application.yml]:5:18
Reason: HV000030: No validator could be found for constraint 'javax.validation.constraints.NotBlank' validating type 'java.lang.String'. Check configuration for 'name'
Action:
Update your application's configuration
I've tried adding the file jboss-deployment-structure.xml with the following contents to WEB-INF/classes:
<jboss-deployment-structure>
<ear-subdeployments-isolated>true</ear-subdeployments-isolated>
<deployment>
<exclude-subsystems>
<subsystem name="jaxrs"/>
</exclude-subsystems>
<exclusions>
<module name="javaee.api"/>
<module name="javax.validation.api"/>
<module name="javax.faces.api"/>
<module name="org.hibernate.validator"/>
</exclusions>
</deployment>
</jboss-deployment-structure>
But, no luck. What's the workaround? Thanks in advance.
Even though this question is a year old, I ran into the same issue and couldn't find a solution.
This, I know, will work for Spring Boot 2.1.x and JBoss 7.1, not sure about versions before that.
We obviously need to exclude org.hibernate.validator and javax.validation.api. What wasn't clear is that we also need to exclude the javax.faces.api (it has a transitive dependency on javax.validation.api). Excluding that javax.faces causes JBoss to fail on start up due to missing jsf libraries. We can then simply exclude the jsf subsystem.
<jboss-deployment-structure>
<deployment>
<exclude-subsystems>
<subsystem name="jsf" />
</exclude-subsystems>
<exclusions>
<module name="javax.validation.api" />
<module name="javax.faces.api" />
<module name="org.hibernate.validator" />
</exclusions>
</deployment>
</jboss-deployment-structure>
Assuming you don't need JSF from jboss, this should work.
According to this article the jboss-deployment-structure.xml shoudl be placed in 'the top level deployment, in META-INF (or WEB-INF for web deployments)'.
So with your current set-up the things configured in the xml are not applied, so if the xml is put in the right location it might work.
javax.validation.constraints.NotBlank is part of Bean Validation 2.0 and thus Java EE 8. I suspect EAP 7.1 does not support this feature yet.
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
Using Spring 3.1.2, JUnit 4.10.0, and pretty new to both versions. I'm having the problem that I can't get the annotation-based autowiring to work.
Below are two samples, the one not using annotations, which is working fine. And the second one using annotation, which doesn't work, and I don't find the reason. I followed the samples of spring-mvc-test pretty much.
Working:
package com.company.web.api;
// imports
public class ApiTests {
#Test
public void testApiGetUserById() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("/com/company/web/api/ApiTests-context.xml");
UserManagementService userManagementService = (UserManagementService) ctx.getBean("userManagementService");
ApiUserManagementController apiUserManagementController = new ApiUserManagementController(userManagementService);
MockMvc mockMvc = standaloneSetup(apiUserManagementController).build();
// The actual test
mockMvc.perform(get("/api/user/0").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
}
}
Failing, because userManagementService is null, not getting autowired:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration // should default to ApiTests-context.xml in same package
public class ApiTests {
#Autowired
UserManagementService userManagementService;
private MockMvc mockMvc;
#Before
public void setup(){
// SetUp never gets called?!
}
#Test
public void testGetUserById() throws Exception {
// !!! at this point, userManagementService is still null - why? !!!
ApiUserManagementController apiUserManagementController
= new ApiUserManagementController(userManagementService);
mockMvc = standaloneSetup(apiUserManagementController).build();
// The actual test
mockMvc.perform(get("/api/user/0").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
}
}
Note that both test classes above should be using the same context configuration, and the userManagementService is defined in there.
ApiTests-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="user"/>
<property name="password" value="passwd"/>
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
p:dataSource-ref="dataSource" p:mappingResources="company.hbm.xml">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
</props>
</property>
<property name="eventListeners">
<map>
<entry key="merge">
<bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
</entry>
</map>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory"/>
<!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= -->
<context:annotation-config/>
<tx:annotation-driven/>
<context:mbean-export/>
<!-- tried both this and context:component-scan -->
<!--<bean id="userManagementService" class="com.company.web.hibernate.UserManagementServiceImpl"/>-->
<context:component-scan base-package="com.company"/>
<!-- Hibernate's JMX statistics service -->
<bean name="application:type=HibernateStatistics" class="org.hibernate.jmx.StatisticsService" autowire="byName"/>
</beans>
and the UserManagementService (interface) as well as UserManagementServiceImpl has the #Service annotation.
Two minor questions/observations: setup() never gets called, even though it has the #Before annotation. Furthermore, I noticed that my test methods don't get executed/recognized if they don't start with the name 'test', which is not the case though with all spring-mvc-test samples I saw.
pom.xml:
<dependency>
<groupId>org.junit</groupId>
<artifactId>com.springsource.org.junit</artifactId>
<version>4.10.0</version>
<scope>test</scope>
</dependency>
Update:
The problem only occurs when I run the tests from maven; it's ok when I run the test from within my IDE (IntelliJ IDEA).
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.3</version>
<configuration>
<includes>
<include>**/*Tests.java</include>
</includes>
</configuration>
</plugin>
Autowiring wont happen unless you do a component scan.
Why have you commented it out in your code ?
<!--<context:component-scan base-package="com.company"/>-->
Also re:junit. If you're in eclipse can you just go to the dependency tree view of the pom and filter on junit. Check you're actually using that version and not pulling in an older junit.
Edit: Ok I just checked your config and was able to get it working this side. My only guess can be is that you're somehow running it with a bad test runner which is causing it to use the wrong junit.
Edit 2 (SOLVED): So it turns out that the problem is because you are using a custom version of junit. Surefire looks for the provided junit library and cant find it. As a result it defaults to junit 3, which is what causes your app to skip loading the config.
You can explictly specify the custom provider like
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.3</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.12.3</version>
</dependency>
</dependencies>
</plugin>
But I have found that it does not work well with custom repos. If possible I would suggest to use the standard version of junit.
Try specific context configuration, e.g.
#ContextConfiguration(locations = {"/file1.xml", "/file2.xml" })
(just showing how this can be used with multiple files when needed - one may be enough)
Edit: have you enabled AutowiredAnnotationBeanPostProcessor as mentioned here? http://www.mkyong.com/spring/spring-auto-wiring-beans-with-autowired-annotation/
I had this same problem. My #Autowire would work within my IDE (SpringSource STS) but would fail to load the application context when I was using Maven to build from the command line.
The issue was with my dependencies in the pom.xml. I was using the Spring version of JUnit which caused the error. I think this is the root cause of the original post. I didn't have to code any Maven plugin for it work.
I changed
<dependency>
<groupId>org.junit</groupId>
<artifactId>com.springsource.org.junit</artifactId>
<version>4.7.0</version>
</dependency>
to
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>