Skip to main content
Known Participant
Jun 30, 2022 9:07 AM UTC
Question

Coldfusion Log JSON Format

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 is closed to new replies. Start a new post to keep the conversation going.

    6 replies

    BKBK
    Community Expert
    Community Expert
    Jul 2, 2022 7:41 PM UTC

    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">--->

     

     

     

     

    Translate
    Known Participant
    Jul 4, 2022 2:17 PM UTC

    @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.

     

    Translate
    Thanks,Souvik
    BKBK
    Community Expert
    Community Expert
    Jul 4, 2022 2:37 PM UTC

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

    Could you please provide examples to illustrate.

    Thanks.

    Translate
    Charlie Arehart
    Community Expert
    Community Expert
    Jun 30, 2022 12:00 PM UTC

    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. 

    Translate
    /Charlie (troubleshooter, carehart. org)