Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
added logging feature
Browse files Browse the repository at this point in the history
  • Loading branch information
kaxada committed Mar 24, 2023
1 parent 86768ed commit b846187
Show file tree
Hide file tree
Showing 5 changed files with 563 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ node_modules
npm-debug.log
*.pem
.env
test.js
logs
31 changes: 28 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ const { App } = require("octokit");
require("dotenv").config();
const githubBot = require("./githubBot");
const express = require("express");
const logger = require('./utils/logger');
const path = require("path");
const fs = require("fs");

const app = express();
app.use(express.json());
Expand All @@ -17,14 +20,36 @@ const bot = new App({
webhooks: { secret: process.env.webhookSecret },
});

// Middleware function to log response details
app.use((req, res, next) => {
const oldSend = res.send;
res.send = function (data) {
logger.info(`Response for ${req.method} ${req.url}:`);
logger.info(data);
oldSend.apply(res, arguments);
};
next();
});

app.post('/', async (req, res) => {
const { headers: { 'x-github-event': name }, body: payload } = req;
const octokit = await bot.getInstallationOctokit(payload.installation.id);
githubBot(name, octokit, payload);
logger.info(`Received ${name} event from Github`);
res.send('ok');
});

app.listen(process.env.PORT, () =>
console.info(`App listening on PORT:${process.env.PORT}`)
);
app.get('/logs', (req, res) => {
const logFile = path.join(__dirname, 'logs/app.log.2023-03-24');
fs.readFile(logFile, 'utf8', (err, data) => {
if (err) {
logger.error(err);
return res.status(500).send('Error reading log file');
}
res.send(`<pre>${data}</pre>`);
});
});

app.listen(process.env.PORT, () =>
logger.info(`App listening on PORT:${process.env.PORT}`)
);
Loading

0 comments on commit b846187

Please sign in to comment.