master
TheDude 1 year ago
parent f90f47aa54
commit 742c543600
  1. 4
      package.json
  2. 19
      src/config/logger.js
  3. 9
      src/index.js
  4. 30
      src/middlewares/loggingMiddleware.js

@ -24,7 +24,9 @@
"dependencies": { "dependencies": {
"axios": "^0.21.1", "axios": "^0.21.1",
"dotenv": "^10.0.0", "dotenv": "^10.0.0",
"express": "^4.17.1" "express": "^4.17.1",
"winston": "^latest_version",
"express-winston": "^latest_version"
}, },
"devDependencies": { "devDependencies": {
"chai": "^4.3.8", "chai": "^4.3.8",

@ -0,0 +1,19 @@
import winston from 'winston';
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' })
]
});
// If we're not in production, log to the `console` with the format:
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
if (process.env.NODE_ENV !== 'production') {
logger.add(new winston.transports.Console({
format: winston.format.simple()
}));
}

@ -2,10 +2,14 @@
import express from 'express'; import express from 'express';
import axios from 'axios'; import axios from 'axios';
import crypto from 'crypto'; import crypto from 'crypto';
import express from 'express';
import { logger } from './config/logger';
import { requestLogger, errorLogger } from './middlewares/loggingMiddleware';
import dotenv from 'dotenv'; import dotenv from 'dotenv';
dotenv.config(); dotenv.config();
const app = express(); const app = express();
app.use(requestLogger);
const PORT = 3000; const PORT = 3000;
// Access environment variables // Access environment variables
@ -156,8 +160,11 @@ app.post('/handleWebhook', async (req, res) => {
if (!transactionSuccess) { if (!transactionSuccess) {
console.error(`Failed to process transaction after ${MAX_RETRIES} retries.`); console.error(`Failed to process transaction after ${MAX_RETRIES} retries.`);
} }
app.use(errorLogger);
}); });
app.listen(PORT, () => { app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`); logger.info(`Server is running on http://localhost:${PORT}`);
}); });

@ -0,0 +1,30 @@
import expressWinston from 'express-winston';
// Request logger middleware
app.use(expressWinston.logger({
transports: [
new winston.transports.Console()
],
format: winston.format.combine(
winston.format.colorize(),
winston.format.json()
),
meta: true, // optional: control whether you want to log the meta data about the request (default to true)
msg: "HTTP {{req.method}} {{req.url}}", // optional: customize the default logging message. E.g. "{{res.statusCode}} {{req.method}} {{res.responseTime}}ms {{req.url}}"
expressFormat: true, // Use the default Express/morgan request formatting. Enabling this will override any msg if true. Will only output colors with colorize set to true
colorize: false, // Color the text and status code, using the Express/morgan color palette (default green, 3XX cyan, 4XX yellow, 5XX red).
ignoreRoute: function (req, res) { return false; } // optional: allows to skip some log messages based on request and/or response
}));
// Your routes go here
// Error logger middleware (should be after all your routes)
app.use(expressWinston.errorLogger({
transports: [
new winston.transports.Console()
],
format: winston.format.combine(
winston.format.colorize(),
winston.format.json()
)
}));
Loading…
Cancel
Save