You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I figured I would transform the error into something loggable:
/** Turns an error into a plain object. */functionparseError(err: Error): ParsedError{returnJSON.parse(JSON.stringify(err,Object.getOwnPropertyNames(err)),)asParsedError}/** Handles proper stringification of Buffer and bigint output. */functionreplacer(_key: string,value: unknown){if(typeofvalue==="bigint")returnvalue.toString()returnvalue}constlogId=randomUUID()exportconstlogger=createLogger({defaultMeta: { logId },format: format.json({replacer: (key,value: unknown)=>{if(valueinstanceofError)returnparseError(value)returnreplacer(key,value)},}),level: "debug",transports: [newtransports.Console({format: format.simple()})],})// Testsconsterr=newError("This is an error.")logger.info("logger.info:",{error: err})console.log("console.log:",{error: parseError(err)})
However, the new output is completely detached from the code I've written.
info: logger.info: {"error":{"originalColumn":22,"originalLine":24},"logId":"abe10e0e-0b04-4651-be81-00ede4fca436"}
console.log: {
error: {
message: "This is an error.",
originalLine: 24,
originalColumn: 22,
line: 24,
column: 22,
sourceURL: "/home/nato/Code/localhost/GitLabAPI/src/logger.ts",
stack: "Error: This is an error.\n at module code (/home/nato/Code/localhost/GitLabAPI/src/logger.ts:41:12)"
}
}
More than half of the fields were completely ignored. If I log inside the format.json lambda, the keys are passed correctly.
What version of Logform presents the issue?
2.6.0
What version of Node are you using?
21.1.0
If this is a TypeScript issue, what version of TypeScript are you using?
5.2.2
If this worked in a previous version of Logform, which was it?
No response
Minimum Working Example
import{randomUUID}from"crypto"import{createLogger,format,transports}from"winston"interfaceParsedError{readonlymessage: stringreadonlyoriginalLine: numberreadonlyoriginalColumn: numberreadonlyline: numberreadonlycolumn: numberreadonlysourceURL: stringreadonlystack: string}/** Turns an error into a plain object. */functionparseError(err: Error): ParsedError{returnJSON.parse(JSON.stringify(err,Object.getOwnPropertyNames(err)),)asParsedError}/** Handles proper stringification of Buffer and bigint output. */functionreplacer(_key: string,value: unknown){if(typeofvalue==="bigint")returnvalue.toString()returnvalue}constlogId=randomUUID()exportconstlogger=createLogger({defaultMeta: { logId },format: format.json({replacer: (key,value: unknown)=>{if(valueinstanceofError)returnparseError(value)returnreplacer(key,value)},}),level: "debug",transports: [newtransports.Console({format: format.simple()})],})// Testsconsterr=newError("This is an error.")logger.info("logger.info:",{error: err})console.log("console.log:",{error: parseError(err)})
Additional information
If we pre-parse each error individually before passing them to Winston, then it works. The bug only appears when we send an Error instance, which seems like the whole point of using a logger in the first place, so I'm a bit confused at why this doesn't work natively nor after hacking into it.
Btw, I don't see a reason to not export the defaut replacer
* Handles proper stringification of Buffer and bigint output.
*/
functionreplacer(key,value){
// safe-stable-stringify does support BigInt, however, it doesn't wrap the value in quotes.
// Leading to a loss in fidelity if the resulting string is parsed.
// It would also be a breaking change for logform.
if(typeofvalue==='bigint')
returnvalue.toString();
returnvalue;
}
🔎 Search Terms
log error, log errors, format.json, format error, format errors
The text was updated successfully, but these errors were encountered:
NatoBoram
changed the title
[Bug]: Errors don't get processed correctly by format.json
[Bug]: Errors don't get processed correctly by format.json / format.simpleNov 9, 2023
The problem
I want to log errors.
This gives me
{}
.I figured I would transform the error into something loggable:
However, the new output is completely detached from the code I've written.
More than half of the fields were completely ignored. If I log inside the
format.json
lambda, the keys are passed correctly.What version of Logform presents the issue?
2.6.0
What version of Node are you using?
21.1.0
If this is a TypeScript issue, what version of TypeScript are you using?
5.2.2
If this worked in a previous version of Logform, which was it?
No response
Minimum Working Example
Additional information
If we pre-parse each error individually before passing them to Winston, then it works. The bug only appears when we send an
Error
instance, which seems like the whole point of using a logger in the first place, so I'm a bit confused at why this doesn't work natively nor after hacking into it.Btw, I don't see a reason to not export the defaut
replacer
logform/json.js
Lines 7 to 18 in ded082a
🔎 Search Terms
log error, log errors, format.json, format error, format errors
The text was updated successfully, but these errors were encountered: