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 99637e1
Show file tree
Hide file tree
Showing 5 changed files with 575 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
43 changes: 40 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,48 @@ 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 logsDir = path.join(__dirname, 'logs');
fs.readdir(logsDir, (err, files) => {
if (err) {
logger.error(err);
return res.status(500).send('Error reading logs directory');
}
const logFiles = files.filter(file => file.startsWith('app.log'));
if (logFiles.length === 0) {
return res.status(404).send('No log files found');
}
const logFile = path.join(logsDir, logFiles[0]);
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 99637e1

Please sign in to comment.