Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nextjs): Improve debug logging #1866

Merged
merged 5 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tough-masks-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/nextjs': patch
---

Improves the debug log output, and changes the internal behavior to use multiple `console.log()` calls. This will help to avoid any platform logging limitations per call.
3 changes: 3 additions & 0 deletions packages/nextjs/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
globals: {
PACKAGE_VERSION: '0.0.0-test',
},
displayName: 'nextjs',
injectGlobals: true,
roots: ['<rootDir>/src'],
Expand Down
6 changes: 5 additions & 1 deletion packages/nextjs/src/utils/debugLogger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,13 @@ describe('withLogger', () => {
const handler = withLogger('test-logger', logger => () => {
logger.enable();
logger.debug(veryLongString);
logger.debug(veryLongString);
});
handler();
expect(log.mock.calls[0][0]).toHaveLength(4096);

for (const mockCall of log.mock.calls) {
expect(mockCall[0].length).toBeLessThanOrEqual(4096);
}

// restore original console log and reset environment value
process.env.VERCEL = undefined;
Expand Down
40 changes: 32 additions & 8 deletions packages/nextjs/src/utils/debugLogger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// TODO: Replace with a more sophisticated logging solution

import nextPkg from 'next/package.json';

import { logFormatter } from './logFormatter';

export type Log = string | Record<string, unknown>;
Expand All @@ -25,15 +27,29 @@ export const createDebugLogger = (name: string, formatter: (val: LogEntry) => st
},
commit: () => {
if (isEnabled) {
const log = `Clerk debug start :: ${name}\n${entries
.map(log => formatter(log))
.map(e => `-- ${e}\n`)
.join('')}`;
if (process.env.VERCEL) {
console.log(truncate(log, 4096));
} else {
console.log(log);
console.log(debugLogHeader(name));

/**
* We buffer each collected log entry so we can format them and log them all at once.
* Individual console.log calls are used to ensure we don't hit platform-specific log limits (Vercel and Netlify are 4kb).
*/
for (const log of entries) {
let output = formatter(log);

output = output
.split('\n')
.map(l => ` ${l}`)
.join('\n');

// Vercel errors if the output is > 4kb, so we truncate it
if (process.env.VERCEL) {
output = truncate(output, 4096);
}

console.log(output);
}

console.log(debugLogFooter(name));
}
},
};
Expand Down Expand Up @@ -77,6 +93,14 @@ export const withLogger: WithLogger = (loggerFactoryOrName, handlerCtor) => {
}) as ReturnType<typeof handlerCtor>;
};

function debugLogHeader(name: string) {
return `[clerk debug start: ${name}]`;
}

function debugLogFooter(name: string) {
return `[clerk debug end: ${name}] (@clerk/nextjs=${PACKAGE_VERSION},next=${nextPkg.version})`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

}

// ref: https://stackoverflow.com/questions/57769465/javascript-truncate-text-by-bytes-length
function truncate(str: string, maxLength: number) {
const encoder = new TextEncoder();
Expand Down