migrate-mongo
migrate-mongo copied to clipboard
Fix up/down command console output when logger replaces it.
Hello, this is tiny PR that fixes pretty unique issue.
I am replacing console with logger function, so that when I run up or down commands, the console output is logged using logger configuration (I am using log4js).
My config looks like:
const mongoConfig = require('./').mongo;
const argv = require('yargs').argv;
const logger = require('log4js').getLogger('migrate-mongo');
// Replace node console with logger for up and down migration commands, so we have a
// log record for these actions.
if (argv._.includes('up') || argv._.includes('down')) {
console.log = logger.info.bind(logger);
console.error = logger.error.bind(logger);
}
const config = {
mongodb: {
url: mongoConfig.connection,
options: {
useNewUrlParser: true, // removes a deprecation warning when connecting
useUnifiedTopology: true, // removes a deprecating warning when connecting
},
},
migrationsDir: 'migrations',
changelogCollectionName: 'changelog',
};
module.exports = config;
This all works fine, I am getting STDOUT output similar to:
[2021-05-26T11:17:00.087] [INFO] migrate-mongo - MIGRATED UP: 20210524112904-something_changed.js
Log4js is configured to use stdout and file appenders. The problem is that command output goes to standard output, but does not go to the file for unknown reason. Presumably this is related to the fact that process ends synchronously with console.log call in the code, and internally in node file writing stops too early, just guessing.
So, solution is simple. When I defer process.exit(0); to the next tick by moving it in separate then in the chain, my issue gets resolved. In fact, you are using the same pattern for status command, which I am not interested in logging, but noticed it logs correctly when I remove command condition in the config file.
Please consider accepting this PR 😃
Checklist
- [x]
npm testpasses and has 100% coverage
Coverage remained the same at 100.0% when pulling 9536a480bb9e7109a72871c7cb3b3ab8b4dfeb1f on kabalin:fix-console-log-replace into 4906ff23676ba3b2a9be219be64eb94f440db597 on seppevs:master.
I've included this PR in https://github.com/theogravity/migrate-mongo-alt