diff --git a/env/development.env b/env/development.env index c6ffa5a..082ab54 100644 --- a/env/development.env +++ b/env/development.env @@ -1,4 +1,5 @@ ## Server ## PORT=3000 NODE_ENV=development -DATABASE_CONNECTION_STRING=mongodb://localhost:27017 \ No newline at end of file +DATABASE_CONNECTION_STRING=mongodb://localhost:27017 +LOG_LEVEL=debug \ No newline at end of file diff --git a/src/config/mongoDb.ts b/src/config/mongoDb.ts index 1a0a67d..d885102 100644 --- a/src/config/mongoDb.ts +++ b/src/config/mongoDb.ts @@ -1,5 +1,7 @@ import mongoose from "mongoose"; import environment from "./environment.js"; +import logger from "../logging/logger.js"; +import * as util from "util"; const connect = async (): Promise => { await mongoose.connect(environment.DATABASE_CONNECTION_STRING, { @@ -7,4 +9,20 @@ const connect = async (): Promise => { }); }; +if (environment.LOG_LEVEL === "debug") { + // https://dev.to/sw360cab/mongoose-debug-messages-with-a-custom-logging-library-or-style-1hk4 + mongoose.set("debug", (collectionName, methodName, ...methodArgs) => { + const msgMapper = (m: any): string => { + return util + .inspect(m, false, 10, true) + .replace(/\n/g, "") + .replace(/\s{2,}/g, " "); + }; + logger.debug( + `\x1B[0;36mMongoose:\x1B[0m: ${collectionName}.${methodName}` + + `(${methodArgs.map(msgMapper).join(", ")})` + ); + }); +} + export default { connect }; diff --git a/src/logging/expressLogger.ts b/src/logging/expressLogger.ts index 41aa806..9d471af 100644 --- a/src/logging/expressLogger.ts +++ b/src/logging/expressLogger.ts @@ -1,7 +1,9 @@ import expressWinston from "express-winston"; import winston from "winston"; +import environment from "../config/environment.js"; const expressLogger = expressWinston.logger({ + level: environment.LOG_LEVEL, transports: [new winston.transports.Console()], format: winston.format.combine( winston.format.colorize(),