Skip to content

Commit 9e1f28d

Browse files
committed
Bugfix, proper logging
1 parent 1d0c40c commit 9e1f28d

File tree

3 files changed

+71
-47
lines changed

3 files changed

+71
-47
lines changed

pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,26 @@
6060
<artifactId>azure-cosmos</artifactId>
6161
<version>4.3.1</version>
6262
</dependency>
63+
64+
<dependency>
65+
<groupId>org.apache.logging.log4j</groupId>
66+
<artifactId>log4j-slf4j-impl</artifactId>
67+
<version>2.13.0</version>
68+
<scope>test</scope>
69+
</dependency>
70+
71+
<dependency>
72+
<groupId>org.apache.logging.log4j</groupId>
73+
<artifactId>log4j-api</artifactId>
74+
<version>2.11.1</version>
75+
<scope>test</scope>
76+
</dependency>
77+
<dependency>
78+
<groupId>org.slf4j</groupId>
79+
<artifactId>slf4j-jdk14</artifactId>
80+
<version>1.7.28</version>
81+
</dependency>
82+
6383
<dependency>
6484
<groupId>org.apache.commons</groupId>
6585
<artifactId>commons-lang3</artifactId>

src/main/java/com/azure/cosmos/sample/async/AsyncMain.java

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ public static void main(String[] args) {
5555
AsyncMain p = new AsyncMain();
5656

5757
try {
58-
System.out.println("Starting ASYNC main");
58+
logger.info("Starting ASYNC main");
5959
p.getStartedDemo();
60-
System.out.println("Demo complete, please hold while resources are released");
60+
logger.info("Demo complete, please hold while resources are released");
6161
} catch (Exception e) {
6262
e.printStackTrace();
63-
System.err.println(String.format("Cosmos getStarted failed with %s", e));
63+
logger.error("Cosmos getStarted failed with %s", e);
6464
} finally {
65-
System.out.println("Closing the client");
65+
logger.info("Closing the client");
6666
p.close();
6767
}
6868
System.exit(0);
@@ -71,7 +71,7 @@ public static void main(String[] args) {
7171
// </Main>
7272

7373
private void getStartedDemo() throws Exception {
74-
System.out.println("Using Azure Cosmos DB endpoint: " + AccountSettings.HOST);
74+
logger.info("Using Azure Cosmos DB endpoint: {}", AccountSettings.HOST);
7575

7676
// Create async client
7777
// <CreateAsyncClient>
@@ -109,29 +109,29 @@ private void getStartedDemo() throws Exception {
109109
johnsonFamilyItem,
110110
smithFamilyItem);
111111

112-
System.out.println("Reading items.");
112+
logger.info("Reading items.");
113113
readItems(familiesToCreate);
114114

115-
System.out.println("Querying items.");
115+
logger.info("Querying items.");
116116
queryItems();
117117
}
118118

119119
private void createDatabaseIfNotExists() throws Exception {
120-
System.out.println("Create database " + databaseName + " if not exists.");
120+
logger.info("Create database {} if not exists.", databaseName);
121121

122122
// Create database if not exists
123123
// <CreateDatabaseIfNotExists>
124124
Mono<CosmosDatabaseResponse> databaseResponseMono = client.createDatabaseIfNotExists(databaseName);
125125
databaseResponseMono.flatMap(databaseResponse -> {
126126
database = client.getDatabase(databaseResponse.getProperties().getId());
127-
System.out.println("Checking database " + database.getId() + " completed!\n");
127+
logger.info("Checking database {} completed!\n", database.getId());
128128
return Mono.empty();
129129
}).block();
130130
// </CreateDatabaseIfNotExists>
131131
}
132132

133133
private void createContainerIfNotExists() throws Exception {
134-
System.out.println("Create container " + containerName + " if not exists.");
134+
logger.info("Create container {} if not exists.", containerName);
135135

136136
// Create container if not exists
137137
// <CreateContainerIfNotExists>
@@ -142,7 +142,7 @@ private void createContainerIfNotExists() throws Exception {
142142
// Create container with 400 RU/s
143143
containerResponseMono.flatMap(containerResponse -> {
144144
container = database.getContainer(containerResponse.getProperties().getId());
145-
System.out.println("Checking container " + container.getId() + " completed!\n");
145+
logger.info("Checking container {} completed!\n", container.getId());
146146
return Mono.empty();
147147
}).block();
148148

@@ -160,25 +160,25 @@ private void createFamilies(Flux<Family> families) throws Exception {
160160
return container.createItem(family);
161161
}) //Flux of item request responses
162162
.flatMap(itemResponse -> {
163-
logger.info(String.format("Created item with request charge of %.2f within" +
164-
" duration %s",
165-
itemResponse.getRequestCharge(), itemResponse.getDuration()));
166-
logger.info(String.format("Item ID: %s\n", itemResponse.getItem().getId()));
163+
logger.info("Created item with request charge of {}} within" +
164+
" duration {}",
165+
itemResponse.getRequestCharge(), itemResponse.getDuration());
166+
logger.info("Item ID: {}\n", itemResponse.getItem().getId());
167167
return Mono.just(itemResponse.getRequestCharge());
168168
}) //Flux of request charges
169169
.reduce(0.0,
170170
(charge_n, charge_nplus1) -> charge_n + charge_nplus1
171171
) //Mono of total charge - there will be only one item in this stream
172172
.block(); //Preserve the total charge and print aggregate charge/item count stats.
173173

174-
logger.info(String.format("Created items with total request charge of %.2f\n", charge));
174+
logger.info("Created items with total request charge of {}\n", charge);
175175

176176
} catch (Exception err) {
177177
if (err instanceof CosmosException) {
178178
//Client-specific errors
179179
CosmosException cerr = (CosmosException) err;
180180
cerr.printStackTrace();
181-
logger.error(String.format("Read Item failed with %s\n", cerr));
181+
logger.error("Read Item failed with %s\n", cerr);
182182
} else {
183183
//General errors
184184
err.printStackTrace();
@@ -201,8 +201,8 @@ private void readItems(Flux<Family> familiesToCreate) {
201201
}).flatMap(itemResponse -> {
202202
double requestCharge = itemResponse.getRequestCharge();
203203
Duration requestLatency = itemResponse.getDuration();
204-
logger.info(String.format("Item successfully read with id %s with a charge of %.2f and within duration %s",
205-
itemResponse.getItem().getId(), requestCharge, requestLatency));
204+
logger.info("Item successfully read with id {} with a charge of {} and within duration {}",
205+
itemResponse.getItem().getId(), requestCharge, requestLatency);
206206
return Flux.empty();
207207
}).blockLast();
208208

@@ -211,7 +211,7 @@ private void readItems(Flux<Family> familiesToCreate) {
211211
//Client-specific errors
212212
CosmosException cerr = (CosmosException) err;
213213
cerr.printStackTrace();
214-
logger.error(String.format("Read Item failed with %s\n", cerr));
214+
logger.error("Read Item failed with {}\n", cerr);
215215
} else {
216216
//General errors
217217
err.printStackTrace();
@@ -238,11 +238,10 @@ private void queryItems() {
238238
try {
239239

240240
pagedFluxResponse.byPage(preferredPageSize).flatMap(fluxResponse -> {
241-
logger.info("Got a page of query result with " +
242-
fluxResponse.getResults().size() + " items(s)"
243-
+ " and request charge of " + fluxResponse.getRequestCharge());
241+
logger.info("Got a page of query result with {} items(s)"
242+
+ " and request charge of {}", fluxResponse.getResults().size(), fluxResponse.getRequestCharge());
244243

245-
logger.info("Item Ids " + fluxResponse
244+
logger.info("Item Ids {}" , fluxResponse
246245
.getResults()
247246
.stream()
248247
.map(Family::getId)
@@ -256,7 +255,7 @@ private void queryItems() {
256255
//Client-specific errors
257256
CosmosException cerr = (CosmosException) err;
258257
cerr.printStackTrace();
259-
logger.error(String.format("Read Item failed with %s\n", cerr));
258+
logger.error("Read Item failed with %s\n", cerr);
260259
} else {
261260
//General errors
262261
err.printStackTrace();

src/main/java/com/azure/cosmos/sample/sync/SyncMain.java

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
import java.util.List;
2929
import java.util.stream.Collectors;
3030

31+
import org.slf4j.Logger;
32+
import org.slf4j.LoggerFactory;
33+
3134
public class SyncMain {
3235

3336
private CosmosClient client;
@@ -38,6 +41,8 @@ public class SyncMain {
3841
private CosmosDatabase database;
3942
private CosmosContainer container;
4043

44+
protected static Logger logger = LoggerFactory.getLogger(SyncMain.class.getSimpleName());
45+
4146
public void close() {
4247
client.close();
4348
}
@@ -52,14 +57,14 @@ public static void main(String[] args) {
5257
SyncMain p = new SyncMain();
5358

5459
try {
55-
System.out.println("Starting SYNC main");
60+
logger.info("Starting SYNC main");
5661
p.getStartedDemo();
57-
System.out.println("Demo complete, please hold while resources are released");
62+
logger.info("Demo complete, please hold while resources are released");
5863
} catch (Exception e) {
5964
e.printStackTrace();
60-
System.err.println(String.format("Cosmos getStarted failed with %s", e));
65+
logger.error("Cosmos getStarted failed with %s", e);
6166
} finally {
62-
System.out.println("Closing the client");
67+
logger.info("Closing the client");
6368
p.close();
6469
}
6570
System.exit(0);
@@ -68,7 +73,7 @@ public static void main(String[] args) {
6873
// </Main>
6974

7075
private void getStartedDemo() throws Exception {
71-
System.out.println("Using Azure Cosmos DB endpoint: " + AccountSettings.HOST);
76+
logger.info("Using Azure Cosmos DB endpoint: {}", AccountSettings.HOST);
7277

7378
// Create sync client
7479
// <CreateSyncClient>
@@ -95,27 +100,27 @@ private void getStartedDemo() throws Exception {
95100

96101
createFamilies(familiesToCreate);
97102

98-
System.out.println("Reading items.");
103+
logger.info("Reading items.");
99104
readItems(familiesToCreate);
100105

101-
System.out.println("Querying items.");
106+
logger.info("Querying items.");
102107
queryItems();
103108
}
104109

105110
private void createDatabaseIfNotExists() throws Exception {
106-
System.out.println("Create database " + databaseName + " if not exists.");
111+
logger.info("Create database " + databaseName + " if not exists.");
107112

108113
// Create database if not exists
109114
// <CreateDatabaseIfNotExists>
110115
CosmosDatabaseResponse cosmosDatabaseResponse = client.createDatabaseIfNotExists(databaseName);
111116
database = client.getDatabase(cosmosDatabaseResponse.getProperties().getId());
112117
// </CreateDatabaseIfNotExists>
113118

114-
System.out.println("Checking database " + database.getId() + " completed!\n");
119+
logger.info("Checking database " + database.getId() + " completed!\n");
115120
}
116121

117122
private void createContainerIfNotExists() throws Exception {
118-
System.out.println("Create container " + containerName + " if not exists.");
123+
logger.info("Create container " + containerName + " if not exists.");
119124

120125
// Create container if not exists
121126
// <CreateContainerIfNotExists>
@@ -128,7 +133,7 @@ private void createContainerIfNotExists() throws Exception {
128133
container = database.getContainer(cosmosContainerResponse.getProperties().getId());
129134
// </CreateContainerIfNotExists>
130135

131-
System.out.println("Checking container " + container.getId() + " completed!\n");
136+
logger.info("Checking container " + container.getId() + " completed!\n");
132137
}
133138

134139
private void createFamilies(List<Family> families) throws Exception {
@@ -145,15 +150,15 @@ private void createFamilies(List<Family> families) throws Exception {
145150
// </CreateItem>
146151

147152
// Get request charge and other properties like latency, and diagnostics strings, etc.
148-
System.out.println(String.format("Created item with request charge of %.2f within" +
149-
" duration %s",
150-
item.getRequestCharge(), item.getDuration()));
153+
logger.info("Created item with request charge of {} within" +
154+
" duration {}",
155+
item.getRequestCharge(), item.getDuration());
151156
totalRequestCharge += item.getRequestCharge();
152157
}
153-
System.out.println(String.format("Created %d items with total request " +
154-
"charge of %.2f",
158+
logger.info("Created {} items with total request " +
159+
"charge of {}",
155160
families.size(),
156-
totalRequestCharge));
161+
totalRequestCharge);
157162
}
158163

159164
private void readItems(ArrayList<Family> familiesToCreate) {
@@ -165,11 +170,11 @@ private void readItems(ArrayList<Family> familiesToCreate) {
165170
CosmosItemResponse<Family> item = container.readItem(family.getId(), new PartitionKey(family.getLastName()), Family.class);
166171
double requestCharge = item.getRequestCharge();
167172
Duration requestLatency = item.getDuration();
168-
System.out.println(String.format("Item successfully read with id %s with a charge of %.2f and within duration %s",
169-
item.getItem().getId(), requestCharge, requestLatency));
173+
logger.info("Item successfully read with id {} with a charge of {} and within duration {}",
174+
item.getItem().getId(), requestCharge, requestLatency);
170175
} catch (CosmosException e) {
171176
e.printStackTrace();
172-
System.err.println(String.format("Read Item failed with %s", e));
177+
logger.error("Read Item failed with %s", e);
173178
}
174179
// </ReadItem>
175180
});
@@ -187,11 +192,11 @@ private void queryItems() {
187192
"SELECT * FROM Family WHERE Family.lastName IN ('Andersen', 'Wakefield', 'Johnson')", queryOptions, Family.class);
188193

189194
familiesPagedIterable.iterableByPage(10).forEach(cosmosItemPropertiesFeedResponse -> {
190-
System.out.println("Got a page of query result with " +
195+
logger.info("Got a page of query result with " +
191196
cosmosItemPropertiesFeedResponse.getResults().size() + " items(s)"
192197
+ " and request charge of " + cosmosItemPropertiesFeedResponse.getRequestCharge());
193198

194-
System.out.println("Item Ids " + cosmosItemPropertiesFeedResponse
199+
logger.info("Item Ids {}", cosmosItemPropertiesFeedResponse
195200
.getResults()
196201
.stream()
197202
.map(Family::getId)

0 commit comments

Comments
 (0)