How to add default logback.xml to spring boot project? - spring-boot

I have created a new spring boot project using spring initializer with spring-boot-starter-web as a starter project.I am able to use logback for logging but am unable to find default logback.xml in the project.I tried to search out but could not find reference to default file on logback documentation as well. I am looking to customize the logback file to use custom appender.

You can customize by using your application.yml, like this :
logging:
level:
root: WARN
org.springframework.security: DEBUG
org.springframework.web.servlet: INFO
org.springframework.jmx: INFO
org.springframework.cloud.config.client: INFO
org.springframework.cloud.context: INFO
org.hibernate: INFO
org.hibernate.sql: DEBUG
pattern:
console: '%date{HH:mm:ss.SSS} [%-5level] %-65logger - %msg%n'
file: '%date{HH:mm:ss.SSS} [%-5level] %-65logger - %msg%n'
file: D:/logs/my-log.log
And define for each profile

Related

How can I override the JPA properties for multiple datasources for Integration tests?

I have sucessfully configured two datasources for two different databases and schemas in my Spring Boot application. Now, for the integration tests I want to use an embedded database (HSQL) and execute the tests there. I tried overriding the properties using the following file (/src/test/resources/application-test.properties)
eot.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver
eot.datasource.username=sa
eot.datasource.password=sa
eot.datasource.url=jdbc:hsqldb:mem:test;DB_CLOSE_DELAY=-1
eot.datasource.hikari.pool-name=ptest-eot
eot.datasource.jpa.show-sql=true
eot.datasource.jpa.generate-ddl=true
eot.datasource.jpa.database-platform=org.hibernate.dialect.HSQLDialect
eot.datasource.jpa.properties.hibernate.ddl-auto=create
eot.datasource.jpa.properties.hibernate.dialect=org.hibernate.dialect.HSQLDialect
info.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver
info.datasource.username=sa
info.datasource.password=sa
info.datasource.url=jdbc:hsqldb:mem:test;DB_CLOSE_DELAY=-1
info.datasource.hikari.pool-name=ptest-info
info.datasource.jpa.show-sql=true
info.datasource.jpa.generate-ddl=true
info.datasource.jpa.database-platform=org.hibernate.dialect.HSQLDialect
info.datasource.jpa.properties.hibernate.ddl-auto=create
info.datasource.jpa.properties.hibernate.dialect=org.hibernate.dialect.HSQLDialect
In the log I can see it picks up some of the properties. In my application.yml the pool's name is 'eot-pool' and when I run the tests it shows correctly as 'ptest-eot'.
2022-05-27 17:09:03.855 INFO 6592 --- [ Test worker] mx.com.gnp.crm.adfe.EotJpaConfiguration : org.springframework.boot.autoconfigure.jdbc.DataSourceProperties#18a1fd92
2022-05-27 17:09:04.039 INFO 6592 --- [ Test worker] com.zaxxer.hikari.HikariDataSource : ptest-eot - Starting...
2022-05-27 17:09:04.845 INFO 6592 --- [ Test worker] com.zaxxer.hikari.pool.PoolBase : ptest-eot - Driver does not support get/set network timeout for connections. (característica no soportada)
2022-05-27 17:09:04.851 INFO 6592 --- [ Test worker] com.zaxxer.hikari.HikariDataSource : ptest-eot - Start completed.
But it's not overriding the JPA properties. It's not creating the schemas, tables, nor printing the SQL statements to the log.
I tried removing the 'properties' part. Using for example:
info.datasource.jpa.show-sql=true
info.datasource.jpa.hibernate.ddl-auto=create
info.datasource.jpa.hibernate.dialect=org.hibernate.dialect.HSQLDialect
But the JPA properties aren't being replaced.
When I run the tests, the log shows the wrong dialect (from the main application.yml file).
HHH000400: Using dialect: org.hibernate.dialect.DB2400Dialect
And when the tests runs:
SQL Error: -5501, SQLState: 42501
Because the schema and the table don't exist.
How can I override the JPA properties for integration tests when I have multiple datasources?
It seems you have a general application.yml file and a specific application-test.properties file for the test environment. I'm not sure if you can mix those file extensions, probably not. Either you choose to use .yml or .properties for both. Try to change the file name to application-test.yml. Also, to activate this specific test environment it's necessary to put this config in application.yml:
spring:
profiles:
active:test

spring boot logback not logging my application logs

