33const http = require ( 'http' ) ;
44const express = require ( 'express' ) ;
55const mysql = require ( 'mysql2' ) ;
6+ const bunyan = require ( 'bunyan' ) ;
67const { S3Client, GetBucketLocationCommand } = require ( '@aws-sdk/client-s3' ) ;
78
89const PORT = parseInt ( process . env . SAMPLE_APP_PORT || '8000' , 10 ) ;
910
1011const app = express ( ) ;
1112
13+ // Create bunyan logger
14+ const logger = bunyan . createLogger ( { name : 'express-app' , level : 'info' } ) ;
15+
1216app . 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 ( ) ;
@@ -37,19 +43,26 @@ app.get('/outgoing-http-call', (req, res) => {
3743app . get ( '/aws-sdk-call' , async ( req , res ) => {
3844 const s3Client = new S3Client ( { region : 'us-east-1' } ) ;
3945 const bucketName = 'e2e-test-bucket-name-' + ( req . query . testingId || 'MISSING_ID' ) ;
46+
47+ // Add custom warning log for validation testing
48+ const warningMsg = "This is a custom log for validation testing" ;
49+ logger . warn ( warningMsg ) ;
50+
4051 try {
4152 await s3Client . send (
4253 new GetBucketLocationCommand ( {
4354 Bucket : bucketName ,
4455 } ) ,
4556 ) . 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 ) ;
57+ const msg = '/aws-sdk-call called successfully; UNEXPECTEDLY RETURNED DATA: ' + data ;
58+ logger . info ( msg ) ;
59+ res . send ( msg ) ;
4860 } ) ;
4961 } catch ( e ) {
5062 if ( e instanceof Error ) {
51- console . log ( '/aws-sdk-call called successfully' )
52- res . send ( '/aws-sdk-call called successfully' ) ;
63+ const msg = '/aws-sdk-call called successfully' ;
64+ logger . info ( msg ) ;
65+ res . send ( msg ) ;
5366 }
5467 }
5568} ) ;
@@ -66,14 +79,16 @@ app.get('/remote-service', (req, res) => {
6679 const request = http . request ( options , ( rs ) => {
6780 rs . setEncoding ( 'utf8' ) ;
6881 rs . on ( 'data' , ( result ) => {
69- console . log ( `/remote-service called successfully: ${ result } ` ) ;
70- res . send ( `/remote-service called successfully: ${ result } ` ) ;
82+ const msg = `/remote-service called successfully: ${ result } ` ;
83+ logger . info ( msg ) ;
84+ res . send ( msg ) ;
7185 } ) ;
7286 } ) ;
7387 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- } )
88+ const msg = '/remote-service called with errors: ' + err . errors ;
89+ logger . error ( msg ) ;
90+ res . send ( msg ) ;
91+ } ) ;
7792 request . end ( ) ;
7893} ) ;
7994
@@ -82,12 +97,14 @@ let makeAsyncCall = false;
8297setInterval ( ( ) => {
8398 if ( makeAsyncCall ) {
8499 makeAsyncCall = false ;
85- console . log ( 'Async call triggered by /client-call API' ) ;
100+ logger . info ( 'Async call triggered by /client-call API' ) ;
86101
87102 const request = http . get ( 'http://local-root-client-call' , ( rs ) => {
88103 rs . setEncoding ( 'utf8' ) ;
89104 rs . on ( 'data' , ( result ) => {
90- res . send ( `GET local-root-client-call response: ${ result } ` ) ;
105+ const msg = `GET local-root-client-call response: ${ result } ` ;
106+ logger . info ( msg ) ;
107+ res . send ( msg ) ;
91108 } ) ;
92109 } ) ;
93110 request . on ( 'error' , ( err ) => { } ) ; // Expected
@@ -96,9 +113,9 @@ setInterval(() => {
96113} , 5000 ) ; // Check every 5 seconds
97114
98115app . get ( '/client-call' , ( req , res ) => {
99- res . send ( '/client-call called successfully' ) ;
100- console . log ( '/client-call called successfully' ) ;
101-
116+ const msg = '/client-call called successfully' ;
117+ logger . info ( msg ) ;
118+ res . send ( msg ) ;
102119 // Trigger async call to generate telemetry for InternalOperation use case
103120 makeAsyncCall = true ;
104121} ) ;
@@ -115,8 +132,9 @@ app.get('/mysql', (req, res) => {
115132 // Connect to the database
116133 connection . connect ( ( err ) => {
117134 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 ) ;
135+ const msg = '/mysql called with an error: ' + err . errors ;
136+ logger . error ( msg ) ;
137+ return res . status ( 500 ) . send ( msg ) ;
120138 }
121139
122140 // Perform a simple query
@@ -125,15 +143,19 @@ app.get('/mysql', (req, res) => {
125143 connection . end ( ) ;
126144
127145 if ( queryErr ) {
128- return res . status ( 500 ) . send ( 'Could not complete http request to RDS database:' + queryErr . message ) ;
146+ const msg = 'Could not complete http request to RDS database:' + queryErr . message ;
147+ logger . error ( msg ) ;
148+ return res . status ( 500 ) . send ( msg ) ;
129149 }
130150
131151 // Send the query results as the response
132- res . send ( `/outgoing-http-call response: ${ results } ` ) ;
152+ const msg = `/outgoing-http-call response: ${ results } ` ;
153+ logger . info ( msg ) ;
154+ res . send ( msg ) ;
133155 } ) ;
134156 } ) ;
135157} ) ;
136158
137159app . listen ( PORT , ( ) => {
138- console . log ( `Listening for requests on http://localhost:${ PORT } ` ) ;
160+ logger . info ( `Listening for requests on http://localhost:${ PORT } ` ) ;
139161} ) ;
0 commit comments