Java - Logging using java.util.logging
by
Jeremy Canfield |
Updated: February 23 2023
| Java articles
There are various ways to go about append events to a custom log file in Java.
- Using BufferedOutputStream and FileOutputStream
- Using Log4j
- Using java.util.logging (this article)
Here is an example of how to append events to an application servers log using the java.util.logging logger.
import java.util.logging.*;
public class Main {
private static final 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.
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