-
Notifications
You must be signed in to change notification settings - Fork 59
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
Add cross-fork logger for runtime-handler #459
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@twilio/runtime-handler': patch | ||
--- | ||
|
||
Fix error messages in local development |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,35 @@ | ||||||
import { LoggerInstance } from '../types'; | ||||||
|
||||||
export class CrossForkLogger implements LoggerInstance { | ||||||
constructor() {} | ||||||
|
||||||
debug(msg: string) { | ||||||
this.sendLog('debug', msg); | ||||||
} | ||||||
|
||||||
info(msg: string) { | ||||||
this.sendLog('info', msg); | ||||||
} | ||||||
|
||||||
warn(msg: string, title: string = '') { | ||||||
this.sendLog('warn', msg, title); | ||||||
} | ||||||
|
||||||
error(msg: string, title: string = '') { | ||||||
this.sendLog('error', msg, title); | ||||||
} | ||||||
|
||||||
log(msg: string, level: number) { | ||||||
this.sendLog('log', msg, level); | ||||||
} | ||||||
|
||||||
private sendLog(level: keyof LoggerInstance, ...args: (string | number)[]) { | ||||||
process.send && | ||||||
process.send({ | ||||||
crossForkLogMessage: { | ||||||
level, | ||||||
args: args, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
}, | ||||||
}); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ import { | |
import { Reply } from './internal/functionRunner'; | ||
import { Response } from './internal/response'; | ||
import * as Runtime from './internal/runtime'; | ||
import { ServerConfig } from './types'; | ||
import { LoggerInstance, ServerConfig } from './types'; | ||
import debug from './utils/debug'; | ||
import { wrapErrorInHtml } from './utils/error-html'; | ||
import { getCodeLocation } from './utils/getCodeLocation'; | ||
|
@@ -303,25 +303,46 @@ export function functionPathToRoute( | |
reply, | ||
debugMessage, | ||
debugArgs = [], | ||
crossForkLogMessage, | ||
}: { | ||
err?: Error | number | string; | ||
reply?: Reply; | ||
debugMessage?: string; | ||
debugArgs?: any[]; | ||
crossForkLogMessage?: { | ||
level: keyof LoggerInstance; | ||
args: [string] | [string, number] | [string, string]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't imagine these ever changing, but does it make sense to colocation the potential args shapes with the LoggerInstance type? In case the logger instance type is used similarly elsewhere. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. args in the sendLog has the type |
||
}; | ||
}) => { | ||
if (debugMessage) { | ||
log(debugMessage, ...debugArgs); | ||
return; | ||
} | ||
|
||
if (crossForkLogMessage) { | ||
if ( | ||
config.logger && | ||
typeof config.logger[crossForkLogMessage.level] === 'function' | ||
) { | ||
Comment on lines
+321
to
+326
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: will typeof config.logger[crossForkLogMessage.level] ever return something other than a function? |
||
config.logger[crossForkLogMessage.level]( | ||
// @ts-ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not too happy about this but typing here would have been quite the pain and in my opinion would distract from the fact that this is just a pass through. |
||
...crossForkLogMessage.args | ||
); | ||
} | ||
return; | ||
} | ||
|
||
if (err) { | ||
const error = deserializeError(err); | ||
handleError(error, req, res, functionPath); | ||
} | ||
|
||
if (reply) { | ||
res.status(reply.statusCode); | ||
res.set(reply.headers); | ||
res.send(reply.body); | ||
} | ||
|
||
forked.kill(); | ||
} | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
curious, when do we use a log level? Also, what does
level
represent here?