Skip to content

Commit 4581534

Browse files
authored
Merge pull request #18999 from panos-kakos/JAVA-49505
[JAVA-49505]
2 parents 6442e1c + c91f237 commit 4581534

File tree

9 files changed

+229
-1
lines changed

9 files changed

+229
-1
lines changed

spring-boot-modules/spring-boot-3-observation/pom.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@
7171
<artifactId>p6spy-spring-boot-starter</artifactId>
7272
<version>${p6spy-spring-boot-starter.version}</version>
7373
</dependency>
74+
<dependency>
75+
<groupId>com.github.gavlyukovskiy</groupId>
76+
<artifactId>datasource-proxy-spring-boot-starter</artifactId>
77+
<version>${datasource-proxy-spring-boot-starter.version}</version>
78+
</dependency>
7479
<dependency>
7580
<groupId>com.h2database</groupId>
7681
<artifactId>h2</artifactId>
@@ -133,8 +138,12 @@
133138

134139
<properties>
135140
<start-class>com.baeldung.samples.SimpleObservationApplication</start-class>
136-
<p6spy-spring-boot-starter.version>1.9.0</p6spy-spring-boot-starter.version>
141+
<p6spy-spring-boot-starter.version>1.12.1</p6spy-spring-boot-starter.version>
142+
<datasource-proxy-spring-boot-starter.version>1.12.1</datasource-proxy-spring-boot-starter.version>
137143
<micrometer.version>1.15.3</micrometer.version>
144+
<spring-boot.version>3.5.8</spring-boot.version>
145+
<logback.version>1.5.20</logback.version>
146+
<slf4j.version>2.0.17</slf4j.version>
138147
</properties>
139148

