
/*
 *  Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
*/
package mavenlogging;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;


public class Log4jLoggingServlet extends HttpServlet {
    private static final String CONTENT_TYPE = "text/html; charset=ISO-8859-1";
    Logger logger = Logger.getLogger(Log4jLoggingServlet.class.getName());

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType(CONTENT_TYPE);
        PrintWriter out = response.getWriter();
        
        
        out.println("<html>");
        out.println("<head><title>Log4jLoggingServlet</title></head>");
        out.println("<body>");
        out.println("<a href='jdk'>Test jdk logging</a>");
        out.println("<p>This servlet has logged messages using log4j loggers. Please access service logs for the log records.</p>");
       

        logger.info("This is the information.");
        logger.warn("This is some warning.");
        
       


        try {
            throw new Exception("Trace example: This is an exception. This will not be logged.");
        } catch (Throwable e) {
            logger.setLevel(Level.INFO);
            logger.trace(e.getMessage(), e);
        }
        
        try {
            throw new Exception("Trace example: This is an exception. This will be logged.");
        } catch (Throwable e) {
            
            logger.setLevel(Level.TRACE);
            logger.trace(e.getMessage(), e);
        }

        out.println("</body></html>");
        out.close();
    }
}
