feature: use application logger to log HTTP requests (#9655)
This commit is contained in:
@@ -18,7 +18,9 @@ if (!IS_DEV) {
|
|||||||
transports.push(
|
transports.push(
|
||||||
new winston.transports.Console({
|
new winston.transports.Console({
|
||||||
format: winston.format.combine(
|
format: winston.format.combine(
|
||||||
winston.format.cli(),
|
winston.format.cli({
|
||||||
|
levels: winston.config.npm.levels,
|
||||||
|
}),
|
||||||
winston.format.splat()
|
winston.format.splat()
|
||||||
),
|
),
|
||||||
})
|
})
|
||||||
@@ -169,6 +171,7 @@ export class Reporter {
|
|||||||
* @param {String | Error} messageOrError - can either be a string with a
|
* @param {String | Error} messageOrError - can either be a string with a
|
||||||
* message to log the error under; or an error object.
|
* message to log the error under; or an error object.
|
||||||
* @param {Error?} error - an error object to log message with
|
* @param {Error?} error - an error object to log message with
|
||||||
|
* Level 0
|
||||||
*/
|
*/
|
||||||
error(messageOrError: string | Error, error?: Error) {
|
error(messageOrError: string | Error, error?: Error) {
|
||||||
let message = messageOrError as string
|
let message = messageOrError as string
|
||||||
@@ -273,9 +276,22 @@ export class Reporter {
|
|||||||
return null
|
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.
|
* Logs a message at the info level.
|
||||||
* @param {string} message - the message to log
|
* @param {string} message - the message to log
|
||||||
|
* Level 5
|
||||||
*/
|
*/
|
||||||
debug(message: string) {
|
debug(message: string) {
|
||||||
this.loggerInstance_.log({
|
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.
|
* Logs a message at the info level.
|
||||||
* @param {string} message - the message to log
|
* @param {string} message - the message to log
|
||||||
|
* Level 2
|
||||||
*/
|
*/
|
||||||
info(message: string) {
|
info(message: string) {
|
||||||
this.loggerInstance_.log({
|
this.loggerInstance_.log({
|
||||||
@@ -298,8 +339,9 @@ export class Reporter {
|
|||||||
/**
|
/**
|
||||||
* Logs a message at the warn level.
|
* Logs a message at the warn level.
|
||||||
* @param {string} message - the message to log
|
* @param {string} message - the message to log
|
||||||
|
* Level 1
|
||||||
*/
|
*/
|
||||||
warn = (message: string) => {
|
warn(message: string) {
|
||||||
this.loggerInstance_.warn({
|
this.loggerInstance_.warn({
|
||||||
level: "warn",
|
level: "warn",
|
||||||
message,
|
message,
|
||||||
|
|||||||
@@ -5,7 +5,11 @@ import session from "express-session"
|
|||||||
import Redis from "ioredis"
|
import Redis from "ioredis"
|
||||||
import morgan from "morgan"
|
import morgan from "morgan"
|
||||||
import path from "path"
|
import path from "path"
|
||||||
|
import { logger } from "../logger"
|
||||||
import { configManager } from "../config"
|
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<{
|
export async function expressLoader({ app }: { app: Express }): Promise<{
|
||||||
app: Express
|
app: Express
|
||||||
@@ -14,8 +18,10 @@ export async function expressLoader({ app }: { app: Express }): Promise<{
|
|||||||
const baseDir = configManager.baseDir
|
const baseDir = configManager.baseDir
|
||||||
const configModule = configManager.config
|
const configModule = configManager.config
|
||||||
const isProduction = configManager.isProduction
|
const isProduction = configManager.isProduction
|
||||||
const isStaging = process.env.NODE_ENV === "staging"
|
const NODE_ENV = process.env.NODE_ENV || "development"
|
||||||
const isTest = process.env.NODE_ENV === "test"
|
const IS_DEV = NODE_ENV.startsWith("dev")
|
||||||
|
const isStaging = NODE_ENV === "staging"
|
||||||
|
const isTest = NODE_ENV === "test"
|
||||||
|
|
||||||
let sameSite: string | boolean = false
|
let sameSite: string | boolean = false
|
||||||
let secure = false
|
let secure = false
|
||||||
@@ -40,7 +46,7 @@ export async function expressLoader({ app }: { app: Express }): Promise<{
|
|||||||
store: null,
|
store: null,
|
||||||
}
|
}
|
||||||
|
|
||||||
let redisClient
|
let redisClient: Redis
|
||||||
|
|
||||||
if (configModule?.projectConfig?.redisUrl) {
|
if (configModule?.projectConfig?.redisUrl) {
|
||||||
const RedisStore = createStore(session)
|
const RedisStore = createStore(session)
|
||||||
@@ -55,11 +61,29 @@ export async function expressLoader({ app }: { app: Express }): Promise<{
|
|||||||
}
|
}
|
||||||
|
|
||||||
app.set("trust proxy", 1)
|
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(cookieParser())
|
||||||
app.use(session(sessionOpts))
|
app.use(session(sessionOpts))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user