Bootstrap FreeKB - Java - Append events to application server log
Java - Append events to application server log

Updated:   |  Java articles

System.out.println is used to display text on the console and to also write events to the JVMs log.


Servlet

For example, let's say you've the following servlet. Notice the doGet method contains System.out.println("Hello World");

package com.main;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      System.out.println("Hello World");
	}
}

 

When running this servlet in Eclipse, "Hello World" is displayed on the Console tab.

 

Likewise, "Hello World" will be written to the JVMs log.

[1/7/19 5:46:28:501 CST] 00000253 SystemOut  O Hello World

 


Bean/Class and JSP

System.out.println should append events to an application servers log. In this most basic example, "Hello World" should be appended to the application servers log.

public class Main {
	public static void main(String[] args) {
		System.out.println("Hello World");
	}
}

 

Which should result in something like this in the application servers log. Notice that the date time stamp is not included.

22-Feb-2023 02:38:42.309 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
22-Feb-2023 02:38:42.338 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["https-openssl-nio-8443"]
22-Feb-2023 02:38:42.345 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [722] milliseconds
Hello World

 

Typically, you are going to want the datetime stamp and log level (e.g. INFO WARNING ERROR) included. In this scenario, a Java Logger can be used. In this example, the java.util.logging logger is used.

import java.util.logging.*;

public class Main {

    Logger logger = Logger.getLogger (Main.class.getName());

	public static void main(String[] args) {
		logger.info("Hello World");
	}
}

 

The application server logs should have something like this now.

22-Feb-2023 02:51:23.233 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
22-Feb-2023 02:51:23.265 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["https-openssl-nio-8443"]
22-Feb-2023 02:51:23.272 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [798] milliseconds
22-Feb-2023 02:51:33.118 INFO [main] Main.main Hello World

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 50853d in the box below so that we can be sure you are a human.