-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restore Backend Logging Framework w/ Winston MongoDB transport.
- Loading branch information
Showing
15 changed files
with
346 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.