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 would like to be able to log messages with named parameters like we can do with string interpolation.
// Idealogger.info("here is a sample log. {logid}",{logid: 12});// String Interpolation exampleconstsample=`test message ${my_variable}`
Use case
Here is how I am currently doing it with a custom formatter.
consttemplater=format((info)=>{info.original_format=info.message;letmessage=info.messageasstring;for(constkininfo){message=message.replace(`{${k}}`,JSON.stringify(info[k]));}info.message=message;returninfo;});constlogger=winston.createLogger({level: "info",format: winston.format.combine(templater(),winston.format.json(),winston.format.splat()),transports: [newwinston.transports.Console()],});logger.info("here is a sample log. {logid}",{logid: 12});
Which produces
{"level":"info","message":"here is a sample log. 12","original_format":"here is a sample log. {logid}","logid":"12"}
Additional information
Custom formatter version works okay (at least gives the desired result) but I am not sure about its performance. Maybe it could be implemented in the library in a better way
🔎 Search Terms
interpolation, format, variable
The text was updated successfully, but these errors were encountered:
Correct me if I am wrong but then I would miss the original format part wouldn't I? It makes searching by format and grouping much easier if I can access that.
logger.info({message: `here is a sample log. ${logid}`, logid: 12, original_format:"here is a sample log. {logid}"});
depending on your desire to analyze logid programmatically without parsing it from the string. However, if this is happening at volume, you might want to replace original_format with a numeric code and have some separate lookup table for details on that error. The human-readable format of the message might even then become optional depending on how you plan to use/consume the logs.
The vision
Hi,
I would like to be able to log messages with named parameters like we can do with string interpolation.
Use case
Here is how I am currently doing it with a custom formatter.
Which produces
Additional information
Custom formatter version works okay (at least gives the desired result) but I am not sure about its performance. Maybe it could be implemented in the library in a better way
🔎 Search Terms
interpolation, format, variable
The text was updated successfully, but these errors were encountered: