Commit 613facf8 authored by Jiachi Liu's avatar Jiachi Liu

improve forward logs stack display

parent 7e093afb
...@@ -336,7 +336,11 @@ const createErrorArg = (error: Error) => { ...@@ -336,7 +336,11 @@ const createErrorArg = (error: Error) => {
} }
} }
const createLogEntry = (level: LogMethod, args: any[]) => { const createLogEntry = (
level: LogMethod,
args: any[],
capturedStack?: string | null
) => {
// Always log to client file logger with args (formatting done inside log method) // Always log to client file logger with args (formatting done inside log method)
clientFileLogger.log(level, args) clientFileLogger.log(level, args)
...@@ -345,9 +349,9 @@ const createLogEntry = (level: LogMethod, args: any[]) => { ...@@ -345,9 +349,9 @@ const createLogEntry = (level: LogMethod, args: any[]) => {
return return
} }
// do not abstract this, it implicitly relies on which functions call it. forcing the inlined implementation makes you think about callers // Prefer a stack captured at the original console.* callsite (from the patched
// error capture stack trace maybe // console wrapper) if one was provided; otherwise, fall back to capturing here.
const stack = stackWithOwners(new Error()) const stack = capturedStack ?? stackWithOwners(new Error())
const stackLines = stack?.split('\n') const stackLines = stack?.split('\n')
const cleanStack = stackLines?.slice(3).join('\n') // this is probably ignored anyways const cleanStack = stackLines?.slice(3).join('\n') // this is probably ignored anyways
const entry: ConsoleEntry<unknown> = { const entry: ConsoleEntry<unknown> = {
...@@ -561,13 +565,16 @@ export const initializeDebugLogForwarding = (router: 'app' | 'pages'): void => { ...@@ -561,13 +565,16 @@ export const initializeDebugLogForwarding = (router: 'app' | 'pages'): void => {
try { try {
methods.forEach((method) => methods.forEach((method) =>
patchConsoleMethod(method, (_, ...args) => { patchConsoleMethod(method, (_, ...args) => {
// Capture the stack as close as possible to the original console.* call.
const stack = stackWithOwners(new Error())
if (isHMR(args)) { if (isHMR(args)) {
return return
} }
if (isReactServerReplayedLog(args)) { if (isReactServerReplayedLog(args)) {
return return
} }
createLogEntry(method, args) createLogEntry(method, args, stack)
}) })
) )
} catch {} } catch {}
......
...@@ -26,7 +26,9 @@ export default function ClientPage() { ...@@ -26,7 +26,9 @@ export default function ClientPage() {
console.warn('Client: This is a warning message from client component') console.warn('Client: This is a warning message from client component')
}, []) }, [])
console.error('globalThis', globalThis) if (typeof window !== 'undefined') {
console.error('window', window)
}
return <p>client page with logging</p> return <p>client page with logging</p>
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment