Spring boot with custom logback configuration - java

I'm trying to provide some custom logging levels to the default logback configuration, but it seems my configuration file is being ignored. I though Boot may skip it for whatever reason but if I make the file not valid (remove / of self-closing tag for example) I got an exception about not being able to parse it - so it is being picked up.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- Spring boot base -->
<include resource="org/springframework/boot/logging/logback/base.xml" />
<logger name="com.wedgetail" level="WARN" />
<logger name="nv" level="WARN" />
<logger name="StatsLogger" level="WARN" />
</configuration>
So for testing purposes (as my loggers are may be wrongly spelled or whatever) I included:
<logger name="org.springframework" level="ERROR" />
When the application comes up I still see spring logging on info level:
2014-08-22 14:28:34.756 WARN 5444 --- [on(3)-127.0.0.1] o.s.b.l.LoggingApplicationListener : Logging environment value '-Djava.util.logging.config.file="C:\Documents and Settings\zykajan\.IntelliJIdea13\system\tomcat\Unnamed_cnc-parent_2\conf\logging.properties"' cannot be opened and will be ignored
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.1.5.RELEASE)
2014-08-22 14:28:34.881 INFO 5444 --- [on(3)-127.0.0.1] o.s.boot.SpringApplication : Starting application on prgdwm395059 with PID 5444 (C:\workspace\command-and-control\webapp\target\cnc-webapp-UNVERSIONED-SNAPSHOT\WEB-INF\lib\spring-boot-1.1.5.RELEASE.jar started by zykajan in C:\Apps\Apache\jakarta\apache-tomcat-7.0.55\bin)
2014-08-22 14:28:34.928 INFO 5444 --- [on(3)-127.0.0.1] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#5dd439: startup date [Fri Aug 22 14:28:34 CEST 2014]; root of context hierarchy
I wonder whether the first line should ring bells unfortunatelly it doesn't in my case :)
2014-08-22 14:28:34.756 WARN 5444 --- [on(3)-127.0.0.1] o.s.b.l.LoggingApplicationListener : Logging environment value '-Djava.util.logging.config.file="C:\Documents and Settings\zykajan\.IntelliJIdea13\system\tomcat\Unnamed_cnc-parent_2\conf\logging.properties"' cannot be opened and will be ignored
As usual, any tips much appreciated.

Upgrade to Spring Boot 1.1.6. for a fix of this issue.

Related

Using RestartEndpoint to restart application in Spring, it loops restart

I'm trying to restart the application by using RestartEndpoint, but it loops restart with Spring version 2.6.4 and java 11.
Any solutions for this problem?
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
<version>3.1.1</version>
</dependency>
#Component
public class TestRestartApplication {
#Autowired
private RestartEndpoint restartEndpoint;
public void restartApp() {
restartEndpoint.restart();
}
}
application.properties:
management.endpoint.restart.enabled=true
management.endpoints.web.exposure.include=restart,health
Console logs:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.6.4)
2022-03-16 20:53:29.904 INFO 1828 --- [ Thread-6] com.rean.ReloadSpringApplication : Starting ReloadSpringApplication using Java 11.0.2 on MSI with PID 1828 (D:\Rean Java\reload-spring-application\target\classes started by dawnt in D:\Rean Java\reload-spring-application)
2022-03-16 20:53:29.904 INFO 1828 --- [ Thread-6] com.rean.ReloadSpringApplication : No active profile set, falling back to 1 default profile: "default"
2022-03-16 20:53:30.030 INFO 1828 --- [ Thread-6] o.s.cloud.context.scope.GenericScope : BeanFactory id=abc9c58b-bafa-3329-a3c8-8f791cae2962
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.6.4)
2022-03-16 20:53:29.904 INFO 1828 --- [ Thread-6] com.rean.ReloadSpringApplication : Starting ReloadSpringApplication using Java 11.0.2 on MSI with PID 1828 (D:\Rean Java\reload-spring-application\target\classes started by dawnt in D:\Rean Java\reload-spring-application)
2022-03-16 20:53:29.904 INFO 1828 --- [ Thread-6] com.rean.ReloadSpringApplication : No active profile set, falling back to 1 default profile: "default"
2022-03-16 20:53:30.030 INFO 1828 --- [ Thread-6] o.s.cloud.context.scope.GenericScope : BeanFactory id=abc9c58b-bafa-3329-a3c8-8f791cae2962

When I start my spring boot application, my database is not initiating

