Skip to content

Commit 1b0cf81

Browse files
author
Jeel Mehta
committed
Replace Console with bunyan lgger in Sample App
1 parent 9baec9a commit 1b0cf81

File tree

4 files changed

+57
-40
lines changed

4 files changed

+57
-40
lines changed

sample-apps/node/frontend-service/index.js

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
const http = require('http');
44
const express = require('express');
55
const mysql = require('mysql2');
6+
const bunyan = require('bunyan');
67
const { S3Client, GetBucketLocationCommand } = require('@aws-sdk/client-s3');
78

89
const PORT = parseInt(process.env.SAMPLE_APP_PORT || '8000', 10);
910

1011
const app = express();
1112

13+
// Create bunyan logger
14+
const logger = bunyan.createLogger({name: 'express-app', level: 'info'});
15+
1216
app.get('/healthcheck', (req, res) => {
13-
console.log(`/healthcheck called successfully`)
17+
logger.info('/healthcheck called successfully');
1418
res.send('healthcheck');
1519
});
1620

@@ -23,12 +27,14 @@ app.get('/outgoing-http-call', (req, res) => {
2327
const httpRequest = http.request(options, (rs) => {
2428
rs.setEncoding('utf8');
2529
rs.on('data', (result) => {
26-
console.log(`/outgoing-http-call called successfully`)
27-
res.send(`/outgoing-http-call called successfully`);
30+
const msg = '/outgoing-http-call called successfully';
31+
logger.info(msg);
32+
res.send(msg);
2833
});
2934
rs.on('error', (err) => {
30-
console.log(`/outgoing-http-call called with error: ${err}`)
31-
res.send(`/outgoing-http-call called with error: ${err}`);
35+
const msg = `/outgoing-http-call called with error: ${err}`;
36+
logger.error(msg);
37+
res.send(msg);
3238
});
3339
});
3440
httpRequest.end();
@@ -43,19 +49,21 @@ app.get('/aws-sdk-call', async (req, res) => {
4349
Bucket: bucketName,
4450
}),
4551
).then((data) => {
46-
console.log('/aws-sdk-call called successfully; UNEXPECTEDLY RETURNED DATA: ' + data);
47-
res.send('/aws-sdk-call called successfully; UNEXPECTEDLY RETURNED DATA: ' + data);
52+
const msg = '/aws-sdk-call called successfully; UNEXPECTEDLY RETURNED DATA: ' + data;
53+
logger.info(msg);
54+
res.send(msg);
4855
});
4956
} catch (e) {
5057
if (e instanceof Error) {
51-
console.log('/aws-sdk-call called successfully')
52-
res.send('/aws-sdk-call called successfully');
58+
const msg = '/aws-sdk-call called successfully';
59+
logger.info(msg);
60+
res.send(msg);
5361
}
5462
}
5563
});
5664

5765
app.get('/remote-service', (req, res) => {
58-
const endpoint = req.query.ip || 'localhost';
66+
const endpoint = req.query.ip || '*********';
5967
const options = {
6068
hostname: endpoint,
6169
port: 8001,
@@ -66,74 +74,76 @@ app.get('/remote-service', (req, res) => {
6674
const request = http.request(options, (rs) => {
6775
rs.setEncoding('utf8');
6876
rs.on('data', (result) => {
69-
console.log(`/remote-service called successfully: ${result}`);
70-
res.send(`/remote-service called successfully: ${result}`);
77+
const msg = `/remote-service called successfully: ${result}`;
78+
logger.info(msg);
79+
res.send(msg);
7180
});
7281
});
7382
request.on('error', (err) => {
74-
console.log('/remote-service called with errors: ' + err.errors);
75-
res.send('/remote-service called with errors: ' + err.errors);
76-
})
83+
const msg = '/remote-service called with errors: ' + err.errors;
84+
logger.error(msg);
85+
res.send(msg);
86+
});
7787
request.end();
7888
});
7989

80-
// The following logic serves as the async call made by the /client-call API
8190
let makeAsyncCall = false;
8291
setInterval(() => {
8392
if (makeAsyncCall) {
8493
makeAsyncCall = false;
85-
console.log('Async call triggered by /client-call API');
94+
logger.info('Async call triggered by /client-call API');
8695

8796
const request = http.get('http://local-root-client-call', (rs) => {
8897
rs.setEncoding('utf8');
8998
rs.on('data', (result) => {
90-
res.send(`GET local-root-client-call response: ${result}`);
99+
const msg = `GET local-root-client-call response: ${result}`;
100+
logger.info(msg);
101+
res.send(msg);
91102
});
92103
});
93104
request.on('error', (err) => {}); // Expected
94105
request.end();
95106
}
96-
}, 5000); // Check every 5 seconds
107+
}, 5000);
97108