140149
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.logging;
2+
3+
import com.baeldung.logging.repository.CampaignRepository;
4+
import com.baeldung.logging.service.CampaignService;
5+
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.boot.CommandLineRunner;
9+
import org.springframework.boot.SpringApplication;
10+
import org.springframework.boot.autoconfigure.SpringBootApplication;
11+
import org.springframework.context.annotation.Bean;
12+
import org.springframework.context.annotation.PropertySource;
13+
14+
import java.time.LocalDate;
15+
16+
@SpringBootApplication
17+
@PropertySource({ "classpath:logging/logging.properties" })
18+
class LoggingApplication {
19+
20+
private static final Logger logger = LoggerFactory.getLogger(LoggingApplication.class);
21+
22+
public static void main(String[] args) {
23+
SpringApplication.run(LoggingApplication.class, args);
24+
}
25+
26+
@Bean
27+
CommandLineRunner demo(CampaignRepository campaignRepository, CampaignService campaignService) {
28+
return args -> {
29+
LocalDate startDate = LocalDate.of(2024, 1, 1);
30+
LocalDate endDate = LocalDate.of(2024, 12, 31);
31+
32+
logger.info("Executing query to find campaigns between {} and {}", startDate, endDate);
33+
var campaigns = campaignRepository.findCampaignsByStartDateBetween(startDate, endDate);
34+
logger.info("Found {} campaigns", campaigns.size());
35+
campaigns.forEach(campaign -> logger.info("Campaign: {}", campaign));
36+
37+
logger.info("Executing JdbcTemplate query to find campaign by name");
38+
Long campaignId = campaignService.findCampaignIdByName("Spring Campaign");
39+
logger.info("Found campaign with ID: {}", campaignId);
40+
};
41+
}
42+
43+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.baeldung.logging.model;
2+
3+
import java.math.BigDecimal;
4+
import java.time.LocalDate;
5+
6+
import jakarta.persistence.*;
7+
8+
@Entity
9+
@Table(name = "campaign")
10+
public class Campaign {
11+
12+
@Id
13+
@GeneratedValue(strategy = GenerationType.IDENTITY)
14+
private Long id;
15+
16+
@Column(name = "name")
17+
private String name;
18+
19+
@Column(name = "budget")
20+
private BigDecimal budget;
21+
22+
@Column(name = "start_date")
23+
private LocalDate startDate;
24+
25+
@Column(name = "end_date")
26+
private LocalDate endDate;
27+
28+
public Campaign() {
29+
}
30+
31+
public Campaign(String name, BigDecimal budget, LocalDate startDate, LocalDate endDate) {
32+
this.name = name;
33+
this.budget = budget;
34+
this.startDate = startDate;
35+
this.endDate = endDate;
36+
}
37+
38+
public Long getId() {
39+
return id;
40+
}
41+
42+
public void setId(Long id) {
43+
this.id = id;
44+
}
45+
46+
public String getName() {
47+
return name;
48+
}
49+
50+
public void setName(String name) {
51+
this.name = name;
52+
}
53+
54+
public BigDecimal getBudget() {
55+
return budget;
56+
}
57+
58+
public void setBudget(BigDecimal budget) {
59+
this.budget = budget;
60+
}
61+
62+
public LocalDate getStartDate() {
63+
return startDate;
64+
}
65+
66+
public void setStartDate(LocalDate startDate) {
67+
this.startDate = startDate;
68+
}
69+
70+
public LocalDate getEndDate() {
71+
return endDate;
72+
}
73+
74+
public void setEndDate(LocalDate endDate) {
75+
this.endDate = endDate;
76+
}
77+
78+
@Override
79+
public String toString() {
80+
return "Campaign{" + "id=" + id + ", name='" + name + '\'' + ", budget=" + budget + ", startDate=" + startDate + ", endDate=" + endDate + '}';
81+
}
82+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.logging.repository;
2+
3+
import com.baeldung.logging.model.Campaign;
4+
5+
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.data.jpa.repository.Query;
7+
import org.springframework.data.repository.query.Param;
8+
import org.springframework.stereotype.Repository;
9+
10+
import java.time.LocalDate;
11+
import java.util.List;
12+
13+
@Repository
14+
public interface CampaignRepository extends JpaRepository<Campaign, Long> {
15+
16+
@Query("SELECT c FROM Campaign c WHERE c.startDate BETWEEN :startDate AND :endDate")
17+
List<Campaign> findCampaignsByStartDateBetween(@Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.logging.service;
2+
3+
import org.springframework.jdbc.core.JdbcTemplate;
4+
import org.springframework.stereotype.Service;
5+
6+
@Service
7+
public class CampaignService {
8+
9+
private final JdbcTemplate jdbcTemplate;
10+
11+
public CampaignService(JdbcTemplate jdbcTemplate) {
12+
this.jdbcTemplate = jdbcTemplate;
13+
}
14+
15+
public Long findCampaignIdByName(String name) {
16+
return jdbcTemplate.queryForObject("SELECT id FROM campaign WHERE name = ?", Long.class, name);
17+
}
18+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
INSERT INTO campaign (name, budget, start_date, end_date) VALUES ('Spring Campaign', 10000.00, '2024-01-15', '2024-03-31');
2+
INSERT INTO campaign (name, budget, start_date, end_date) VALUES ('Summer Sale', 25000.00, '2024-06-01', '2024-08-31');
3+
INSERT INTO campaign (name, budget, start_date, end_date) VALUES ('Holiday Special', 50000.00, '2024-11-01', '2024-12-31');
4+
INSERT INTO campaign (name, budget, start_date, end_date) VALUES ('New Year Promo', 15000.00, '2024-01-01', '2024-01-31');
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
6+
</encoder>
7+
</appender>
8+
9+
<logger name="org.hibernate.SQL" level="DEBUG">
10+
<appender-ref ref="STDOUT"/>
11+
</logger>
12+
13+
<logger name="org.hibernate.orm.jdbc.bind" level="TRACE">
14+
<appender-ref ref="STDOUT"/>
15+
</logger>
16+
17+
<logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="TRACE">
18+
<appender-ref ref="STDOUT"/>
19+
</logger>
20+
21+
<logger name="org.springframework.jdbc.core.JdbcTemplate" level="DEBUG">
22+
<appender-ref ref="STDOUT"/>
23+
</logger>
24+
25+
<logger name="org.springframework.jdbc.core.StatementCreatorUtils" level="TRACE">
26+
<appender-ref ref="STDOUT"/>
27+
</logger>
28+
29+
<logger name="net.ttddyy.dsproxy.listener" level="DEBUG">
30+
<appender-ref ref="STDOUT"/>
31+
</logger>
32+
33+
<root level="INFO">
34+
<appender-ref ref="STDOUT"/>
35+
</root>
36+
</configuration>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
spring.jpa.show-sql=true
2+
spring.jpa.properties.hibernate.format_sql=true
3+
4+
logging.level.org.hibernate.SQL=DEBUG
5+
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
6+
logging.level.org.hibernate.orm.jdbc.bind=TRACE
7+
8+
logging.level.org.springframework.jdbc.core.JdbcTemplate=DEBUG
9+
logging.level.org.springframework.jdbc.core.StatementCreatorUtils=TRACE
10+
11+
logging.level.net.ttddyy.dsproxy.listener=debug
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CREATE TABLE IF NOT EXISTS campaign (
2+
id BIGINT AUTO_INCREMENT PRIMARY KEY,
3+
name VARCHAR(255),
4+
budget DECIMAL(19, 2),
5+
start_date DATE,
6+
end_date DATE
7+
);

0 commit comments

Comments
 (0)