Skip to content

Commit a288e7b

Browse files
committed
update application logging + refactor application startup
1 parent d37555b commit a288e7b

File tree

9 files changed

+96
-328
lines changed

9 files changed

+96
-328
lines changed
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
package org.couchbase.quickstart.springboot;
22

3+
import org.springframework.boot.CommandLineRunner;
34
import org.springframework.boot.SpringApplication;
45
import org.springframework.boot.autoconfigure.SpringBootApplication;
56
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
67

8+
import lombok.extern.slf4j.Slf4j;
9+
710
@SpringBootApplication(exclude = SecurityAutoConfiguration.class, proxyBeanMethods = false)
8-
public class Application {
11+
@Slf4j
12+
public class Application implements CommandLineRunner {
913

1014
public static void main(String[] args) {
1115
SpringApplication.run(Application.class, args);
1216
}
1317

18+
public void run(String... args) {
19+
log.info("Application started successfully.");
20+
}
21+
1422
}

src/main/java/org/couchbase/quickstart/springboot/configs/CollectionNames.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,11 @@
22

33
public class CollectionNames {
44

5+
private CollectionNames() {
6+
}
7+
58
public static final String PROFILE = "profile";
9+
public static final String AIRPORT = "airport";
10+
public static final String AIRLINE = "airline";
11+
public static final String ROUTE = "route";
612
}

src/main/java/org/couchbase/quickstart/springboot/configs/CouchbaseConfig.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
package org.couchbase.quickstart.springboot.configs;
22

3-
import com.couchbase.client.core.error.CouchbaseException;
4-
import org.springframework.beans.factory.annotation.Autowired;
53
import org.springframework.context.annotation.Bean;
64
import org.springframework.context.annotation.Configuration;
75

6+
import com.couchbase.client.core.error.CouchbaseException;
87
import com.couchbase.client.core.msg.kv.DurabilityLevel;
98
import com.couchbase.client.java.Bucket;
109
import com.couchbase.client.java.Cluster;
1110
import com.couchbase.client.java.manager.bucket.BucketSettings;
1211
import com.couchbase.client.java.manager.bucket.BucketType;
1312

13+
import lombok.extern.slf4j.Slf4j;
14+
1415
@Configuration
16+
@Slf4j
1517
public class CouchbaseConfig {
1618

17-
@Autowired
18-
private DBProperties dbProp;
19+
private final DBProperties dbProp;
20+
21+
public CouchbaseConfig(DBProperties dbProp) {
22+
this.dbProp = dbProp;
23+
}
24+
1925

2026
/**
2127
* NOTE: If connecting to Couchbase Capella, you must enable TLS.
@@ -32,6 +38,7 @@ public class CouchbaseConfig {
3238
@Bean(destroyMethod = "disconnect")
3339
public Cluster getCouchbaseCluster() {
3440
try {
41+
log.debug("Connecting to Couchbase cluster at " + dbProp.getHostName());
3542
return Cluster.connect(dbProp.getHostName(), dbProp.getUsername(), dbProp.getPassword());
3643

3744
// Here is an alternative version that enables TLS by configuring the cluster environment.
@@ -50,12 +57,13 @@ public Cluster getCouchbaseCluster() {
5057
);
5158
5259
*/
60+
5361
} catch (CouchbaseException e) {
54-
System.err.println("Could not connect to Couchbase cluster at " + dbProp.getHostName());
55-
System.err.println("Please check the username (" + dbProp.getUsername() + ") and password (" + dbProp.getPassword() + ")");
62+
log.error("Could not connect to Couchbase cluster at " + dbProp.getHostName());
63+
log.error("Please check the username (" + dbProp.getUsername() + ") and password (" + dbProp.getPassword() + ")");
5664
throw e;
5765
} catch (Exception e) {
58-
System.err.println("Could not connect to Couchbase cluster at " + dbProp.getHostName());
66+
log.error("Could not connect to Couchbase cluster at " + dbProp.getHostName());
5967
throw e;
6068
}
6169

@@ -64,14 +72,19 @@ public Cluster getCouchbaseCluster() {
6472
@Bean
6573
public Bucket getCouchbaseBucket(Cluster cluster) {
6674

67-
// Creates the cluster if it does not exist yet
68-
if (!cluster.buckets().getAllBuckets().containsKey(dbProp.getBucketName())) {
69-
cluster.buckets().createBucket(
70-
BucketSettings.create(dbProp.getBucketName())
71-
.bucketType(BucketType.COUCHBASE)
72-
.minimumDurabilityLevel(DurabilityLevel.NONE)
73-
.ramQuotaMB(128));
75+
try {
76+
// Creates the cluster if it does not exist yet
77+
if (!cluster.buckets().getAllBuckets().containsKey(dbProp.getBucketName())) {
78+
cluster.buckets().createBucket(
79+
BucketSettings.create(dbProp.getBucketName())
80+
.bucketType(BucketType.COUCHBASE)
81+
.minimumDurabilityLevel(DurabilityLevel.NONE)
82+
.ramQuotaMB(128));
83+
}
84+
return cluster.bucket(dbProp.getBucketName());
85+
} catch (Exception e) {
86+
log.error("Could not connect to Couchbase bucket " + dbProp.getBucketName());
87+
throw e;
7488
}
75-
return cluster.bucket(dbProp.getBucketName());
7689
}
7790
}

src/main/java/org/couchbase/quickstart/springboot/controllers/AirlineController.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
import com.couchbase.client.core.error.DocumentNotFoundException;
2323

2424
import io.swagger.v3.oas.annotations.Operation;
25+
import lombok.extern.slf4j.Slf4j;
2526

2627
@RestController
2728
@CrossOrigin
2829
@RequestMapping("/api/v1/airline")
30+
@Slf4j
2931
public class AirlineController {
3032

3133
private final AirlineService airlineService;
@@ -34,6 +36,11 @@ public AirlineController(AirlineService airlineService) {
3436
this.airlineService = airlineService;
3537
}
3638

39+
// All errors
40+
private static final String INTERNAL_SERVER_ERROR = "Internal Server Error";
41+
private static final String DOCUMENT_NOT_FOUND = "Document Not Found";
42+
private static final String DOCUMENT_EXISTS = "Document Exists";
43+
3744
@Operation(summary = "Get an airline by ID")
3845
@GetMapping("/{id}")
3946
public ResponseEntity<Airline> getAirline(@PathVariable String id) {
@@ -42,11 +49,14 @@ public ResponseEntity<Airline> getAirline(@PathVariable String id) {
4249
if (airline != null) {
4350
return new ResponseEntity<>(airline, HttpStatus.OK);
4451
} else {
52+
log.error(DOCUMENT_NOT_FOUND + ": " + id);
4553
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
4654
}
4755
} catch (DocumentNotFoundException e) {
56+
log.error(DOCUMENT_NOT_FOUND + ": " + id);
4857
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
4958
} catch (Exception e) {
59+
log.error(INTERNAL_SERVER_ERROR + ": " + e.getMessage());
5060
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
5161
}
5262
}
@@ -58,8 +68,10 @@ public ResponseEntity<Airline> createAirline(@PathVariable String id, @Valid @Re
5868
Airline newAirline = airlineService.createAirline(airline);
5969
return new ResponseEntity<>(newAirline, HttpStatus.CREATED);
6070
} catch (DocumentExistsException e) {
71+
log.error(DOCUMENT_EXISTS + ": " + id);
6172
return new ResponseEntity<>(HttpStatus.CONFLICT);
6273
} catch (Exception e) {
74+
log.error(INTERNAL_SERVER_ERROR + ": " + e.getMessage());
6375
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
6476
}
6577

@@ -76,8 +88,10 @@ public ResponseEntity<Airline> updateAirline(@PathVariable String id, @Valid @Re
7688
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
7789
}
7890
} catch (DocumentNotFoundException e) {
91+
log.error("Document not found: " + id);
7992
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
8093
} catch (Exception e) {
94+
log.error(INTERNAL_SERVER_ERROR + ": " + e.getMessage());
8195
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
8296
}
8397
}
@@ -89,8 +103,10 @@ public ResponseEntity<Void> deleteAirline(@PathVariable String id) {
89103
airlineService.deleteAirline(id);
90104
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
91105
} catch (DocumentNotFoundException e) {
106+
log.error("Document not found: " + id);
92107
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
93108
} catch (Exception e) {
109+
log.error(INTERNAL_SERVER_ERROR + ": " + e.getMessage());
94110
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
95111
}
96112
}
@@ -102,6 +118,7 @@ public ResponseEntity<List<Airline>> listAirlines() {
102118
List<Airline> airlines = airlineService.listAirlines();
103119
return new ResponseEntity<>(airlines, HttpStatus.OK);
104120
} catch (Exception e) {
121+
log.error(INTERNAL_SERVER_ERROR + ": " + e.getMessage());
105122
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
106123
}
107124
}
@@ -113,6 +130,7 @@ public ResponseEntity<List<Airline>> listAirlinesByCountry(@PathVariable String
113130
List<Airline> airlines = airlineService.listAirlinesByCountry(country);
114131
return new ResponseEntity<>(airlines, HttpStatus.OK);
115132
} catch (Exception e) {
133+
log.error(INTERNAL_SERVER_ERROR + ": " + e.getMessage());
116134
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
117135
}
118136
}

src/main/java/org/couchbase/quickstart/springboot/controllers/AirportController.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
import com.couchbase.client.core.error.DocumentNotFoundException;
2323

2424
import io.swagger.v3.oas.annotations.Operation;
25+
import lombok.extern.slf4j.Slf4j;
2526

2627
@RestController
2728
@RequestMapping("/api/v1/airport")
29+
@Slf4j
2830
public class AirportController {
2931

3032
private final AirportService airportService;
@@ -33,6 +35,11 @@ public AirportController(AirportService airportService) {
3335
this.airportService = airportService;
3436
}
3537

38+
// All errors
39+
private static final String INTERNAL_SERVER_ERROR = "Internal Server Error";
40+
private static final String DOCUMENT_NOT_FOUND = "Document Not Found";
41+
private static final String DOCUMENT_EXISTS = "Document Exists";
42+
3643
@Operation(summary = "Get an airport by ID")
3744
@GetMapping("/{id}")
3845
public ResponseEntity<Airport> getAirport(@PathVariable String id) {
@@ -41,11 +48,14 @@ public ResponseEntity<Airport> getAirport(@PathVariable String id) {
4148
if (airport != null) {
4249
return new ResponseEntity<>(airport, HttpStatus.OK);
4350
} else {
51+
log.error(DOCUMENT_NOT_FOUND + ": " + id);
4452
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
4553
}
4654
} catch (DocumentNotFoundException e) {
55+
log.error(DOCUMENT_NOT_FOUND + ": " + id);
4756
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
4857
} catch (Exception e) {
58+
log.error(INTERNAL_SERVER_ERROR + ": " + e.getMessage());
4959
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
5060
}
5161
}
@@ -57,8 +67,10 @@ public ResponseEntity<Airport> createAirport(@PathVariable String id, @Valid @Re
5767
Airport newAirport = airportService.createAirport(airport);
5868
return new ResponseEntity<>(newAirport, HttpStatus.CREATED);
5969
} catch (DocumentExistsException e) {
70+
log.error(DOCUMENT_EXISTS + ": " + id);
6071
return new ResponseEntity<>(HttpStatus.CONFLICT);
6172
} catch (Exception e) {
73+
log.error(INTERNAL_SERVER_ERROR + ": " + e.getMessage());
6274
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
6375
}
6476

@@ -75,8 +87,10 @@ public ResponseEntity<Airport> updateAirport(@PathVariable String id, @Valid @Re
7587
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
7688
}
7789
} catch (DocumentNotFoundException e) {
90+
log.error("Document not found: " + id);
7891
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
7992
} catch (Exception e) {
93+
log.error(INTERNAL_SERVER_ERROR + ": " + e.getMessage());
8094
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
8195
}
8296
}
@@ -88,8 +102,10 @@ public ResponseEntity<Void> deleteAirport(@PathVariable String id) {
88102
airportService.deleteAirport(id);
89103
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
90104
} catch (DocumentNotFoundException e) {
105+
log.error("Document not found: " + id);
91106
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
92107
} catch (Exception e) {
108+
log.error(INTERNAL_SERVER_ERROR + ": " + e.getMessage());
93109
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
94110
}
95111
}
@@ -101,6 +117,7 @@ public ResponseEntity<List<Airport>> listAirports() {
101117
List<Airport> airports = airportService.listAirports();
102118
return new ResponseEntity<>(airports, HttpStatus.OK);
103119
} catch (Exception e) {
120+
log.error(INTERNAL_SERVER_ERROR + ": " + e.getMessage());
104121
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
105122
}
106123
}
@@ -112,6 +129,7 @@ public ResponseEntity<List<Airport>> listDirectConnections(@RequestParam String
112129
List<Airport> airports = airportService.listDirectConnections(airportCode);
113130
return new ResponseEntity<>(airports, HttpStatus.OK);
114131
} catch (Exception e) {
132+
log.error(INTERNAL_SERVER_ERROR + ": " + e.getMessage());
115133
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
116134
}
117135
}

src/main/java/org/couchbase/quickstart/springboot/controllers/HealthCheckController.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)