-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeError: winston.transports.MongoDB is not a constructor #171
Comments
Any updates on this? I am having the same problem |
Hello @miktam, |
@KathiravanBalasubramanian nope, it's not the reason, unfortunately. |
Again, has there been any updates on this? Literally dropping of the face of the earth does not inspire confidence. Simply stating 'missing the connection string' is not helpful. |
@verbal-assassin do you use the latest version of Can you check that the issue is present with the latest version of |
Similar problem here:
import { createLogger, format, transports } from 'winston';
import 'winston-mongodb';
const { combine, timestamp, json, colorize,
prettyPrint, simple, errors,
} = format;
const finalMongoOptions = {
db: process.env.MONGO_URI,
level: process.env.LOGGING_LEVEL,
name: 'mongodb',
collection: process.env.MONGO_LOGGING_COLLECTION || 'logs',
decolorize: true,
tryReconnect: true,
options: {
useUnifiedTopology: true,
useNewUrlParser: true,
},
};
if (Number.isInteger(process.env.LOGS_EXPIRE_AFTER_DAYS)) {
finalMongoOptions.expireAfterSeconds = process.env.LOGS_EXPIRE_AFTER_DAYS;
}
const logger = createLogger({
// https://github.com/winstonjs/winston#logging-levels
level: process.env.LOGGING_LEVEL,
// https://github.com/winstonjs/winston#formats
format: combine(
// https://github.com/taylorhakes/fecha format
timestamp({ format: 'DD-MM-YY HH:mm:ss' }),
prettyPrint({ depth: 5 }),
json(),
errors({ stack: true }),
),
// https://github.com/winstonjs/winston#transports
transports: [
new transports.Console({
level: process.env.LOGGING_LEVEL || ERROR_LEVELS.debug.name,
format: combine(
colorize(),
simple()
),
}),
new transports.MongoDB(finalMongoOptions), // ERROR HERE
],
// https://github.com/winstonjs/winston#to-exit-or-not-to-exit
exitOnError: false,
});
export default logger; The issue occurs also with Node LTS (14.15.0) |
@chrisvoo Did you get any solution? |
Nope, at the moment to bypass that error I just did: const transports: any = winston.transports |
@saurabhsinghhauhan this for me works: import {
createLogger, format, transports, Logger,
} from 'winston';
import { MongoDB, MongoDBConnectionOptions } from 'winston-mongodb';
export type LogLevel = 'error' | 'warn' | 'info' | 'verbose' | 'debug' | 'silly'
export type BootrapWinstonOptions = {
mongoOptions: {
db: string
level?: LogLevel
collection?: string
options?: any // MongoClient's options
expireAfterSeconds?: number
}
exitOnError?: boolean
}
/**
* It creates a Logger instance
* @param {object} options It contains all the options used to build the logger.
* - `level (string)`: see `ERROR_LEVELS`, default "debug"
* @returns {Logger}
*/
export default function buildLogger(options: BootrapWinstonOptions): Logger {
const {
combine,
timestamp,
json,
colorize,
prettyPrint,
simple,
errors,
} = format;
const { mongoOptions } = options;
// see https://docs.mongodb.com/manual/reference/connection-string/
if (!mongoOptions?.db) {
throw new Error(
"'mongoOptions' is missing or it's missing its property 'db' (Mongo's connection string)",
);
}
const finalMongoOptions: MongoDBConnectionOptions = {
db: mongoOptions.db,
level: mongoOptions.level ?? 'debug',
name: 'mongodb',
collection: mongoOptions.collection ?? 'log',
decolorize: true,
tryReconnect: true,
options: mongoOptions.options ?? {
useUnifiedTopology: true,
useNewUrlParser: true,
},
};
if (Number.isInteger(mongoOptions.expireAfterSeconds)) {
finalMongoOptions.expireAfterSeconds = mongoOptions.expireAfterSeconds;
}
return createLogger({
level: finalMongoOptions.level,
format: combine(
// https://github.com/taylorhakes/fecha format
timestamp({ format: 'DD-MM-YY HH:mm:ss' }),
prettyPrint({ depth: 5 }),
json(),
errors({ stack: true }),
),
// https://github.com/winstonjs/winston#transports
transports: [
new transports.Console({
level: process.env.NODE_ENV === 'production' ? 'info' : 'debug',
format: combine(colorize(), simple()),
}),
new MongoDB(finalMongoOptions),
],
// https://github.com/winstonjs/winston#to-exit-or-not-to-exit
exitOnError:
typeof options.exitOnError === 'boolean'
? options.exitOnError
: false,
});
}
import { MongoClient } from 'mongodb';
import buildLogger from '../src/logger';
it('Creates the collection and insert the logs', (done) => {
const logger = buildLogger({
mongoOptions: {
db: 'mongodb://localhost:27017/test_mongolog',
expireAfterSeconds: 120,
},
exitOnError: true,
});
// eslint-disable-next-line no-unused-vars
logger.on('finish', (info) => {
const url = 'mongodb://localhost:27017';
const dbName = 'test_mongolog';
MongoClient.connect(url, { useUnifiedTopology: true }, (err, client) => {
if (err) {
throw err;
}
const testDb = client.db(dbName);
// we verify that we have one log in the last 5 seconds
testDb
.collection('log')
.find({
timestamp: {
$lt: new Date(),
$gte: new Date(Date.now() - 5000),
},
})
.toArray()
.then((docs) => {
expect(docs.length).toBe(1);
expect(docs[0].meta.key).toBe(5);
client.close();
done();
});
});
});
logger.info('A message', {
metadata: {
key: 5,
},
});
logger.end();
}); |
Can you check whether the issue persists with @5.0.6? |
winston@^3.3.3 import winston from 'winston'
import {MongoDB} from 'winston-mongodb'
import config from '../configs'
import { format, createLogger} from 'winston'
const {combine, timestamp, errors, json} = format;
const transports: any = winston.transports
const buildProdLog = ()=>{
return createLogger({
level: 'info',
format: combine(
timestamp({format : 'YYYY-MM-DD HH:mm:ss ZZ'}),
errors({stack : true}),
json()
),
defaultMeta:{service : 'user-service'},
transports: [
new transports.Console(),
new transports.File({filename : './LOGS/PROD.log'}),
new transports.MongoDB({
level : "info",
db : config.MONGO_URI,
options:{
ignoreUndefined: true,
useUnifiedTopology: true
},
collection : "production-logs"
})
],
});
}
export {buildProdLog} I am still facing this error new transports.MongoDB({
^
TypeError: transports.MongoDB is not a constructor |
I'm also facing this error as well |
@yurijmikhalevich How come this issue was closed? Was there ever a solution for it? |
@Kevin-Aaaquil, thanks for checking and providing the example. |
I have the same problem, but only when I have cloned the |
Update on this one - my mind's background processes couldn't let it go. I think it's something to do with the module dependencies, because sometimes it complained (after I had done I forked the repo as https://github.com/Back2bikes/winston-mongodb, and then installed it from there: In the dependencies section of package.json:
It installs and runs perfectly, so I have an element of success, but I still don't know why the |
I wonder if this has core cause in common with winstonjs/winston#1814 and if those having difficulty might have an issue in git setup? |
Did anyone eventually find a fix for this? |
a possible workaround that worked for us is adding the parentheses for
|
Here's my code snippet: import { transports, format, createLogger } from "winston";
import("winston-mongodb");
import { config } from "dotenv";
config();
console.log(process.env.DB_URI);
export const usersLogger = createLogger({
format: format.combine(format.timestamp()),
transports: [
new transports.File({
filename: "logs/users-error.log",
level: "error",
}),
new transports.MongoDB({
level: "error",
db: process.env.DB_URI,
options: {
useUnifiedTopology: true,
},
collection: "logs",
format: format.combine(
format.timestamp(),
// Convert logs to a json format
format.json()
),
}),
],
});
export const reportsLogger = createLogger({
format: format.combine(format.timestamp()),
transports: [
new transports.File({
filename: "logs/users-error.log",
level: "error",
}),
new transports.MongoDB({
level: "error",
db: process.env.DB_URI,
options: {
useUnifiedTopology: true,
},
collection: "logs",
format: format.combine(
format.timestamp(),
// Convert logs to a json format
format.json()
),
}),
],
});
I'm new to logging and somehow I fail to see how I can integrate your suggestion here. |
So, I found a fix that works for me. import { transports, format, createLogger } from "winston";
import("winston-mongodb");
import { config } from "dotenv";
import { MongoDB } from "winston-mongodb"; // <-- THIS WAS THE MAIN DIFFERENCE
config();
const mongoOptions = {
db: process.env.DB_URI,
level: "error",
options: {
useUnifiedTopology: true,
useNewUrlParser: true,
},
collection: "logs",
format: format.combine(
format.timestamp(),
format.json()
),
};
export const logger = createLogger({
format: format.combine(format.timestamp()),
transports: [
new transports.File({
filename: "logs/users-error.log",
level: "error",
}),
new MongoDB(mongoOptions), // <-- This replaced "new transports.MongoDB({options})"
],
}); |
Hi, here is how i was able to get mine working while still using types/interfaces
|
Your solution worked for me too, thanks! |
When trying to instantiate following error is thrown
TypeError: winston.transports.MongoDB is not a constructor
Source code:
Version of the library:
"winston-mongodb": "^5.0.1"
The text was updated successfully, but these errors were encountered: