-1

I am doing a Simple demo project in Servlet 3.0 and Tomcat
I am accepting JSON request from Postman and providing JSON response .
Now i also want to do logging in my project .

So i have use log4j2
Jars used :-
log4j-1.2.12.jar ,
jackson-databind-2.6.3.jar ,
jackson-core-2.6.3.jar

Servlet Code :-

@WebServlet("/StatusServlet")
public class TestingServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    Logger logger = Logger.getLogger(TestingServlet.class);

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub


        BasicConfigurator.configure();

        String requestData = request.getReader().lines().collect(Collectors.joining());
        System.out.println(requestData);

        // 2. initiate jackson mapper

            ObjectMapper mapper = new ObjectMapper();

            TestingPojo pojo = mapper.readValue(requestData, TestingPojo.class);

           logger.info("status "+pojo.getStatus());
            PrintWriter out = response.getWriter();
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            mapper.writeValue(out, pojo.getStatus());
            out.flush(); 


    }

My Project Directory :-

Webservice            
   |-src         
      |- com       
          |-test          
             TestingPojo.java               
             TestingServlet.java       
             log4j2.xml

My log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Configuration status="TRACE">
    <Properties>
        <Property name="logPath">/tmp</Property>
        <!-- <Property name="patternInfo">%d{dd/MMM/yyyy HH:mm:ss,SSS} %F : %C 
            : %M() : THREAD[%t] : LINE[%L] : [%m]%n</Property> -->
        <Property name="patternInfo">%d{dd/MMM/yyyy HH:mm:ss,SSS} %m%n</Property>
    </Properties>
    <Appenders>
        <RollingFile name="RollingFile" fileName="${logPath}/i-am.log"
            filePattern="${logPath}/webservice-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout pattern="${patternInfo}" />
            <Policies>
                <SizeBasedTriggeringPolicy size="10MB" />
                <TimeBasedTriggeringPolicy interval="1"
                    modulate="true" />
            </Policies>
            <!-- <DefaultRolloverStrategy max="10 MB"/> -->
        </RollingFile>
    </Appenders>
    <Loggers>
        <Logger name="root" level="debug" additivity="false">
            <appender-ref ref="RollingFile" level="debug" />
        </Logger>
        <Root level="TRACE" additivity="false">
            <AppenderRef ref="RollingFile" />
        </Root>
    </Loggers>
</Configuration>

When i run the code in eclipse i get logs in console in pattern format .
But when i deploy the code in remote server i get no logs in mentioned Rolling File Appender path . No file gets created.

Is there anything wrong with my configurations ?
Also I have used Basic Configurator.configure() , Is there any other way ?

  • I don't know about Tomcat but for most servers the logging configuration is centralized so every application doesn't have to configure its own log files – Guillaume Jun 13 '19 at 08:39

1 Answers1

0

I found same article for you.(You need to change directory for log4j2.xml)

Click here!

There are two way explained,

  1. placed log4j2.xml in class path(recommand)
  2. run tomcat with logging jvm option

This is out of OP's question, but I hope you remind this.

  • Please consider to add console Appender, it would be nice to see in your IDE console.

  • Please consider re-use ObjectMapper : Jackson-Performance , javadoc

jornathan
  • 646
  • 5
  • 13