Add logging options

This commit is contained in:
Jesse Wierzbinski 2023-09-14 17:21:38 -10:00
parent aa1ffe5b9b
commit 76b1b8c3b4
No known key found for this signature in database
GPG key ID: F9A1E418934E40B0
3 changed files with 66 additions and 1 deletions

2
.gitignore vendored
View file

@ -2,7 +2,7 @@
# Logs
logs
logs/*
_.log
npm-debug.log_
yarn-debug.log*

View file

@ -1,4 +1,5 @@
import { getConfig } from "@config";
import { appendFile } from "fs/promises";
import "reflect-metadata";
import { AppDataSource } from "~database/datasource";
@ -10,6 +11,12 @@ const router = new Bun.FileSystemRouter({
console.log("[+] Starting FediProject...");
const config = getConfig();
const requests_log = Bun.file(process.cwd() + "/logs/requests.log");
if (!(await requests_log.exists())) {
console.log("[+] requests.log does not exist, creating it...");
await Bun.write(process.cwd() + "/logs/requests.log", "");
}
if (!AppDataSource.isInitialized) await AppDataSource.initialize();
@ -17,6 +24,38 @@ Bun.serve({
port: config.http.port,
hostname: config.http.base_url || "0.0.0.0", // defaults to "0.0.0.0"
async fetch(req) {
if (config.logging.log_requests_verbose) {
await appendFile(
`${process.cwd()}/logs/requests.log`,
`[${new Date().toISOString()}] ${req.method} ${
req.url
}\n\tHeaders:\n`
);
// Add headers
const headers = req.headers.entries();
for (const [key, value] of headers) {
await appendFile(
`${process.cwd()}/logs/requests.log`,
`\t\t${key}: ${value}\n`
);
}
const body = await req.clone().text();
await appendFile(
`${process.cwd()}/logs/requests.log`,
`\tBody:\n\t${body}\n`
);
} else if (config.logging.log_requests) {
await appendFile(
process.cwd() + "/logs/requests.log",
`[${new Date().toISOString()}] ${req.method} ${req.url}\n`
);
}
const matchedRoute = router.match(req);
if (matchedRoute) {

View file

@ -34,6 +34,20 @@ export interface ConfigType {
activitypub: {
use_tombstones: boolean;
};
filters: {
note_filters: string[];
username_filters: string[];
displayname_filters: string[];
bio_filters: string[];
emoji_filters: string[];
};
logging: {
log_requests: boolean;
log_requests_verbose: boolean;
log_filters: boolean;
};
[key: string]: unknown;
}
@ -115,6 +129,18 @@ export const configDefaults: ConfigType = {
activitypub: {
use_tombstones: true,
},
filters: {
note_filters: [],
username_filters: [],
displayname_filters: [],
bio_filters: [],
emoji_filters: [],
},
logging: {
log_requests: false,
log_requests_verbose: false,
log_filters: true,
},
};
export const getConfig = () => {