Skip to content

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

Merged
seppevs merged 1 commit intoseppevs:masterfrom
kabalin:fix-console-log-replace
Jan 29, 2025
Merged

Fix up/down command console output when logger replaces it.#365
seppevs merged 1 commit intoseppevs:masterfrom
kabalin:fix-console-log-replace

Conversation

@kabalin
Copy link
Copy Markdown
Contributor

@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
Copy Markdown

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
Copy Markdown

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

@seppevs seppevs merged commit 547a7d4 into seppevs:master Jan 29, 2025
@kabalin kabalin deleted the fix-console-log-replace branch January 29, 2025 16:01
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.

4 participants