Skip to content

Commit

Permalink
Merge pull request #240 from telefonicaid/task/log_mongo_url_conectio…
Browse files Browse the repository at this point in the history
…n_attempt

log url and options in connection attempt
  • Loading branch information
fgalan authored Mar 10, 2021
2 parents 3d57405 + 878144b commit 76db700
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Add: db uri and options in mongo connection log INFO trace
- Fix: log about getProtocol result
- Fix: ensure protocol exists before remove it (#234)
- Fix: print URI in logs about redirection error (#232)
Expand Down
33 changes: 18 additions & 15 deletions lib/model/dbConn.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ function init(host, db, port, username, password, options, callback) {
return previous;
}

const hosts = host
.split(',')
.map(addPort)
.reduce(commaConcat, '');
const hosts = host.split(',').map(addPort).reduce(commaConcat, '');

url = 'mongodb://' + credentials + hosts + '/' + db;

Expand All @@ -105,46 +102,52 @@ function init(host, db, port, username, password, options, callback) {
}

function connectionAttempt(url, options, callback) {
logger.info(context, 'Attempting to connect to MongoDB instance. Attempt %d', retries);
logger.info(
context,
'Attempting to connect to MongoDB instance with url %j and options %j. Attempt %d',
url,
options,
retries
);
// just to avoid warnings with recent mongoose versions
options.useNewUrlParser = true;
options.useUnifiedTopology = true;
mongoose.set('useCreateIndex', true);
/* eslint-disable-next-line no-unused-vars */
const candidateDb = mongoose.createConnection(url, options, function(error, result) {
const candidateDb = mongoose.createConnection(url, options, function (error, result) {
if (error) {
logger.error(context, 'MONGODB-001: Error trying to connect to MongoDB: %s', error);
lastError = error;
} else {
defaultDb = candidateDb;

defaultDb.on('error', function(error) {
defaultDb.on('error', function (error) {
logger.error(context, 'Mongo Driver error: %j', error);
});
/* eslint-disable-next-line no-unused-vars */
defaultDb.on('connecting', function(error) {
defaultDb.on('connecting', function (error) {
logger.debug(context, 'Mongo Driver connecting');
});
defaultDb.on('connected', function() {
defaultDb.on('connected', function () {
logger.debug(context, 'Mongo Driver connected');
});
defaultDb.on('reconnected', function() {
defaultDb.on('reconnected', function () {
logger.debug(context, 'Mongo Driver reconnected');
});
defaultDb.on('disconnected', function() {
defaultDb.on('disconnected', function () {
logger.debug(context, 'Mongo Driver disconnected');
});
defaultDb.on('reconnectFailed', function() {
defaultDb.on('reconnectFailed', function () {
logger.error(context, 'MONGODB-004: MongoDB connection was lost');
process.exit(1);
});
defaultDb.on('disconnecting', function() {
defaultDb.on('disconnecting', function () {
logger.debug(context, 'Mongo Driver disconnecting');
});
defaultDb.on('open', function() {
defaultDb.on('open', function () {
logger.debug(context, 'Mongo Driver open');
});
defaultDb.on('close', function() {
defaultDb.on('close', function () {
logger.debug(context, 'Mongo Driver close');
});
}
Expand Down

0 comments on commit 76db700

Please sign in to comment.