add logging to server

This commit is contained in:
Maxime Cannoodt 2022-07-06 14:59:38 +02:00
parent 50aac161c4
commit 1a624f38b9
4 changed files with 575 additions and 1110 deletions

11
server/logger.ts Normal file
View File

@ -0,0 +1,11 @@
import { createWriteStream } from "fs";
import pino, { multistream } from "pino";
// const streams = [
// { stream: process.stdout },
// { stream: createWriteStream("/logs/logfile.log", { flags: "a" }) },
// ];
// export default pino({}, multistream(streams));
export default pino({});

1645
server/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -7,17 +7,19 @@
"test": "echo 'No tests set up for the server.'",
"coverage": "echo 'No coverage set up for the server.'",
"build": "npx tsc",
"dev": "npx nodemon ./server.ts"
"dev": "npx nodemon ./server.ts | npx pino-pretty"
},
"author": "Maxime Cannoodt (mcndt)",
"license": "MIT",
"dependencies": {
"@prisma/client": "^4.0.0",
"cors": "^2.8.5",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"express-rate-limit": "^6.4.0",
"helmet": "^5.1.0",
"pino": "^8.1.0",
"pino-http": "^8.1.1",
"pino-pretty": "^8.1.0",
"sqlite3": "^5.0.8"
},
"devDependencies": {

View File

@ -1,16 +1,25 @@
import "dotenv/config";
import express, { Express, Request } from "express";
import cors from "cors";
import { PrismaClient, EncryptedNote } from "@prisma/client";
import { addDays } from "./util";
import helmet from "helmet";
import rateLimit from "express-rate-limit";
import pinoHttp from "pino-http";
import logger from "./logger";
// Initialize middleware clients
const prisma = new PrismaClient();
const app: Express = express();
app.use(express.json());
// configure logging
app.use(
pinoHttp({
logger: logger,
})
);
// configure Helmet and CORS
app.use(
helmet({
crossOriginResourcePolicy: {
@ -29,7 +38,7 @@ const postLimiter = rateLimit({
// start the Express server
app.listen(process.env.PORT, () => {
console.log(`server started at port ${process.env.PORT}`);
logger.info(`server started at port ${process.env.PORT}`);
});
// Post new encrypted note
@ -78,7 +87,7 @@ const interval =
1000;
setInterval(async () => {
try {
console.log("[Cleanup] Cleaning up expired notes...");
logger.info("[Cleanup] Cleaning up expired notes...");
const deleted = await prisma.encryptedNote.deleteMany({
where: {
expire_time: {
@ -86,9 +95,9 @@ setInterval(async () => {
},
},
});
console.log(`[Cleanup] Deleted ${deleted.count} expired notes.`);
logger.info(`[Cleanup] Deleted ${deleted.count} expired notes.`);
} catch (err) {
console.error(`[Cleanup] Error cleaning expired notes:`);
console.error(err);
logger.error(`[Cleanup] Error cleaning expired notes:`);
logger.error(err);
}
}, interval);