6262 * - CLOUDANTNOSQLDB_PORT (optional) DB port number (default: 443)
6363 * - CLOUDANTNOSQLDB_APIKEY The IBM Cloud IAM APIkey if using IAMAuthentication on DB connection
6464 * - DB_NAME The name of the DB to listen on. e.g "MyTestDB"
65- * - DB_LAST_SEQ (optional) last_seq value to use as start identifier for db changes feed.
65+ * - DB_LAST_SEQ (optional) lastSeq value to use as start identifier for db changes feed.
6666 * - CE_TARGET Full URL of the target Code Engine function or application that should receive events
6767 * - DB_POST_CHANGES_TIMEOUT Max wait-time in milliseconds on each long-polling call to the DB (default: 8000)
6868 * The poll timeout can be used to adapt the listening timeout to the settings defined on the cloudant DB
7373 *************************************************************************************/
7474import { CloudantV1 } from "@ibm-cloud/cloudant" ;
7575import { BasicAuthenticator , IamAuthenticator } from "ibm-cloud-sdk-core" ;
76- import log4js from "log4js" ;
7776import { LRUCache } from "lru-cache" ;
7877import { isJobAlreadyRunning , updateJobConfig , getJobConfig } from "./ce-api-utils.mjs" ;
78+ import winston from "winston" ;
79+ const { combine, json } = winston . format ;
7980
80- //
81- // use a formatted logger to have timestamps in the log output
82- log4js . configure ( {
83- appenders : {
84- out : { type : "stdout" } ,
85- } ,
86- categories : {
87- default : { appenders : [ "out" ] , level : process . env . LOGLEVEL || "info" } ,
88- } ,
81+ const logger = winston . createLogger ( {
82+ level : "debug" ,
83+ transports : [ new winston . transports . Console ( ) ] ,
84+ format : combine ( json ( ) ) ,
8985} ) ;
90- const logger = log4js . getLogger ( ) ;
86+
9187
9288logger . info ( 'Starting Cloudant DB change listener ...' ) ;
9389
@@ -358,7 +354,7 @@ function dbChangeHandler(change) {
358354 logger . info ( `Successfully called CE URL of app or function with response: '${ httpRes . status } ' - duration: ${ Date . now ( ) - startTime } ms` ) ;
359355 } )
360356 . catch ( ( err ) => {
361- logger . error ( `Failed to call CE URL of app or function'${ targetCEUrl } ', err = ` , err ) ;
357+ logger . error ( `Failed to call CE URL of app or function '${ targetCEUrl } '` , err ) ;
362358 } ) ;
363359}
364360
@@ -433,7 +429,7 @@ async function doListen() {
433429 //* change from which the listening starts. This since option is controlled in
434430 //* this loop by following rules:
435431 //* - use DB_LAST_SEQ value on first cycle of loop
436- //* - if waitForDbChanges() call provides a valid last_seq value in response, then
432+ //* - if waitForDbChanges() call provides a valid lastSeq value in response, then
437433 //* use this value for the next run of the loop
438434 //* - if waitForDbChanges() call fails with timeout or retryalbe error,
439435 //* then re-run with same seq value as in the previous loop cycle
@@ -454,7 +450,7 @@ async function doListen() {
454450
455451 //
456452 // Response exist when waitForDbChanges returns OK
457- // If response.result.results == 0 = { "results": [] , "last_seq " : value } means timeout occur
453+ // If response.result.results == 0 = { "results": [] , "lastSeq " : value } means timeout occur
458454 // If response = <value> Doc change received ( feed.on(data,..))
459455 if ( Object . keys ( response ) . length === 0 ) {
460456 logger . info ( "Cloudant-SDK provided an unexpected empty response object on postChanges() call. Continue listening" ) ;
@@ -464,15 +460,15 @@ async function doListen() {
464460
465461 if ( ! response || ! response . result ) {
466462 sinceToken = "now" ;
467- logger . info ( `Got Ok postChanges result, but not a valid last_seq value. Start fresh by pulling only new changes.` ) ;
463+ logger . info ( `Got Ok postChanges result, but not a valid lastSeq value. Start fresh by pulling only new changes.` ) ;
468464 continue ; // run waitForDbChanges with new since token
469465 }
470466
471- if ( Array . isArray ( response . result . results ) && response . result . results . length === 0 && response . result . last_seq ) {
467+ if ( Array . isArray ( response . result . results ) && response . result . results . length === 0 && response . result . lastSeq ) {
472468 //
473469 // Wait timed out and delivered the lastSeq value as start point for the next loop
474- sinceToken = response . result . last_seq ;
475- lastHandledSeq = response . result . last_seq ;
470+ sinceToken = response . result . lastSeq ;
471+ lastHandledSeq = response . result . lastSeq ;
476472 logger . info ( `No changes detected in the given wait period. Assigned new since token from result.` ) ;
477473 continue ; // run waitForDbChanges with updated since token
478474 }
@@ -486,9 +482,9 @@ async function doListen() {
486482 } ) ;
487483
488484 //
489- // Get the last_seq value to use in the next postChanges() query
490- sinceToken = response . result . last_seq ;
491- lastHandledSeq = response . result . last_seq ;
485+ // Get the lastSeq value to use in the next postChanges() query
486+ sinceToken = response . result . lastSeq ;
487+ lastHandledSeq = response . result . lastSeq ;
492488 continue ; // run waitForDbChanges with updated since token
493489 }
494490
@@ -497,7 +493,7 @@ async function doListen() {
497493 // valid lastSeq number then continue the wrapper loop with
498494 // since = now
499495 sinceToken = "now" ;
500- logger . info ( `Got Ok postChanges result, but not a valid last_seq value. Start fresh by pulling only new changes.` ) ;
496+ logger . info ( `Got Ok postChanges result, but not a valid lastSeq value. Start fresh by pulling only new changes.` ) ;
501497 continue ;
502498 } catch ( err ) {
503499 // Handle how to proceed loop in case of WRAPPER_TIMEOUT
@@ -512,7 +508,7 @@ async function doListen() {
512508 continue ;
513509 }
514510
515- logger . error ( "Error in waitForDbChanges loop, err = " , err ) ;
511+ logger . error ( "Error in waitForDbChanges loop" , err ) ;
516512
517513 // In case of unexpected error occur, then continue
518514 // with wrapper loop and since = 'now'
0 commit comments