how to configure log4j2 webapplication

2019-09-10 14:54发布

问题:

Am little new to web applications, recently I was in need to employ a logging mechanism and for that I choose Log4J2, I went through there guide, and downloaded required libraries. This is what so far I did.

1. Added following jars to web-inf/lib
   --  log4j-core2.1.jar
   --  log4j-api-2.1.jar
   --  log4j-web-2.1.jar


2. Added below xml as, log4j2.xml in java/src directory

<?xml version="1.0" encoding="UTF-8"?>
<configuration name="NONPROD" status="OFF">


    <Properties>

        <Property name="log-path">logs</Property>
    </Properties>


    <Appenders>

        <Console name="console-log" target="SYSTEM_OUT">
            <!-- <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n -->
            <!-- </pattern> -->

            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>


        <RollingFile name="info-log" fileName="${log-path}/web-info.log"
            filePattern="${log-path}/web-info-%d{yyyy-MM-dd}.log">
            <PatternLayout>
                <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
                </pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1"
                    modulate="true" />

                <OnStartupTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="20 MB" />
            </Policies>
        </RollingFile>

        <RollingFile name="error-log" fileName="${log-path}/web-error.log"
            filePattern="${log-path}/web-error-%d{yyyy-MM-dd}.log">
            <PatternLayout>
                <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
                </pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1"
                    modulate="true" />

                <OnStartupTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="20 MB" />
            </Policies>
        </RollingFile>


        <RollingFile name="debug-log" fileName="${log-path}/web-debug.log"
            filePattern="${log-path}/web-debug-%d{yyyy-MM-dd}.log">
            <PatternLayout>
                <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
                </pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1"
                    modulate="true" />

                <OnStartupTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="20 MB" />
            </Policies>
        </RollingFile>



        <RollingFile name="trace-log" fileName="${log-path}/web-trace.log"
            filePattern="${log-path}/web-trace-%d{yyyy-MM-dd}.log">
            <PatternLayout>
                <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
                </pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1"
                    modulate="true" />

                <OnStartupTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="20 MB" />
            </Policies>
        </RollingFile>

    </Appenders>

    <Loggers>

        <Logger name="com.demo.web.log4j2.file" level="debug"
            additivity="false">

            <appender-ref ref="trace-log" level="trace" />

            <appender-ref ref="error-log" level="error" />

            <appender-ref ref="debug-log" level="debug" />

            <appender-ref ref="info-log" level="info" />
        </Logger>

        <Logger name="com.demo.web.log4j2.console" level="all"
            additivity="false">
            <AppenderRef ref="console-log" />
        </Logger>


        <Root level="info" additivity="true">

            <AppenderRef ref="console-log" />

        </Root>

    </Loggers>


</configuration>

3. third and last i wrote an small web service to test logging

@Path("/register")
public class Register {

    private DataManager mManager;
     private static final Logger LOG = LogManager.getLogger(Register.class);

    public Register(){
        mManager = DataManager.getInstance();
    }


    @GET
    @Path("/{param}")
    public Response getMsg(@PathParam("param") String msg) {

        LOG.error("Not supported operations");
        return Response.status(Response.Status.BAD_REQUEST)
                .entity("Not supported").build();

    }

But in console, upon trigger get request this is all i got printed.

INFO: Server startup in 35959 ms
Not supported operations // this is not in pattern i supplied in log4j2.xml

// this is my pattern
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />

I figure out this is probably due to some thing gone wrong, and am struggling to find the actual cause, being totally new to web programming, please help me out to configure my logger.

Thanks, Techfist

回答1:

Your servlet container is using some other logging framework which logs to console. Figure out which logging framework that is and use one of log4j2's bridge libraries to redirect output to your log4j2 setup.



回答2:

Alright, I found the cause behind this, Issue was primarily happening due to two reasons.

  1. First I was suppose to exclude log4j* pattern from jarsToSkip attribute in catilina property
  2. Second, i had kept two log4j2.xml one inside web-inf other inside java/src, it was supposed to be only present at java/src. two files were not required.

Third, but not mandatory, before deployment just check if log4j2.xml is included inside web-inf/classes or not, if not then simply add it.

that's it, after following this issue was resolved now am getting proper logs.