feature: use application logger to log HTTP requests (#9655)
This commit is contained in:
@@ -18,7 +18,9 @@ if (!IS_DEV) {
|
||||
transports.push(
|
||||
new winston.transports.Console({
|
||||
format: winston.format.combine(
|
||||
winston.format.cli(),
|
||||
winston.format.cli({
|
||||
levels: winston.config.npm.levels,
|
||||
}),
|
||||
winston.format.splat()
|
||||
),
|
||||
})
|
||||
@@ -169,6 +171,7 @@ export class Reporter {
|
||||
* @param {String | Error} messageOrError - can either be a string with a
|
||||
* message to log the error under; or an error object.
|
||||
* @param {Error?} error - an error object to log message with
|
||||
* Level 0
|
||||
*/
|
||||
error(messageOrError: string | Error, error?: Error) {
|
||||
let message = messageOrError as string
|
||||
@@ -273,9 +276,22 @@ export class Reporter {
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs a message at the silly level.
|
||||
* @param {string} message - the message to log
|
||||
* Level 6
|
||||
*/
|
||||
silly(message: string) {
|
||||
this.loggerInstance_.log({
|
||||
level: "silly",
|
||||
message,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs a message at the info level.
|
||||
* @param {string} message - the message to log
|
||||
* Level 5
|
||||
*/
|
||||
debug(message: string) {
|
||||
this.loggerInstance_.log({
|
||||
@@ -284,9 +300,34 @@ export class Reporter {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs a message at the vebose level.
|
||||
* @param {string} message - the message to log
|
||||
* Level 4
|
||||
*/
|
||||
verbose(message: string) {
|
||||
this.loggerInstance_.log({
|
||||
level: "vebose",
|
||||
message,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs a message at the http level.
|
||||
* @param {string} message - the message to log
|
||||
* Level 3
|
||||
*/
|
||||
http(message: string) {
|
||||
this.loggerInstance_.log({
|
||||
level: "http",
|
||||
message,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs a message at the info level.
|
||||
* @param {string} message - the message to log
|
||||
* Level 2
|
||||
*/
|
||||
info(message: string) {
|
||||
this.loggerInstance_.log({
|
||||
@@ -298,8 +339,9 @@ export class Reporter {
|
||||
/**
|
||||
* Logs a message at the warn level.
|
||||
* @param {string} message - the message to log
|
||||
* Level 1
|
||||
*/
|
||||
warn = (message: string) => {
|
||||
warn(message: string) {
|
||||
this.loggerInstance_.warn({
|
||||
level: "warn",
|
||||
message,
|
||||
|
||||
@@ -5,7 +5,11 @@ import session from "express-session"
|
||||
import Redis from "ioredis"
|
||||
import morgan from "morgan"
|
||||
import path from "path"
|
||||
import { logger } from "../logger"
|
||||
import { configManager } from "../config"
|
||||
import { MedusaRequest, MedusaResponse } from "./types"
|
||||
|
||||
const NOISY_ENDPOINTS_CHUNKS = ["@fs", "@id", "@vite", "@react", "node_modules"]
|
||||
|
||||
export async function expressLoader({ app }: { app: Express }): Promise<{
|
||||
app: Express
|
||||
@@ -14,8 +18,10 @@ export async function expressLoader({ app }: { app: Express }): Promise<{
|
||||
const baseDir = configManager.baseDir
|
||||
const configModule = configManager.config
|
||||
const isProduction = configManager.isProduction
|
||||
const isStaging = process.env.NODE_ENV === "staging"
|
||||
const isTest = process.env.NODE_ENV === "test"
|
||||
const NODE_ENV = process.env.NODE_ENV || "development"
|
||||
const IS_DEV = NODE_ENV.startsWith("dev")
|
||||
const isStaging = NODE_ENV === "staging"
|
||||
const isTest = NODE_ENV === "test"
|
||||
|
||||
let sameSite: string | boolean = false
|
||||
let secure = false
|
||||
@@ -40,7 +46,7 @@ export async function expressLoader({ app }: { app: Express }): Promise<{
|
||||
store: null,
|
||||
}
|
||||
|
||||
let redisClient
|
||||
let redisClient: Redis
|
||||
|
||||
if (configModule?.projectConfig?.redisUrl) {
|
||||
const RedisStore = createStore(session)
|
||||
@@ -55,11 +61,29 @@ export async function expressLoader({ app }: { app: Express }): Promise<{
|
||||
}
|
||||
|
||||
app.set("trust proxy", 1)
|
||||
app.use(
|
||||
morgan("combined", {
|
||||
skip: () => isTest,
|
||||
})
|
||||
)
|
||||
|
||||
/**
|
||||
* Method to skip logging HTTP requests. We skip in test environment
|
||||
* and also exclude files served by vite during development
|
||||
*/
|
||||
function shouldSkipHttpLog(req: MedusaRequest, res: MedusaResponse) {
|
||||
return (
|
||||
isTest || NOISY_ENDPOINTS_CHUNKS.some((chunk) => req.url.includes(chunk))
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* The middleware to use for logging. We write the log messages
|
||||
* using winston, but rely on morgan to hook into HTTP requests
|
||||
*/
|
||||
const loggingMiddleware = morgan(IS_DEV ? "dev" : "tiny", {
|
||||
skip: shouldSkipHttpLog,
|
||||
stream: {
|
||||
write: (message: string) => logger.http(message),
|
||||
},
|
||||
})
|
||||
|
||||
app.use(loggingMiddleware)
|
||||
app.use(cookieParser())
|
||||
app.use(session(sessionOpts))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user