Skip to content

Commit

Permalink
Restore Backend Logging Framework w/ Winston MongoDB transport.
Browse files Browse the repository at this point in the history
  • Loading branch information
swoocn committed Apr 6, 2024
1 parent 05c2e59 commit 1c6cf17
Show file tree
Hide file tree
Showing 15 changed files with 346 additions and 120 deletions.
14 changes: 11 additions & 3 deletions backend/logger.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
const winston = require('winston');
const configureLogging = require('./logging-config');

const loggingConfig = configureLogging();
const logger = winston.createLogger(loggingConfig);
const initializeLogger = async () => {
try {
const loggingConfig = await configureLogging();
const logger = winston.createLogger(loggingConfig);
return logger;
} catch (error) {
console.error('Error initializing logger:', error);
return null;
}
};

module.exports = logger;
module.exports = initializeLogger;
35 changes: 27 additions & 8 deletions backend/logging-config.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,53 @@
const dotenv = require("dotenv");
const winston = require('winston');
const connectDB = require("./mongodb");

require('winston-mongodb');

dotenv.config();

const configureLogging = () => {
console.log(`VERCEL_ENV: ${process.env.VERCEL_ENV}`);
const configureLogging = async () => {
// console.log(`VERCEL_ENV: ${process.env.VERCEL_ENV}`);

// check if the app is running in localhost mode or sandbox
const isDevelopment = () => {
return process.env.VERCEL_ENV === "localhost" || process.env.VERCEL_ENV === "sandbox";
};

// determine the logging configuration based on the environment
// determine the logging configuration based on the server environment
if (isDevelopment()) {
return {
level: 'debug',
format: winston.format.cli(),
format: winston.format.combine(
winston.format.errors({ stack: true }),
winston.format.cli()
),
transports: [
new winston.transports.Console()
]
};
} else {
await connectDB();

return {
level: 'error',
format: winston.format.cli(),
level: 'info',
format: winston.format.combine(
winston.format.errors({ stack: true }),
winston.format.timestamp(), // GMT
winston.format.json()
),
transports: [
new winston.transports.Console()
new winston.transports.MongoDB({
db: process.env.MONGODB_URL,
options: {
useNewUrlParser: true,
useUnifiedTopology: true
},
collection: 'serverLogs'
})
]
};
}
}
};

module.exports = configureLogging;
16 changes: 16 additions & 0 deletions backend/mongodb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const mongoose = require("mongoose");
const dotenv = require("dotenv");

dotenv.config();

const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.MONGODB_URL);
// console.log(`Mongo DB Connection successfully attained: ${conn.connection.host}`);
return conn;
} catch (err) {
console.error("Mongo DB Connection failed", err);
}
};

module.exports = connectDB;

0 comments on commit 1c6cf17

Please sign in to comment.