98109
app.get('/client-call', (req, res) => {
99-
res.send('/client-call called successfully');
100-
console.log('/client-call called successfully');
101-
102-
// Trigger async call to generate telemetry for InternalOperation use case
110+
const msg = '/client-call called successfully';
111+
logger.info(msg);
112+
res.send(msg);
103113
makeAsyncCall = true;
104114
});
105115

106116
app.get('/mysql', (req, res) => {
107-
// Create a connection to the MySQL database
108117
const connection = mysql.createConnection({
109118
host: process.env.RDS_MYSQL_CLUSTER_ENDPOINT,
110119
user: process.env.RDS_MYSQL_CLUSTER_USERNAME,
111120
password: process.env.RDS_MYSQL_CLUSTER_PASSWORD,
112121
database: process.env.RDS_MYSQL_CLUSTER_DATABASE,
113122
});
114123

115-
// Connect to the database
116124
connection.connect((err) => {
117125
if (err) {
118-
console.log('/mysql called with an error: ', err.errors);
119-
return res.status(500).send('/mysql called with an error: ' + err.errors);
126+
const msg = '/mysql called with an error: ' + err.errors;
127+
logger.error(msg);
128+
return res.status(500).send(msg);
120129
}
121130

122-
// Perform a simple query
123131
connection.query('SELECT * FROM tables LIMIT 1;', (queryErr, results) => {
124-
// Close the connection
125132
connection.end();
126133

127134
if (queryErr) {
128-
return res.status(500).send('Could not complete http request to RDS database:' + queryErr.message);
135+
const msg = 'Could not complete http request to RDS database:' + queryErr.message;
136+
logger.error(msg);
137+
return res.status(500).send(msg);
129138
}
130139

131-
// Send the query results as the response
132-
res.send(`/outgoing-http-call response: ${results}`);
140+
const msg = `/outgoing-http-call response: ${results}`;
141+
logger.info(msg);
142+
res.send(msg);
133143
});
134144
});
135145
});
136146

137147
app.listen(PORT, () => {
138-
console.log(`Listening for requests on http://localhost:${PORT}`);
139-
});
148+
logger.info(`Listening for requests on http://localhost:${PORT}`);
149+
});

sample-apps/node/frontend-service/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"@types/express": "^4.17.21",
1515
"@types/node": "^20.14.6",
1616
"express": "^4.21.2",
17-
"mysql2": "^3.11.0"
17+
"mysql2": "^3.11.0",
18+
"bunyan": "^1.8.15"
1819
}
1920
}
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
'use strict';
22

33
const express = require('express');
4+
const bunyan = require('bunyan');
45

56
const PORT = parseInt(process.env.SAMPLE_APP_PORT || '8001', 10);
67

78
const app = express();
89

10+
// Create bunyan logger
11+
const logger = bunyan.createLogger({name: 'remote-service', level: 'info'});
12+
913
app.get('/healthcheck', (req, res) => {
10-
console.log(`/healthcheck (remote-service) called successfully`);
11-
res.send('/healthcheck (remote-service) called successfully');
14+
const msg = '/healthcheck (remote-service) called successfully';
15+
logger.info(msg);
16+
res.send(msg);
1217
});
1318

1419
app.listen(PORT, () => {
15-
console.log(`Listening for requests on http://localhost:${PORT}`);
16-
});
20+
logger.info(`Listening for requests on http://localhost:${PORT}`);
21+
});

sample-apps/node/remote-service/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"@types/node": "^20.14.6",
1616
"express": "^4.21.2",
1717
"mysql2": "^3.11.0",
18-
"nodemon": "^3.1.4"
18+
"nodemon": "^3.1.4",
19+
"bunyan": "^1.8.15"
1920
}
2021
}

0 commit comments

Comments
 (0)