Need help... what am I doing wrong here???
I believe it does read the path and file name from application.properties file. But I don't think it reads the logback.xml or logback-spring.xml
I did some research and found many questions/answers. But I think everyone says to put the logback xml in resources and give the path and file name in application.properties. I know it is simple, but missing something somewhere...
Thanks in advance!!
application.properties
# ===============================
# LOGGING
# ===============================
# log configuration -- update config location as needed
#logging.config=/logback-spring.xml
#logging.file=/logs/iqs-service.txt
logging.path=/logs
logging.file=${logging.path}/log.log
logging.pattern.file=%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M - %msg%n
Content of log file (log.log.0):
[2018-12-10 15:54:41.999] - 10828 INFO [main] --- org.apache.catalina.core.StandardService: Starting service Tomcat
[2018-12-10 15:54:42.002] - 10828 INFO [main] --- org.apache.catalina.core.StandardEngine: Starting Servlet Engine: Apache Tomcat/8.5.14
[2018-12-10 15:54:42.257] - 10828 INFO [localhost-startStop-1] --- org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/]: Initializing Spring embedded WebApplicationContext
[2018-12-10 15:54:46.551] - 10828 INFO [http-nio-19917-exec-1] --- org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/]: Initializing Spring FrameworkServlet 'dispatcherServlet
Log statements in my java method - This never prints in the log
log.info("init(): [" + modelId + "]");
log.error("init(): [" + modelId + "]");
As far as I can tell the only thing I think that will kill it are these lines:
logging.path=/logs
logging.file=${logging.path}/log.log
I believe that if you include both, Spring Boot will ignore both of them. So I'd try removing one of those, and seeing if that helps.
As another thing (but I don't think it contributes to this problem), Spring recommends that you have just a logback-spring.xml file, not logback.xml (and definitely not both).

Spring Boot 1.4 not loading application.properties after upgrade

I am in process of upgrading a spring boot 1.3.3 project to 1.4.2.
After upgrading the project Spring boot will no longer use any application.properties file. It does not locate it. Even when passed as an startup argument.
The project is built with gradle and a shadowjar plugin to compile to a fat jar and start tomcat embedded from the jar.
The way the application is started is: java -jar app-all.jar. The directory containing the jar also contains the application.properties file. (and no other files)
There is no classpath set and the jar file does not contain any other application.properties files. (Tested with unzip -t app-all.jar |grep application.properties)
Starting the project under Spring Boot 1.3.3. The application starts correctly. Start of log file:
04:15:58.846 com.app.MaxApplication INFO [main] - Starting MaxApplication on test-ng with PID 30096 (/home/ubuntu/app-all.jar started by ubuntu in /home/ubuntu)
04:15:58.850 com.app.MaxApplication DEBUG [main] - Running with Spring Boot, Spring
04:15:58.850 com.app.MaxApplication INFO [main] - The following profiles are active: TEST
04:15:58.850 org.springframework.boot.SpringApplication DEBUG [main] - Loading source class com.app.MaxApplication
04:15:58.947 org.springframework.boot.context.config.ConfigFileApplicationListener DEBUG [main] - Activated profiles TEST
04:15:58.947 org.springframework.boot.context.config.ConfigFileApplicationListener DEBUG [main] - Loaded config file 'file:./application.properties'
Changing spring boot version to 1.4.2, cleaning gradle caches and rebuilding and running the application the same way Spring Boot does nto load the applciation.properties file and the application fails to start due to missing configuration settings provided by the file. Start of log file:
01:48:47.547 [main] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Adding [servletConfigInitParams] PropertySource with lowest search precedence
01:48:47.549 [main] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Adding [servletContextInitParams] PropertySource with lowest search precedence
01:48:47.551 [main] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
01:48:47.552 [main] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
01:48:47.552 [main] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,systemProperties,systemEnvironment]
01:48:47.552 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.profiles.active' in any property source
01:48:47.552 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.profiles.active' in any property source
01:48:47.554 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'banner.image.location' in any property source
01:48:47.555 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'banner.location' in any property source
01:48:47.556 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'banner.charset' in any property source
01:48:47.557 [main] DEBUG org.springframework.core.env.MutablePropertySources - Adding [version] PropertySource with lowest search precedence
01:48:47.560 [main] DEBUG org.springframework.core.env.MutablePropertySources - Adding [ansi] PropertySource with highest search precedence
01:48:47.561 [main] DEBUG org.springframework.core.env.MutablePropertySources - Adding [title] PropertySource with highest search precedence
01:48:47.600 [main] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Adding [servletConfigInitParams] PropertySource with lowest search precedence
01:48:47.600 [main] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Adding [servletContextInitParams] PropertySource with lowest search precedence
01:48:47.600 [main] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
01:48:47.600 [main] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
01:48:47.601 [main] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,systemProperties,systemEnvironment]
01:48:47.637 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
01:48:47.649 [main] INFO com.app.MaxApplication - Starting MaxApplication on test-ng (/home/ubuntu/app-all.jar started by ubuntu in /home/ubuntu)
01:48:47.649 [main] DEBUG com.app.max.MaxApplication - Running with Spring Boot, Spring
01:48:47.650 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.profiles.active' in any property source
01:48:47.650 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.profiles.default' in any property source
01:48:47.650 [main] INFO com.app.max.MaxApplication - No active profile set, falling back to default profiles: default
01:48:47.650 [main] DEBUG org.springframework.boot.SpringApplication - Loading source class com.app.MaxApplication
Only two files in the current directory are the application.properties file and the jar file. The externalizing configration documentation says the configuration should be read from there. It also works under Spring 1.3.3
I have defined a configuration class (this is working in 1.3)
#Component
#Configuration
#SpringBootConfiguration
public class MaxApplicationProperties {
#Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
I have tried:
Adding application.properties file to the class path.
Starting the application with --spring.config.location=application.properties (and with full path as well)
Setting config location environment variables.
Adding annotation #PropertySource("classpath:application.properties") to the MaxApplicationProperties class
Force deleting all downloaded libraries and having gradle re-download everything.
None of these options work to make Spring boot attempt to use the application.properties file the same was as Spring boot 1.3.
Anyone know what I am missing? Thanks.

Spring Boot : LOGBack custom fields in every log statement

Thought of sharing the below work as I struggled to find this info:
Requirement is to put the business correlation id(AccCode and Mac) in every log statement. This can be done by using Mapped Diagnostic Context(MDC)..
No need to understand how LOGBack appender->Encoder->Layout work.
Spring boot log4j.properties has below pattern:
LOG_LEVEL_PATTERN=%5p
LOG_PATTERN=[%d{yyyy-MM-dd HH:mm:ss.SSS}] boot%X{context} - ${PID} ${LOG_LEVEL_PATTERN} [%t] --- %c{1}: %m%n
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=${LOG_PATTERN}
Override LOG_LEVEL_PATTERN from application.properties with custom fields:
logging.pattern.level=AccCode:%X{AccCode} Mac:%X{Mac} %5p
Please note, now we have two extra fields defined into the layout. AccCode and Mac. Value for these new fields need to be provided using the MDC:
MDC.put("AccCode", "46014" );
MDC.put("Mac", "2025b9bcf");
Then all the log statements would have AccCode and Mac as below:
2016-09-12 09:11:37.574 **AccCode:46014** **Mac:2025b9bcf** DEBUG 9724 --- [main] c.c.p.sph.http.client.SphApAdminClient : Loging out from Sph

Spring-Camel (camel logs by default goes console appender)

I am facing one strange problem , I am working on one batch application using below tech STACK
Spring (IOC container) , spring batch , Hibernate , camel and Log4J for logging
I have configured log4J for file appender only and my all application logs goes to my log file , but strange thing is that all Camel logs goes to console appender (I have no console apender configured on my log4J properties file)
Spring – Camel Integration is XMl element :
<camelContext xmlns="http://camel.apache.org/schema/spring">
<routeBuilder ref="smtBatchRouter" />
</camelContext>
My Log4J configuration:
log4j.rootLogger=TRACE, file
log4j.logger.org.apache=TRACE
log4j.logger.org.directwebremoting=OFF
log4j.logger.org.hibernate=OFF
log4j.logger.org.springframework=WARN
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.file=myapp.log
log4j.appender.file.ImmediateFlush=true
log4j.appender.file.bufferedIO=false
log4j.appender.file.DatePattern='.' dd-MM-yyyy
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
log4j.appender.file.threshold=TRACE
Sample Camel Log that I am getting in console :
[main] INFO org.apache.camel.spring.SpringCamelContext - Apache Camel 2.11.0 (CamelContext: camel-1) started in 4.009 seconds
[Camel (camel-1) thread #0 - sftp://xyz.com/smtDataFolder] INFO org.apache.camel.component.file.remote.SftpOperations - JSCH -> Connecting to localhost port 22
[Camel (camel-1) thread #0 - sftp://xyz.com/smtDataFolder] INFO org.apache.camel.component.file.remote.SftpOperations - JSCH -> Connection established
[Camel (camel-1) thread #0 - sftp://xyz.com/smtDataFolder] INFO org.apache.camel.component.file.remote.SftpOperations - JSCH -> Remote version string: SSH-1.99-OpenSSH_4.6
I had the same issue and solved it.
Run your application with the flag:
-Dlog4j.debug
It will add diagnostic information to your console. In my case it was
SLF4J: Class path contains multiple SLF4J bindings.
So I just deleted redundant slf4j-jdk14-1.7.9.jar and left only slf4j-log4j12-1.7.19.jar.

Resources