mongoose-execution-time
v1.1.1
Published
Mongoose plugin for measuring and logging query execution time
Downloads
11,174
Maintainers
Readme
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;
}
| Option | Description | Default | |---|---|---| | logger | logger provider | console | | loggerLevel | logger level used by the logger above | debug | | loggerVerbosity | controls how much information gets logged | High | | loggerFunction | for full control over how the log gets written | N/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
}