feat(framework): Improve production structured logging with more valuable information as well (#11489)
* feat(framework): Improve production structured logging with more valuable information as well * feat(framework): Improve production structured logging with more valuable information as well * feat(framework): Improve production structured logging with more valuable information as well * feat(framework): Improve production structured logging with more valuable information as well * fix typo * update trust proxy * Create new-ligers-heal.md * revert trust proxy
This commit is contained in:
1 parent
91d3332f9e
commit
3b7856e8f5
2 files changed
+55
-12
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/framework": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat(framework): Improve production structured logging with more valuable information as well
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import createStore from "connect-redis"
|
import createStore from "connect-redis"
|
||||||
import cookieParser from "cookie-parser"
|
import cookieParser from "cookie-parser"
|
||||||
import express, { Express } from "express"
|
import express, { Express, RequestHandler } from "express"
|
||||||
import session from "express-session"
|
import session from "express-session"
|
||||||
import Redis from "ioredis"
|
import Redis from "ioredis"
|
||||||
import morgan from "morgan"
|
import morgan from "morgan"
|
||||||
@@ -68,25 +68,63 @@ export async function expressLoader({ app }: { app: Express }): Promise<{
|
|||||||
*/
|
*/
|
||||||
function shouldSkipHttpLog(req: MedusaRequest, res: MedusaResponse) {
|
function shouldSkipHttpLog(req: MedusaRequest, res: MedusaResponse) {
|
||||||
return (
|
return (
|
||||||
isTest || NOISY_ENDPOINTS_CHUNKS.some((chunk) => req.url.includes(chunk))
|
isTest ||
|
||||||
|
NOISY_ENDPOINTS_CHUNKS.some((chunk) => req.url.includes(chunk)) ||
|
||||||
|
!logger.shouldLog("http")
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let loggingMiddleware: RequestHandler
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The middleware to use for logging. We write the log messages
|
* The middleware to use for logging. We write the log messages
|
||||||
* using winston, but rely on morgan to hook into HTTP requests
|
* using winston, but rely on morgan to hook into HTTP requests
|
||||||
*/
|
*/
|
||||||
const loggingMiddleware = morgan(
|
if (!IS_DEV) {
|
||||||
IS_DEV
|
const jsonFormat = (tokens, req, res) => {
|
||||||
? ":method :url ← :referrer (:status) - :response-time ms"
|
const result = {
|
||||||
: "combined",
|
level: "http",
|
||||||
{
|
// client ip
|
||||||
skip: shouldSkipHttpLog,
|
client_ip: req.ip || "-",
|
||||||
stream: {
|
|
||||||
write: (message: string) => logger.http(message.trim()),
|
// Request ID can be correlated with other logs (like error reports)
|
||||||
},
|
request_id: req.requestId || "-",
|
||||||
|
|
||||||
|
// Standard HTTP request properties
|
||||||
|
http_version: tokens["http-version"](req, res),
|
||||||
|
method: tokens.method(req, res),
|
||||||
|
path: tokens.url(req, res),
|
||||||
|
|
||||||
|
// Response details
|
||||||
|
status: Number(tokens.status(req, res)),
|
||||||
|
response_size: tokens.res(req, res, "content-length") || 0,
|
||||||
|
request_size: tokens.req(req, res, "content-length") || 0,
|
||||||
|
duration: Number(tokens["response-time"](req, res)),
|
||||||
|
|
||||||
|
// Useful headers that might help in debugging or tracing
|
||||||
|
referrer: tokens.referrer(req, res) || "-",
|
||||||
|
user_agent: tokens["user-agent"](req, res),
|
||||||
|
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
}
|
||||||
|
|
||||||
|
return JSON.stringify(result)
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
loggingMiddleware = morgan(jsonFormat, {
|
||||||
|
skip: shouldSkipHttpLog,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
loggingMiddleware = morgan(
|
||||||
|
":method :url ← :referrer (:status) - :response-time ms",
|
||||||
|
{
|
||||||
|
skip: shouldSkipHttpLog,
|
||||||
|
stream: {
|
||||||
|
write: (message: string) => logger.http(message.trim()),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
app.use(loggingMiddleware)
|
app.use(loggingMiddleware)
|
||||||
app.use(cookieParser())
|
app.use(cookieParser())
|
||||||
|
|||||||
Reference in new issue
Block a user