Bugfix: Add debug option to log response headers#219
Conversation
|
|
||
| private debug(context: ResponsePluginContext) { | ||
| if (context.requestOptions.logHeadersOnError && !context.response?.ok) { | ||
| console.debug( |
There was a problem hiding this comment.
We should think about supporting also for this SDK a logger interface.
| context.response?.headers, | ||
| if (!context.response?.ok) { | ||
| this.logger.debug( | ||
| `[Sinch SDK][Debug][${context.apiName}][${context.operationId}][${context.response?.status}]\nHTTP method: ${context.requestOptions.method}\nURL: ${context.url}\nResponse Headers: ${this.formatHeaders(context.response?.headers)}`, |
There was a problem hiding this comment.
What do you think about having the logger itself adding these formatting information ?
- this.logger: will know "it is" SDK -> it will add it when emitting log
- this.logger.debug: logger.debug function will know it is debug level then it will be added when emitting log
PROS:
- all calls to
loggerwill be prefixed by same ... prefix - all calls to
logger./debug/info/..will have same prefix regarding log level - all logs will have same pattern [Sinch SDK][level] and no human error to forgot them services will call logger function
Use global console for SinchLogger fallbacks so tests can spy on warnings, and drop unused OAuth2 token request logger after v1.5-next error handling.
fe7b383 to
71c6593
Compare
| this.lazyClient.getApiClient(); | ||
| } catch (error) { | ||
| console.error('Impossible to assign the new credentials to the SMS API'); | ||
| new SinchLogger(this.lazyClient.sharedConfig.logger ?? console).error( |
There was a problem hiding this comment.
Here we are assuming we have to send a log onto console even if configuration could have been, by purpose of SDK user, to NOT have any logger (multliple times)
What do you think :
- either we assume there is, by contract, a logger defined
- test if null/undefined and do not log anything
There was a problem hiding this comment.
Fixed 🟡
Removed all ?? console fallbacks; call sites use resolveLogger(...). logger: null → silent NOOP_LOGGER.
packages/sms/src/rest/v1/sms-domain-api.ts (and all other *-service.ts / *-domain-api.ts)
| * @param {SinchClientParameters} params - The object containing the Sinch credentials. | ||
| */ | ||
| constructor(params: SinchClientParameters) { | ||
| if (!params.logger) { |
There was a problem hiding this comment.
See comment about using or not a default logger.
What if for any purpose an end user do not want any log ? (thinking about a logging call causing an issue)
It won't be possible.
We could have distinction between null VS undefined to provide
- null -> a dummy logger not logging anything
- undefined -> a default logger based onto init of SinchLogger
And, by contract we could remove all validation against not defined logger. e.g.: https://github.com/sinch/sinch-sdk-node/pull/219/changes#diff-85a0299065d0f2bd71e5151e1983edb3447d2d9770692d9f8ac007467246bc20R69
There was a problem hiding this comment.
Fixed 🟡
- null -> silent
- undefined -> default console
packages/sdk-core/src/sinch-client.ts resolved once via resolveLogger() in SinchClient and each Service constructor.
| ): PluginRunner<V | Record<string, unknown>, V> { | ||
| return { | ||
| transform: (res: V) => { | ||
| this.debug(context); |
There was a problem hiding this comment.
🤔
Not a big fan to have a dependency onto logger/SinchLogger here.
Finally: it is "just" a container.
We could think about who should be responsible of logging.
Here, not sure a "simple" container should log itself something.
It is an exception response. Why not having transport layer performing the log itself and removing dependency onto any logger here?
There was a problem hiding this comment.
Fixed 🟡
Logger removed from ExceptionResponse. Failed HTTP debug logging moved to ApiFetchClient.
packages/sdk-client/src/client/api-fetch-client.ts
| export interface Logger { | ||
| debug(message: string, ...meta: any[]): void; | ||
| info(message: string, ...meta: any[]): void; | ||
| warn(message: string, ...meta: any[]): void; | ||
| error(message: string, ...meta: any[]): void; | ||
| } |
There was a problem hiding this comment.
We could imagine, for performance/memory consumption purpose having interface defining a callback called by logger when the message will be really send.
Then; it will avoid to format message before calling logger which will finally decide to not log anything because of required severity is not reached.
There was a problem hiding this comment.
Done 🟢
Logger methods now accept either a plain string or a callback () => string. The SDK builds the message only when the logger actually emits it.
| ...SupportedMailgunRegion, | ||
| }; | ||
|
|
||
| export interface LoggerParameters { |
There was a problem hiding this comment.
Strictly speaking; this is nor defining "logger parameters" but the logger itself
There was a problem hiding this comment.
Fixed 🟡
Renamed to WithLogger
… and warning messages
…ger calls across multiple files
How to enable debug logs:
Debug logs: