2

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

Techfist
  • 4,314
  • 6
  • 22
  • 32

2 Answers2

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.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • is my configuration correct, the steps which i followed? – Techfist Feb 05 '15 at 16:31
  • It's correct (I didn't parse the XML, but assuming it's valid) for your program's logging, but it does nothing for your Servlet container or any other libraries' logging. You have to bridge the two. – Sotirios Delimanolis Feb 05 '15 at 16:33
  • am sorry but can you please elaborate a way? am little new to this, and am not that familiar with bridging concept ! – Techfist Feb 05 '15 at 16:36
  • A bridge is an adapter. It adapts one logging framework to another. Again, I'm not sure what logging framework your container uses, but if it used Commons Logging, you'd use [this](http://logging.apache.org/log4j/log4j-2.1/log4j-jcl/index.html). If you scroll down in that same page, and look in the menu on the left for _Adapter_, you should find one appropriate to you. – Sotirios Delimanolis Feb 05 '15 at 16:40
  • actually I have included that already, log4j-web-2.1.jar this guy for web application in my step 1, if i look to server start-up log in console(using tomcat 8) this is a line am seeing "2015-02-05 22:11:55,089 ERROR Console contains an invalid element or attribute "pattern"". is this something which might be causing this issue? – Techfist Feb 05 '15 at 16:51
  • @Techfist Is the XML you've shown in your question the exact same as the one you are trying to run with? – Sotirios Delimanolis Feb 05 '15 at 16:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70321/discussion-between-techfist-and-sotirios-delimanolis). – Techfist Feb 05 '15 at 17:03
0

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.

Techfist
  • 4,314
  • 6
  • 22
  • 32