Archana Mundaye
Archana Mundaye

Reputation: 533

Log message as json format in wildfly logs

I've added below configuration in standalone.xml under

<subsystem xmlns="urn:jboss:domain:logging:7.0">

<size-rotating-file-handler name="JasperReportUiLog" autoflush="true">
                <level name="ALL" />
                <file relative-to="jboss.server.log.dir" path="jasper-json-log.json" />
                <rotate-size value="200m" />
                <max-backup-index value="20" />
                <append value="true" />
</size-rotating-file-handler>

 <logger category="com.ideas.tetris.ui.modules.reports" use-parent-handlers="false">
                <level name="INFO"/>
                <handlers>
                    <handler name="JasperReportUiLog"/>
                </handlers>
</logger>

It doesn't understand

<pattern-formatter pattern="json" /> 

so can't add configuration as

<size-rotating-file-handler name="JasperReportUiLog" autoflush="true">
                    <level name="ALL" />
                    <formatter>
                        <pattern-formatter pattern="json" />
                    </formatter>
                    <file relative-to="jboss.server.log.dir" path="jasper-json-log.json" />
                    <rotate-size value="200m" />
                    <max-backup-index value="20" />
                    <append value="true" />
    </size-rotating-file-handler>

so if I remove this formater then it adds log message which is URL in my case with many queryString Parameters ,as complete string. For building this string we use map, so if I log that map then it shows it as kind of json but again that's because of java.lang.Object's toString method and not because this json formatter and it's not complete json which we can just pick and give to some other algorithm which we intent to use for some ML purpose as i/p for analyzing further request.

I think it is wrong at my end to expect string to format as json but need to know from jboss expert people that if there's a way to log some message as json?

Upvotes: 0

Views: 4809

Answers (2)

Tarun
Tarun

Reputation: 61

The answer is simple as it is, Please follow the following steps

  1. Add a formatter named 'JSON_FORMATTER_NAME'

  2. Replace the 'named-formatter' to JSON_FORMATTER_NAME

Thanks

Upvotes: 0

Archana Mundaye
Archana Mundaye

Reputation: 533

My understanding was not wrong I had missed to declare json formatter in there.

So my final changes look like this

       <profile>
          <subsystem xmlns="urn:jboss:domain:logging:7.0">
             .
             .
             .

            <size-rotating-file-handler name="ReportUiLog" autoflush="true">
                <level name="ALL" />
                <formatter>
                    <named-formatter name="JSON"/>
                </formatter>
                <file relative-to="jboss.server.log.dir" path="jasper-ui-log.json" />
                <rotate-size value="200m" />
                <max-backup-index value="20" />
                <append value="true" />
            </size-rotating-file-handler>
            <logger category="com.mypackagename.reports" use-parent-handlers="true">
                <level name="INFO" />
                <handlers>
                    <handler name="ReportUiLog"/>
                </handlers>
          </logger>
        <formatter name="JSON">
            <json-formatter/>
        </formatter>
    </subsystem>

And I'm using MDC to save few fields so that those also will get logged in log when I'll log my message. So when we do MDC.put("userName","Archana") before logging and after that if you log sentence like log.info("Executed") then in log with word "Executed" it will add this keyValue pair which we have added in MDC before logging. It's important to clear MDC after logging otherwise with each logger your MDC values will get printed.

One note, here if I add multiple values in MDC then those all gets printed automatically but somehow when I'm doing same kind of configuration in log4j.properties in my some other project it's not printing all values of MDC and I've to explicitly specify keys set in MDC which is weird.

Those threads are these link, link2 but hoping that this current solution will help somebody.

Upvotes: 2

Related Questions