morgan-mongo
v1.1.0
Published
Node.js HTTP request logger middleware for Express with MongoDB as storage; morgan and Mongoose based
Downloads
12
Maintainers
Readme
morgan-mongo
Node.js HTTP request logger middleware for Express with MongoDB as storage; morgan and Mongoose based.
Inspired by Storing Log Data MongoDB article.
Built-in support for string, numeric, date and user agent tokens parsing.
Highly configurable output with meaningful defaults; support for standart mongoose and morgan options.
Installation
npm install morgan-mongo --save
Usage
import { morganMongoMiddleware } from 'morgan-mongo';
const app = express();
...
app.use(morganMongoMiddleware());
Output sample
{
"_id" : ObjectId("5c012d5375bad213309ad4c3"),
"userAgent" : {
"family" : "Chrome",
"major" : 70,
"minor" : 0,
"patch" : 3538,
"source" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36"
},
"date" : ISODate("2018-11-30T12:30:11.814Z"),
"httpVersion" : "1.1",
"method" : "GET",
"remoteAddr" : "::1",
"responseTime" : 92.56,
"status" : 200,
"url" : "/quotes?date=2018-11-28"
}
API
morganMongoMiddleware(options?, connectionOptions?, schemaOptions?, morganOptions?)
options
Custom options to provide MongoDB connection string and to control entries parsing.
export type OptionsType = {
connectionString?: string;
includeOnly?: string[];
exclude?: string[];
augment?: MappingDescriptor;
customMapping?: MappingDescriptor;
};
connectionString
: mongo connection string, defaults to mongodb://localhost:27017/morgan-mongo
.
If you use MongoDB Atlas (i.e. connection string with mongodb+srv
protocol schema), dbName
must be additionally provided in connectionOptions
.
Usage samples of other options can be found in tests. MappingDescriptor is described in Custom Mapping section below..
connectionOptions
mongoose ConnectionOptions
schemaOptions
mongoose SchemaOptions
morganOptions
morgan Options
Custom mappings
This section is subject for improvements. Feel free to open issus in case of any questions.
MappingDescriptor
Describes mappings from morgan tokens to properties in mongo document:
export type MappingDescriptor = {
[tokenName: string]: MappingMeta<any>
};
Default mappings are described by defaultMappingDescriptor.
MappingMeta
Describes mapping of single morgan token to property in mongo document:
export type MappingMeta<T> = {
prop: string,
type?: typeof mongoose.SchemaType | mongoose.SchemaDefinition,
params?: any[],
handler?: Handler<T>
};
prop
: mongo document property nametype
: mongoose SchemaType. Ifhandler
is not provided explicitly, default type handler will be used to handle String, Number, Dates and custom user agent type. If type is omitted token value will be unchanged string.params
: morgan token parameters. In case of having multiple same tokens with different parameters to keep uniqueness of keys in MappingDescriptor, parameters can be passed as part of token name there:
{
'req:cache-control': {
prop: 'cacheControl' // maps 'Cache-Control' request header to cacheControl property in mongo document
},
'req:content-type': {
prop: 'contentType' // maps 'Content-Type' request header to contentType property in mongo document
}
}
handler
: optional custom processing of token value (string) to any desired output type
Contribution
Feel free to contribute by opening issues with any questions, bug reports or feature requests.
Credits
mongoose-morgan - entries are not parsed but stored in mongo as strings.