Improve logging (#4921)

* improve logging

* Update logger.ts

---------

Co-authored-by: Henry Heng <henryheng@flowiseai.com>
This commit is contained in:
matekungl-byborg 2025-07-25 19:28:31 +02:00 committed by GitHub
parent 5ae6ae2916
commit 8846fd14e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 41 additions and 35 deletions

View File

@ -20,6 +20,8 @@ let gcsServerStream: any
let gcsErrorStream: any
let gcsServerReqStream: any
let requestLogger: any
if (process.env.STORAGE_TYPE === 's3') {
const accessKeyId = process.env.S3_STORAGE_ACCESS_KEY_ID
const secretAccessKey = process.env.S3_STORAGE_SECRET_ACCESS_KEY
@ -165,28 +167,10 @@ const logger = createLogger({
]
})
export function expressRequestLogger(req: Request, res: Response, next: NextFunction): void {
const unwantedLogURLs = ['/api/v1/node-icon/', '/api/v1/components-credentials-icon/', '/api/v1/ping']
if (/\/api\/v1\//i.test(req.url) && !unwantedLogURLs.some((url) => new RegExp(url, 'i').test(req.url))) {
// Create a sanitized copy of the request body
const sanitizedBody = { ...req.body }
if (sanitizedBody.password) {
sanitizedBody.password = '********'
}
const fileLogger = createLogger({
requestLogger = createLogger({
format: combine(timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), format.json(), errors({ stack: true })),
defaultMeta: {
package: 'server',
request: {
method: req.method,
url: req.url,
body: sanitizedBody, // Use sanitized body instead of raw body
query: req.query,
params: req.params,
headers: req.headers
}
package: 'server'
},
transports: [
...(process.env.DEBUG && process.env.DEBUG === 'true' ? [new transports.Console()] : []),
@ -209,6 +193,28 @@ export function expressRequestLogger(req: Request, res: Response, next: NextFunc
]
})
export function expressRequestLogger(req: Request, res: Response, next: NextFunction): void {
const unwantedLogURLs = ['/api/v1/node-icon/', '/api/v1/components-credentials-icon/', '/api/v1/ping']
if (/\/api\/v1\//i.test(req.url) && !unwantedLogURLs.some((url) => new RegExp(url, 'i').test(req.url))) {
// Create a sanitized copy of the request body
const sanitizedBody = { ...req.body }
if (sanitizedBody.password) {
sanitizedBody.password = '********'
}
// Use the shared requestLogger with request-specific metadata
const requestMetadata = {
request: {
method: req.method,
url: req.url,
body: sanitizedBody, // Use sanitized body instead of raw body
query: req.query,
params: req.params,
headers: req.headers
}
}
const getRequestEmoji = (method: string) => {
const requetsEmojis: Record<string, string> = {
GET: '⬇️',
@ -222,10 +228,10 @@ export function expressRequestLogger(req: Request, res: Response, next: NextFunc
}
if (req.method !== 'GET') {
fileLogger.info(`${getRequestEmoji(req.method)} ${req.method} ${req.url}`)
requestLogger.info(`${getRequestEmoji(req.method)} ${req.method} ${req.url}`, requestMetadata)
logger.info(`${getRequestEmoji(req.method)} ${req.method} ${req.url}`)
} else {
fileLogger.http(`${getRequestEmoji(req.method)} ${req.method} ${req.url}`)
requestLogger.http(`${getRequestEmoji(req.method)} ${req.method} ${req.url}`, requestMetadata)
}
}