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

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

import java.util.logging.Level;
import java.util.logging.Logger;

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


public class LoggingServlet extends HttpServlet {
    private static final String CONTENT_TYPE = "text/html; charset=ISO-8859-1";
    Logger logger = Logger.getLogger(LoggingServlet.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>LoggingServlet</title></head>");
        out.println("<body>");
        out.println("<a href='log4j'>Test log4j logging</a>");
        out.println("<p>This servlet has logged messages using JDK loggers. Please access service logs for the log records.</p>");


        logger.info("This is the information.");
        logger.warning("This is some warning.");
        
        try {
            throw new Exception("Trace example: I will not appear in the log.");
        } catch (Throwable e) {
            logger.setLevel(Level.INFO);
            logger.throwing(LoggingServlet.class.getName(), "doGet", e);
        }


        try {
            throw new Exception("Trace example: Level has to be finer for me to appear in the user log.");
        } catch (Throwable e) {
            logger.setLevel(Level.FINER);
            logger.throwing(LoggingServlet.class.getName(), "doGet", e);
           
        }

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