Skip to main content
Known Participant
June 30, 2022
Question

Coldfusion Log JSON Format

  • June 30, 2022
  • 2 replies
  • 562 views

I need all my Coldfusion logs to store in JSON format.

I can then parse those logs as a single line rather than a multi-line.

How can I achieve that?

 

    This topic has been closed for replies.

    2 replies

    BKBK
    Community Expert
    Community Expert
    July 2, 2022

    Even if ColdFusion had the facility to do that, why would you bother at all? Just let ColdFusion do its thing. You do yours. 🙂

     

    Converting a ColdFusion log file to JSON format is straightforward. Take the gateway log file, for example:

     

    <cfset logFileAsIs=fileread("C:\ColdFusion2021\cfusion\logs\eventgateway.log")>
    
    <cfset logFileAsArray=[]>
    
    <!--- Using line-break as delimiter, convert each line of log
    into an element in an array --->
    <cfloop list="#logFileAsIs#" delimiters="#chr(10)&chr(13)#" index="line">
    	<cfset arrayAppend(logFileAsArray, "{#line#}")>
    </cfloop>
    
    <cfset logFileAsJSON=serializeJSON(logFileAsArray)>
    
    <!---<cfdump var="#logFileAsArray#" label="logFileAsArray">--->
    <!---<cfdump var="#logFileAsJSON#" label="logFileAsJSON">--->

     

     

     

     

    Known Participant
    July 4, 2022

    @BKBK Thank you for the reply.

    I think your solution will only work for single-line log files, not for multi-line log files.

    I need to parse multi-line log files also.

    Let me know your thoughts.

     

    Thanks,Souvik
    BKBK
    Community Expert
    Community Expert
    July 4, 2022

    Sorry, I don't understand what you mean by "single-line" and "multi-line".

    Could you please provide examples to illustrate.

    Thanks.

    Charlie Arehart
    Community Expert
    Community Expert
    June 30, 2022

    To be clear, there's no cf feature to enable that. You should be able to find generic 3rd party tools offering to convert a text log file to json.

     

    You may even be able to find some tomcat-specific feature or add-on providing for this.

     

    Either way (and to your last point), do note that there are 2 kinds of cf logs: those that are single-line (most of them), having a date/time at the start of each line. Then there are a few that are multi-line (having lines without a datetime), including the coldfusion-out.log, coldfusion-error.log, and exception.log, though SOMETIMES they are single-line, depending on the circumstance.

     

    If the latter may be what you refer to, I suspect you'll have a heck of a time converting those to single-line, though I suppose with some tools it may be possible.

     

    Let us know if you find anything, or perhaps someone else will have an idea for you. 

    /Charlie (troubleshooter, carehart. org)