I have one spring-boot test application in which I provide the following configuration in application.yml file. when I start my server using mvn test, it doesn't initiate the database connection, what can be the issue here? Am I missing any dependency in pom.xml?
spring.datasource.driverClassName = com.ibm.db2.jcc.DB2Driver
spring.datasource.url=jdbc:db2://<fullURL>
spring.datasource.username = <uname>
spring.datasource.password = <password>
Following are my dependencies related to spring-boot
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
Here are my boot logs in which I don't see the database connection logs
[INFO] Running com.test.installments.customers.data.processor.test.RunTest
11:11:30.823 [main] INFO o.s.b.t.c.SpringBootTestContextBootstrapper - Neither #ContextConfiguration nor #ContextHierarchy found for test class [com.test.in
stallments.customers.data.processor.test.RunTest], using SpringBootContextLoader
11:11:30.829 [main] INFO o.s.t.c.s.AbstractContextLoader - Could not detect default resource locations for test class [com.test.installments.customers.data.
processor.test.RunTest]: no resource found for suffixes {-context.xml, Context.groovy}.
11:11:30.976 [main] INFO o.s.b.t.c.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]:
[org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springfr
amework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecut
ionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.we
b.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframe
work.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.te
st.context.event.ApplicationEventsTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.tes
t.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.co
ntext.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
11:11:30.992 [main] INFO o.s.b.t.c.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBefor
eModesTestExecutionListener#162be91c, org.springframework.test.context.event.ApplicationEventsTestExecutionListener#2488b073, org.springframework.boot.test.mock.mo
ckito.MockitoTestExecutionListener#1c9f0a20, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener#55787112, org.springfra
mework.test.context.support.DirtiesContextTestExecutionListener#1cd201a8, org.springframework.test.context.transaction.TransactionalTestExecutionListener#7db82169,
org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener#1992eaf4, org.springframework.test.context.event.EventPublishingTestExecutionListener#f74e83
5, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener#3276732, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionLi
stener#3f28bd56, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener#31e3250d, org.springframework.boot.test.aut
oconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener#19fe4644, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListe
ner#21d8bcbe, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener#5be067de]
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.3)
11:11:31.362 [main] INFO c.m.i.c.data.processor.test.RunTest - Starting RunTest using Java 11.0.8 on test with PID 17676 (started by test in C:\git\c
ustomers-data-processor-tests)
11:11:31.362 [main] INFO c.m.i.c.data.processor.test.RunTest - The following profiles are active: dev
11:11:31.446 [main] INFO c.m.i.c.data.processor.test.RunTest - Started RunTest in 0.422 seconds (JVM running for 1.403)
-----------Databse driver name---------- com.ibm.db2.jcc.DB2Driver
11:11:32.161 [main] DEBUG com.intuit.karate.Suite - [config] classpath:karate-config.js
11:11:32.858 [main] INFO com.intuit.karate - karate.env system property was: null
11:11:32.932 [main] INFO com.intuit.karate - [print] Total number of rows
---------------------------------------------------------
feature: classpath:com/test/installments/customers/data/processor/test/fileuload/uploadfile.feature
Couple of things I note:
You are mentioning application.yml but the format of the file is properties
You need to add the Driver in your dependencies for Spring Boot to pick up as well.
If you are running test, and have an applicatio.yml in src/test/resources, it will override the one in src/main/resources

Initialization when running Junit Test case in Spring Boot

I'm writing test cases for service and util classes for my spring boot application. Following are snippets from the test cases
FooServiceImplTest.java
#RunWith(SpringRunner.class)
#SpringBootTest
public class FooServiceImplTest {
#Test
//test cases for FooServiceImplTest
}
BarUtilTest.java
#RunWith(SpringRunner.class)
#SpringBootTest
public class BarUtilTest {
#Test
//test cases for BarUtilTest
}
Now, when I run the test cases, I see something like the following in the console
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.0.RELEASE)
17:21:07.835 [main] INFO c.c.l.s.impl.FooServiceImplTest - Starting FooServiceImplTest on Users-MacBook-Pro.local with PID 8944 (started by user in /Users/user/Documents/my-project)
17:21:07.836 [main] DEBUG c.c.l.s.impl.FooServiceImplTest - Running with Spring Boot v2.1.0.RELEASE, Spring v5.1.2.RELEASE
17:21:07.836 [main] INFO c.c.l.s.impl.FooServiceImplTest - The following profiles are active: prod
17:21:10.828 [main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-2 - Starting...
17:21:10.890 [main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-2 - Start completed.
...
// runs test cases for FooServiceImplTest
17:21:33.643 [main] INFO c.c.l.s.impl.FooServiceImplTest - Started FooServiceImplTest in 40.898 seconds (JVM running for 102.231)
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.0.RELEASE)
17:21:49.290 [main] INFO c.c.l.util.BarUtilTest - Starting BarUtilTest on Users-MacBook-Pro.local with PID 8944 (started by user in /Users/user/Documents/my-project)
17:21:49.290 [main] DEBUG c.c.l.util.BarUtilTest - Running with Spring Boot v2.1.0.RELEASE, Spring v5.1.2.RELEASE
17:21:49.290 [main] INFO c.c.l.util.BarUtilTest - The following profiles are active: prod
17:21:50.809 [main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-3 - Starting...
17:21:50.972 [main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-3 - Start completed.
...
// runs test cases for BarUtilTest
17:22:04.765 [main] INFO c.c.l.util.BarUtilTest - Started BarUtilTest in 30.635 seconds (JVM running for 133.353)
So it looks like for every test suite I write, everything is initialized again. That may be one of the reasons that it is taking a lot of time to run the test cases as I add more test suites. Is there anyway to initialize everything once and run all test cases in one go?

Tomcat redeploy not completely reloading webapp

Using Apache Tomcat/8.5.24, running a Spring Boot application.
When we use the tomcat manager page and click "Reload" on our web-app, the app is not reloaded. catalina.out says that the context has been reloaded, but Spring does not fire and our endpoints don't respond. In order to get a real reload, we have to completely un-deploy the web app and re-deploy from scratch, which is less than ideal.
When I first deploy the app, I get the familiar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.4.2.RELEASE)
Splash screen and all the associated controller bindings and bean creation logs. However, when I click "reload", I get this
23-Jan-2018 15:13:17.235 INFO [https-jsse-nio-8443-exec-10] org.apache.catalina.core.StandardContext.reload Reloading Context with name [/myapp] has started
23-Jan-2018 15:13:17.237 INFO [https-jsse-nio-8443-exec-10] org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext.doClose Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#283650df: startup date [Tue Jan 23 15:11:39 UTC 2018]; root of context hierarchy
23-Jan-2018 15:13:17.246 INFO [https-jsse-nio-8443-exec-10] org.springframework.context.support.DefaultLifecycleProcessor.stop Stopping beans in phase 2147483647
23-Jan-2018 15:13:17.247 INFO [https-jsse-nio-8443-exec-10] org.springframework.jmx.export.annotation.AnnotationMBeanExporter.destroy
Unregistering JMX-exposed beans on shutdown
System is shutting down
23-Jan-2018 15:13:17.428 INFO [https-jsse-nio-8443-exec-10] org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor.shutdown Shutting down ExecutorService 'getAsyncExecutor'
23-Jan-2018 15:13:17.542 INFO [https-jsse-nio-8443-exec-10] org.apache.catalina.core.StandardContext.reload Reloading Context with name [/myapp] is completed
And that's it.
We are using external jars, defined in a context.xml file as follows:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/myapp" docBase="/myapp" reloadable="true">
<Resources>
<!-- Load the configuration files -->
<!-- Location of external log4j2 configuration file -->
<PreResources className="org.apache.catalina.webresources.DirResourceSet"
base="/mycompany/libs/config/"
webAppMount="/WEB-INF/classes" />
<!-- Location of the JARs -->
<JarResources className="org.apache.catalina.webresources.DirResourceSet"
base="/mycompany/libs/application_core/"
webAppMount="/WEB-INF/lib" />
<JarResources className="org.apache.catalina.webresources.DirResourceSet"
base="/mycompany/libs/application_supplemental/"
webAppMount="/WEB-INF/lib" />
<JarResources className="org.apache.catalina.webresources.DirResourceSet"
base="/mycompany/libs/third_party/"
webAppMount="/WEB-INF/lib" />
</Resources>
</Context>
Before we moved to this structure, this wasn't an issue so I assume it has something to do with this configuration but I'm not extremely familiar with all things tomcat and am not sure where to even look.

Turn off Tomcat logging via Spring Boot Application

I have a Spring boot application deployed on Tomcat8
When the app starts I see the following
18-Feb-2016 15:28:12.164 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.endorsed.dirs=
--- Many more JVM arguments
. ___ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.3.1.RELEASE)
18-Feb-2016 15:28:23.328 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /var/lib/tomcat8/webapps/ROOT has finished in 10,922 ms
18-Feb-2016 15:28:23.351 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
I want to stop the JVM argument being logged via org.apache.catalina.startup.VersionLoggerListener
I have a logback.xml file in my resources folder
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d %-5level [%thread] %logger{0}: %msg%n</pattern>
</encoder>
</appender>
<root>
<level value="ERROR" />
<appender-ref ref="consoleAppender" />
</root>
</configuration>
This seems to kick in and reduce logging to ERROR once the Spring container has loaded but after the JVM arg's have been logged
Does anyone know how i can stop VersionLoggerListener logging ?
You can disable the VersionsLoggerListener by removing <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> from your Tomcats server.xml.

Categories