use safe stringify in logs

This commit is contained in:
Hongbo Wu
2023-07-28 16:52:28 +08:00
parent 1621168377
commit d0ad636b7f
6 changed files with 13 additions and 9 deletions

View File

@ -59,6 +59,7 @@
"express": "^4.17.1",
"express-http-context2": "^1.0.0",
"express-rate-limit": "^6.3.0",
"fast-safe-stringify": "^2.1.1",
"firebase-admin": "^11.5.0",
"googleapis": "^105.0.0",
"graphql": "^15.3.0",

View File

@ -223,7 +223,7 @@ export const searchHighlights = async (
],
}
logger.info('searching highlights in elastic', JSON.stringify(searchBody))
logger.info('searching highlights in elastic', searchBody)
const response = await client.search<SearchResponse<Page>>({
index: INDEX_ALIAS,

View File

@ -421,7 +421,7 @@ export const createPage = async (
return page.id
} catch (e) {
logger.error('failed to create a page in elastic', JSON.stringify(e))
logger.error('failed to create a page in elastic', e)
return undefined
}
}
@ -665,7 +665,7 @@ export const searchPages = async (
})
.build()
logger.info('searching pages in elastic', JSON.stringify(body))
logger.info('searching pages in elastic', body)
const response = await client.search<SearchResponse<Page>, BuiltQuery>({
index: INDEX_ALIAS,
body,
@ -904,7 +904,7 @@ export const updatePages = async (
.rawOption('script', updatedScript)
.build()
logger.info('updating pages in elastic', JSON.stringify(searchBody))
logger.info('updating pages in elastic', searchBody)
try {
const { body } = await client.updateByQuery({

View File

@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/restrict-template-expressions */
import { LoggingWinston } from '@google-cloud/logging-winston'
import jsonStringify from 'fast-safe-stringify'
import { cloneDeep, isArray, isObject, isString, truncate } from 'lodash'
import { DateTime } from 'luxon'
import {
@ -83,7 +84,9 @@ function localConfig(id: string): ConsoleTransportOptions {
const { timestamp, message, level, ...meta } = info
return `[${id}@${info.timestamp}] ${info.message}${
Object.keys(meta).length ? '\n' + JSON.stringify(meta, null, 4) : ''
Object.keys(meta).length
? '\n' + jsonStringify(meta, undefined, 4)
: ''
}`
})
),
@ -120,7 +123,7 @@ const truncateObjectDeep = (object: any, length: number): any => {
class GcpLoggingTransport extends LoggingWinston {
log(info: any, callback: (err: Error | null, apiResponse?: any) => void) {
const sizeInfo = JSON.stringify(info).length
const sizeInfo = jsonStringify(info).length
if (sizeInfo > 250000) {
info = truncateObjectDeep(info, 5000) as never // the max length for string values is 5000
}

View File

@ -45,7 +45,7 @@ export const sendEmail = async (msg: MailDataRequired): Promise<boolean> => {
} catch (error) {
const err = asSendGridError(error)
if (err) {
logger.error('sendgrid error:', JSON.stringify(err.response?.body))
logger.error('sendgrid error:', err.response?.body)
} else {
logger.error('error sending email', error)
}

View File

@ -59,9 +59,9 @@ export const sendMulticastPushNotifications = async (
},
})
logger.info('sending multicast message: ', JSON.stringify(message))
logger.info('sending multicast message: ', message)
const res = await getMessaging().sendMulticast(message)
logger.info('send notification result: ', JSON.stringify(res.responses))
logger.info('send notification result: ', res.responses)
return res
} catch (err) {