1.1.1 • Published 9 months ago

mongoose-execution-time v1.1.1

Weekly downloads
114
License
MIT
Repository
github
Last release
9 months ago

Mongoose execution time

Mongoose plugin for measuring and logging query execution time.

npm i mongoose-execution-time

Output example:

Query: find in customers completed in: 7 ms { filter: {} }

Query: find in blogposts completed in: 4 ms { filter: { title: 'Post 1' },
  additionalLogProperties: { bruh: 1 } }

Query: findOne in blogposts completed in: 4 ms { filter: {} }

Query: estimatedDocumentCount in blogposts completed in: 3 ms { filter: {} }

Query: aggregate in blogposts completed in: 1 ms { aggregatePipeline: '[{"$match":{"title":"Post 1"}}]' }

Query: aggregate in blogposts completed in: 1 ms {
  aggregatePipeline: '[{"$match":{"title":"Post 1"}},{"$project":{"title":1}}]'
}

How to use

const mongoose = require('mongoose');
const { logExecutionTime } = require('mongoose-execution-time');

mongoose.plugin(logExecutionTime);

Configuration

The plugin can be easily adjusted via the following configuration options.

interface LogExecutionTimeConfig {
    logger?: any;
    loggerLevel?: string;
    loggerVerbosity?: LoggerVerbosity;
    loggerFunction?: LoggerFunction;
}
OptionDescriptionDefault
loggerlogger providerconsole
loggerLevellogger level used by the logger abovedebug
loggerVerbositycontrols how much information gets loggedHigh
loggerFunctionfor full control over how the log gets writtenN/A

Code example:

const mongoose = require('mongoose');
const { logExecutionTime } = require('mongoose-execution-time');

mongoose.plugin(logExecutionTime, {
    loggerLevel: 'info'
});

Do not want filter/aggregatePipeline information logged?

Simply set the loggerVerbosity to LoggerVerbosity.Normal in the plugin configuration.

const mongoose = require('mongoose');
const { logExecutionTime, LoggerVerbosity } = require('mongoose-execution-time');

mongoose.plugin(logExecutionTime, {
    loggerVerbosity: LoggerVerbosity.Normal,
    loggerLevel: 'info'
});

Logging additional information

The plugin exposes a method for logging additional information in the same log line as the execution time.

Code example:

await BlogPostModel.find({ title: 'Title' }).additionalLogProperties({ message: 'My custom message'});

Output example:

Query: find in blogposts completed in: 8 ms { additionalLogProperties: { message: 'My custom message' } }

Log message configuration

If the default logger formatting or style doesn't fit your needs, it can be adjusted by providing a custom logger function.

Code example:

const mongoose = require('mongoose');
const { logExecutionTime } = require('mongoose-execution-time');

mongoose.plugin(logExecutionTime, {
    loggerFunction: (operation, collectionName, executionTimeMS, filter, update, additionalLogProperties, aggregationPipeline) => {
        console.log(`My custom logger function | ${operation} | ${collectionName} | ${executionTimeMS}`, { filter, update, additionalLogProperties, aggregationPipeline })
    }
});

Output example:

My custom logger function | find | blogposts | 4 {
  filter: { title: 'Post 1' },
  update: undefined,
  additionalLogProperties: { bruh: 1 }
  aggregationPipeline: null
}