Mongoose: Turn off colors when mongoose debug is set to true

Created on 29 Apr 2018  Â·  7Comments  Â·  Source: Automattic/mongoose

How do we turn off colored debug output?
It adds extra colors data to the printed logs, which breaks our log parsing system

developer-experience

Most helpful comment

@tarun1793

You can pass a custom log function as the second parameter to mongoose.set() instead of a boolean.

A simple example:

6403.js

#!/usr/bin/env node
'use strict';

const mongoose = require('mongoose');
mongoose.set('debug', customLogger);

function customLogger(coll, op, doc, proj) {
  process.stdout.write(`${coll}.${op}(${JSON.stringify(doc)}`);
  if (proj) {
    process.stdout.write(',' + JSON.stringify(proj) + ')\n');
  } else {
    process.stdout.write(')\n');
  }
}

mongoose.connect('mongodb://localhost/test');
const conn = mongoose.connection;
const Schema = mongoose.Schema;

const schema = new Schema({
  name: String
});

const Test = mongoose.model('test', schema);

const test = new Test({
  name: 'Marilyn'
});

async function run() {
  await conn.dropDatabase;
  await test.save();
  await Test.findOne({ _id: test._id }, 'name');
  return conn.close();
}

run();

Output:

issues: ./6403.js
tests.insert({"_id":"5ae580410b18b35bd96440ff","name":"Marilyn","__v":0})
tests.findOne({"_id":"5ae580410b18b35bd96440ff"},{"fields":{"name":1}})
issues:

All 7 comments

@tarun1793

You can pass a custom log function as the second parameter to mongoose.set() instead of a boolean.

A simple example:

6403.js

#!/usr/bin/env node
'use strict';

const mongoose = require('mongoose');
mongoose.set('debug', customLogger);

function customLogger(coll, op, doc, proj) {
  process.stdout.write(`${coll}.${op}(${JSON.stringify(doc)}`);
  if (proj) {
    process.stdout.write(',' + JSON.stringify(proj) + ')\n');
  } else {
    process.stdout.write(')\n');
  }
}

mongoose.connect('mongodb://localhost/test');
const conn = mongoose.connection;
const Schema = mongoose.Schema;

const schema = new Schema({
  name: String
});

const Test = mongoose.model('test', schema);

const test = new Test({
  name: 'Marilyn'
});

async function run() {
  await conn.dropDatabase;
  await test.save();
  await Test.findOne({ _id: test._id }, 'name');
  return conn.close();
}

run();

Output:

issues: ./6403.js
tests.insert({"_id":"5ae580410b18b35bd96440ff","name":"Marilyn","__v":0})
tests.findOne({"_id":"5ae580410b18b35bd96440ff"},{"fields":{"name":1}})
issues:

Thanks for this workaround!

On Sun 29 Apr, 2018, 10:34 AM Kev, notifications@github.com wrote:

@tarun1793 https://github.com/tarun1793

You can pass a custom log function as the second parameter to
mongoose.set() instead of a boolean.

A simple example:
6403.js

!/usr/bin/env node'use strict';

const mongoose = require('mongoose');mongoose.set('debug', customLogger);
function customLogger(coll, op, doc, proj) {
process.stdout.write(${coll}.${op}(${JSON.stringify(doc)});
if (proj) {
process.stdout.write(',' + JSON.stringify(proj) + ')\n');
} else {
process.stdout.write(')\n');
}
}
mongoose.connect('mongodb://localhost/test');const conn = mongoose.connection;const Schema = mongoose.Schema;
const schema = new Schema({
name: String
});
const Test = mongoose.model('test', schema);
const test = new Test({
name: 'Marilyn'
});
async function run() {
await conn.dropDatabase;
await test.save();
await Test.findOne({ _id: test._id }, 'name');
return conn.close();
}
run();

Output:

issues: ./6403.js
tests.insert({"_id":"5ae580410b18b35bd96440ff","name":"Marilyn","__v":0})
tests.findOne({"_id":"5ae580410b18b35bd96440ff"},{"fields":{"name":1}})
issues:

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/Automattic/mongoose/issues/6403#issuecomment-385235354,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABs2kHc4tDueDtN2mbctXhg1CJN5yYqoks5ttXsRgaJpZM4TrpXK
.

happy to help 😄

I have the same question.
I tried custom log before and works well however when my project become bigger and bigger, I got JSON.stringify error sometimes.

If possible, I hope there is an option for debug color on/off.

@vkarpov15 If I should create a new request rather than leave a comment here pls let me know.

@Mangosteen-Yang no option currently. You would have to use a custom logger as shown here.

@vkarpov15 shouldnt #8033 close this issue?

@hasezoey yes it should. Thanks! To disable colors, you can do mongoose.set('debug', { color: false });

Was this page helpful?
0 / 5 - 0 ratings