mirror of
https://github.com/wassname/talk.git
synced 2026-09-09 11:38:08 +08:00
feat: improved logging (#2719)
This commit is contained in:
@@ -40,6 +40,7 @@ const persistedQueryMiddleware = ({
|
||||
) {
|
||||
throw new RawQueryNotAuthorized(
|
||||
tenant.id,
|
||||
body && body.query ? body.query : null,
|
||||
req.user ? req.user.id : null
|
||||
);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,16 @@ import {
|
||||
ErrorRequestHandler,
|
||||
RequestHandler,
|
||||
} from "coral-server/types/express";
|
||||
import { Request, Response } from "express";
|
||||
|
||||
const extractMetadata = (req: Request, res: Response) => ({
|
||||
url: req.originalUrl || req.url,
|
||||
method: req.method,
|
||||
statusCode: res.statusCode,
|
||||
host: req.hostname,
|
||||
userAgent: req.get("User-Agent"),
|
||||
ip: req.ip,
|
||||
});
|
||||
|
||||
export const accessLogger: RequestHandler = (req, res, next) => {
|
||||
const startTime = now();
|
||||
@@ -14,24 +24,11 @@ export const accessLogger: RequestHandler = (req, res, next) => {
|
||||
// Compute the end time.
|
||||
const responseTime = Math.round(now() - startTime);
|
||||
|
||||
// Get some extra goodies from the request.
|
||||
const userAgent = req.get("User-Agent");
|
||||
|
||||
// Grab the logger.
|
||||
const log = req.coral ? req.coral.logger : logger;
|
||||
|
||||
// Log this out.
|
||||
log.debug(
|
||||
{
|
||||
url: req.originalUrl || req.url,
|
||||
method: req.method,
|
||||
statusCode: res.statusCode,
|
||||
host: req.hostname,
|
||||
userAgent,
|
||||
responseTime,
|
||||
},
|
||||
"http request"
|
||||
);
|
||||
log.debug({ ...extractMetadata(req, res), responseTime }, "http request");
|
||||
});
|
||||
|
||||
next();
|
||||
@@ -42,7 +39,7 @@ export const errorLogger: ErrorRequestHandler = (err, req, res, next) => {
|
||||
const log = req.coral ? req.coral.logger : logger;
|
||||
|
||||
// Log this out.
|
||||
log.error({ err }, "http error");
|
||||
log.error({ ...extractMetadata(req, res), err }, "http error");
|
||||
|
||||
next(err);
|
||||
};
|
||||
|
||||
@@ -749,10 +749,11 @@ export class PersistedQueryNotFound extends CoralError {
|
||||
}
|
||||
|
||||
export class RawQueryNotAuthorized extends CoralError {
|
||||
constructor(tenantID: string, userID: string | null) {
|
||||
constructor(tenantID: string, query: string | null, userID: string | null) {
|
||||
super({
|
||||
code: ERROR_CODES.RAW_QUERY_NOT_AUTHORIZED,
|
||||
context: { tenantID, pvt: { userID } },
|
||||
status: 400,
|
||||
context: { tenantID, pvt: { userID, query } },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -237,6 +237,9 @@ export function onOperation(options: OnOperationOptions) {
|
||||
) {
|
||||
throw new RawQueryNotAuthorized(
|
||||
params.context.tenant.id,
|
||||
message.payload && message.payload.query
|
||||
? message.payload.query
|
||||
: null,
|
||||
params.context.user ? params.context.user.id : null
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user