1
mirror of https://github.com/thepeacockproject/Peacock synced 2024-11-03 12:49:22 +01:00

Added additional logging for debugging purposes (#183)

This commit is contained in:
Lennard Fonteijn 2023-04-06 01:21:12 +02:00 committed by GitHub
parent 1e6e9a8c11
commit 02f1fbbc00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 3 deletions

View File

@ -93,6 +93,10 @@ const defaultFlags: Flags = {
desc: "[Development] When set to true, it will be possible to restart Peacock while the game is running and connected.",
default: false,
},
developmentLogRequests: {
desc: "[Development] When set to true, will log the body of all requests the game makes. This can cause huge log files!",
default: false,
},
legacyContractDownloader: {
desc: "When set to true, the official servers will be used for contract downloading in H3, which only works for the platform you are playing on. When false, the HITMAPS servers will be used instead. Note that this option only pertains to H3. Official servers will be used for H1 and H2 regardless of the value of this option.",
default: false,

View File

@ -47,7 +47,13 @@ import type {
} from "./types/types"
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs"
import { join } from "path"
import { log, loggingMiddleware, LogLevel } from "./loggingInterop"
import {
errorLoggingMiddleware,
log,
loggingMiddleware,
LogLevel,
requestLoggingMiddleware,
} from "./loggingInterop"
import { eventRouter } from "./eventHandler"
import { contractRoutingRouter } from "./contracts/contractRouting"
import { profileRouter } from "./profileHandler"
@ -125,6 +131,11 @@ process.on("uncaughtException", uncaught)
const app = express()
app.use(loggingMiddleware)
if (getFlag("developmentLogRequests")) {
app.use(requestLoggingMiddleware)
}
app.use("/_wf", webFeaturesRouter)
app.get("/", (req: Request, res) => {
@ -485,6 +496,8 @@ app.all("*", (req, res) => {
res.status(404).send("Not found!")
})
app.use(errorLoggingMiddleware)
program.description(
"The Peacock Project is a HITMAN™ World of Assassination Trilogy server built for general use.",
)

View File

@ -179,10 +179,10 @@ export function log(
category: LogCategory | string = LOG_CATEGORY_DEFAULT,
): void {
if (category === LogCategory.CALLER) {
category = log.caller?.name.toString() ?? "unknown"
category = log.caller?.name.toString() || "unknown"
}
const message = data ?? "No message specified"
const message = data || "No message specified"
const now = new Date()
const stampParts: number[] = [
@ -271,3 +271,51 @@ export function loggingMiddleware(
)
next?.()
}
export function requestLoggingMiddleware(
req: RequestWithJwt,
res: Response,
next?: NextFunction,
): void {
res.once("finish", () => {
const debug = {
method: req.method,
url: req.url,
body: req.body,
statusCode: res.statusCode,
statusMessage: res.statusMessage,
}
log(LogLevel.DEBUG, JSON.stringify(debug), LogCategory.HTTP)
})
next?.()
}
export function errorLoggingMiddleware(
err: Error,
req: RequestWithJwt,
res: Response,
next?: NextFunction,
): void {
const debug = {
method: req.method,
url: req.url,
body: req.body,
error: `${err.name} - ${err.message} - ${
err.cause || "Unknown cause"
}\n${err.stack || "No stack"}`,
}
log(
LogLevel.ERROR,
`${picocolors.green(req.method)} ${picocolors.underline(
req.url,
)} gave an unexpected error! Please see log for details.`,
LogCategory.HTTP,
)
log(LogLevel.DEBUG, JSON.stringify(debug), LogCategory.HTTP)
next?.()
}