Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix up/down command console output when logger replaces it. #365

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kabalin
Copy link

@kabalin kabalin commented May 26, 2021

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
  • npm test passes and has 100% coverage

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 9536a48 on kabalin:fix-console-log-replace into 4906ff2 on seppevs:master.

@theogravity
Copy link

I've included this PR in https://github.com/theogravity/migrate-mongo-alt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants