Home > Enterprise >  Why does Coldfusion add an extra quote to each existing quote in cflog?
Why does Coldfusion add an extra quote to each existing quote in cflog?

Time:10-06

Trying to save info to the logs using cflog, and save in JSON format. But for some reason, an extra dbl-quote is getting added next to each already existing dbl-quote.

Example: I do something like this, turning a simple struct into a JSON string:

<cfset local.fname = "Max">
<cfset local.lname = "Smith">
<cfset local.id = "QA-123">

<cflog text="#serializeJSON(local)#">

And in the logs, it gets saved to look like:

"INFO","http-apr-8888-exec-6","10/04/2021","19:24:46","","{""fname"":""Max"",""lname"":""Smith"",""id"":""QA-123""}"

Then if I try to save it this way, I get no quotes, and thus invalid JSON.

<cflog text='{fname:Max,lname:smith,id:QA-123}'>

results in:

"INFO","http-apr-8888-exec-6","10/04/2021","19:24:46","","{fname:Max,lname:Smith,id:QA-123}"

And

<cflog text='{"fname":"Max","lname":"smith","id":"QA-123"}'>

results in the same as the first example:

"INFO","http-apr-8888-exec-6","10/04/2021","19:24:46","","{""fname"":""Max"",""lname"":""Smith"",""id"":""QA-123""}"

Why is it doing this, and how do I end up with the log entry I want, without any extra quotes?:

"INFO","http-apr-8888-exec-6","10/04/2021","19:24:46","","{"fname":"Max","lname":"Smith","id":"QA-123"}"

CodePudding user response:

I've go through your issue. Yes we have this from Coldfusion 11, Coldfusion 2016. But not in Coldfusion 2018 ( Update 12 ). May be they fixed this case in recent updates about Coldfusion 2018.

Please try to update your version up to date then check with these issue.

But for now, I can give you some solution by using replace() method.

Sample Code :

<cfset myStr = {} >
<cfset myStr.fname = "Kannan">
<cfset myStr.lname = "Pasumpon">
<cfset myStr.id = "CF-123">

<cfset jsonData = replace(serializeJSON( myStr ), '"',"'","All")>
<cflog text="#jsonData#" file="testingWorks">

Here I just replace all my double quotes value with single quotes. And logged it in testingWorks.log file. The new result will be like below,

Result In Log File :

"Information","http-nio-8501-exec-6","10/05/21","12:21:04","","{'LNAME':'Pasumpon','ID':'CF-123','FNAME':'Kannan'}" 

Likewise, You can achieve the this things as per your needs.

Note : The update version resolved this case by default. So I would suggest that update your version first instead of changed / replace the notations.

CodePudding user response:

We're running CF10 (older version because we're phasing out of CF), and viewing the logs through Splunk. Not sure if Splunk is a CSV parser, but figured out a way to get the logs written as desired.

Created a function named writeLog(), using Java sys.out.println instead of the cflog tag:

<cfscript>
function writeLog(required message) {
    var logString = serializeJSON(arguments);
    sys.out.println('{"timestamp":"#dateTimeFormat(now(), "yyyy-mm-dd'T'hh:mm:ss:ssssssZ")#",#Right(logString, Len(logString) - 1)#');
}
</cfscript>

And then call it like so:

<cfscript>
            var emailData = structNew();
            emailData.toAddress = ARGUMENTS.to;
            emailData.fromAddress = ARGUMENTS.from;
            emailData.subject = ARGUMENTS.subject;

            APPLICATION.general.writeLog(message="Sending email", argumentCollection=emailData);
        </cfscript>

And (with some adjustments to Splunk) the resulting log looks like:

{ 
   FROMADDRESS: no[email protected]
   SUBJECT: Welcome to the team
   TOADDRESS: [email protected]
   message: Sending email
   timestamp: 2021-10-05T11:10:21:000021-0400
}
  • Related