Skip to content

Commit 0165ab8

Browse files
authored
fix: replace console.error with logger.errors (#9044)
## Problem When `PayloadRequest` objects are logged using `console.log`, it creates unstructured, multiline entries in logging services like DataDog and Sentry. This circumvents the structured logging approach used throughout the rest of the codebase. ## Solution Replace `console.x` calls with the structured logging system when logging `payload.logger.x` objects. This ensures consistent log formatting and better integration with monitoring tools. ## Changes - Replaced instances of `console.log` with structured logging methods only in `@payloadcms/next` - Maintains logging consistency across the codebase - Improves log readability in DataDog, Sentry, and other monitoring services ## First <img width="914" alt="Screenshot 2024-11-06 at 09 53 44" src="https://github.com/user-attachments/assets/019b6f4b-40ed-4e54-a92a-8d1b50baa303"> ## Then <img width="933" alt="Screenshot 2024-11-06 at 00 50 29" src="https://github.com/user-attachments/assets/0a339db4-d706-4ff9-ba8c-80445bbef5d0">
1 parent 213b7c6 commit 0165ab8

File tree

5 files changed

+8
-8
lines changed

5 files changed

+8
-8
lines changed

packages/next/src/routes/rest/og/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export const generateOGImage = async ({ req }: { req: PayloadRequest }) => {
5353
// Or better yet, use a CDN like Google Fonts if ever supported
5454
fontData = fs.readFile(path.join(dirname, 'roboto-regular.woff'))
5555
} catch (e) {
56-
console.error(`Error reading font file or not readable: ${e.message}`) // eslint-disable-line no-console
56+
req.payload.logger.error(`Error reading font file or not readable: ${e.message}`)
5757
}
5858

5959
const fontFamily = 'Roboto, sans-serif'
@@ -86,7 +86,7 @@ export const generateOGImage = async ({ req }: { req: PayloadRequest }) => {
8686
},
8787
)
8888
} catch (e: any) {
89-
console.error(`${e.message}`) // eslint-disable-line no-console
89+
req.payload.logger.error(`Error generating Open Graph image: ${e.message}`)
9090
return NextResponse.json({ error: `Internal Server Error: ${e.message}` }, { status: 500 })
9191
}
9292
}

packages/next/src/views/Document/getDocumentData.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const getDocumentData = async (args: {
4747
formState,
4848
}
4949
} catch (error) {
50-
console.error('Error getting document data', error) // eslint-disable-line no-console
50+
req.payload.logger.error({ err: error, msg: 'Error getting document data' })
5151
return {
5252
data: null,
5353
formState: {

packages/next/src/views/Document/getDocumentPermissions.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export const getDocumentPermissions = async (args: {
6060
}).then(({ update }) => update?.permission)
6161
}
6262
} catch (error) {
63-
console.error(error) // eslint-disable-line no-console
63+
req.payload.logger.error(error)
6464
}
6565
}
6666

@@ -87,7 +87,7 @@ export const getDocumentPermissions = async (args: {
8787
}).then(({ update }) => update?.permission)
8888
}
8989
} catch (error) {
90-
console.error(error) // eslint-disable-line no-console
90+
req.payload.logger.error(error)
9191
}
9292
}
9393

packages/next/src/views/Versions/getLatestVersion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export async function getLatestVersion(args: Args): Promise<ReturnType> {
6161
updatedAt: response.docs[0].updatedAt,
6262
}
6363
} catch (e) {
64-
console.error(e)
64+
payload.logger.error(e)
6565
return null
6666
}
6767
}

packages/next/src/views/Versions/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export const VersionsView: PayloadServerReactComponent<EditViewComponent> = asyn
9696
})
9797
}
9898
} catch (error) {
99-
console.error(error) // eslint-disable-line no-console
99+
payload.logger.error(error)
100100
}
101101
}
102102

@@ -139,7 +139,7 @@ export const VersionsView: PayloadServerReactComponent<EditViewComponent> = asyn
139139
})
140140
}
141141
} catch (error) {
142-
console.error(error) // eslint-disable-line no-console
142+
payload.logger.error(error)
143143
}
144144

145145
if (!versionsData) {

0 commit comments

Comments
 (0)