How to configure log4j 2 in JSON configuraion for springframework?

838 Views Asked by At


I want to debug springframework, To know what is happening while routing request in Restcontroller.
I have defined JSON configuration like below but that is not working for me.

 {
    "configuration": {
        "appenders": {
            "Console": {
                "name": "consoleAppender",
                "target":"SYSTEM_OUT",
                "PatternLayout": {
                    "pattern": "%d{yyyy-MM-dd HH:mm:ss:SSS} %-5p [%c{1}:%t]:%L - %m%n"
                }
            }
        },
        "loggers": {
            "root": {
                "level": "info",
                "appender-ref": [{ "ref": "consoleAppender" }]
            },
            "logger":[{
                "name":"org.springframework", 
                "level": "debug",
                "appender-ref": [{ "ref": "consoleAppender" }]
            }]
        }
    }

}
Let me know, if i am doing wrong here.
And another question regarding , what is right way to configure log4j using JSON or XML ?

1

There are 1 best solutions below

1
On

The below configuration is using for log4j framework. Its describe on XML .

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="SYSLOG" class="org.apache.log4j.net.SyslogAppender">
        <param name="Facility" value="LOCAL0"/>
        <param name="FacilityPrinting" value="false"/>
        <param name="SyslogHost" value="localhost"/>
        <param name="Threshold" value="INFO"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ISO8601} %-5p [WhereRU][%t|%c{1}] %L %M %m\n"/>
        </layout>
    </appender>
    <appender name="SERVER_DEBUG" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="DatePattern" value="'-'yyyy-MM-dd'.log'"/>
        <param name="File" value="/var/log/testing/testing-debug"/>
        <param name="Threshold" value="DEBUG"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ISO8601} %-5p [WhereRU][%t|%c{1}] %L %M %m\n"/>
        </layout>
    </appender>
    <appender name="SERVER_ERROR" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="DatePattern" value="'-'yyyy-MM-dd'.log'"/>
        <param name="File" value="/var/log/testing/testing-error"/>
        <param name="Threshold" value="ERROR"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ISO8601} %-5p [WhereRU][%t|%c{1}] %L %M %m\n"/>
        </layout>
    </appender>
    <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ISO8601} %-5p [WhereRU][%t|%c{1}] %L %M %m\n" />
        </layout>
    </appender>

    <logger name="com.testing" additivity="false">
        <level value="DEBUG"/>
         <appender-ref ref="SYSLOG"/>
         <appender-ref ref="SERVER_DEBUG"/>
         <appender-ref ref="SERVER_ERROR"/>
         <appender-ref ref="STDOUT" />
    </logger>

    <logger name="org.springframework" additivity="false">
        <level value="ERROR"/>
         <appender-ref ref="SERVER_ERROR"/>
    </logger>

    <logger name="org.apache" additivity="false">
        <level value="ERROR"/>
         <appender-ref ref="SERVER_ERROR"/>
    </logger>

    <logger name="org.springframework.integration">
        <level value="info" />
    </logger>

    <logger name="org.springframework.integration.samples">
        <level value="info" />
    </logger>

    <!-- Everything else to catalina.out -->
    <root>
        <level value="ERROR"/>
        <appender-ref ref="STDOUT" />
        <appender-ref ref="SERVER_DEBUG" />
        <appender-ref ref="SERVER_ERROR" />
    </root>
</log4j:configuration>

Hopes this stuff worked on your problem.