java.util.logging keeps you from having to tote one more jar file around with your application, and it works well with a good Formatter.

In general, at the top of every class, you should have:

Copyprivate static final Logger LOGGER = Logger.getLogger( ClassName.class.getName() );

Then, you can just use various facilities of the Logger class.


Use Level.FINE for anything that is debugging at the top level of execution flow:

CopyLOGGER.log( Level.FINE, "processing {0} entries in loop", list.size() );

Use Level.FINER / Level.FINEST inside of loops and in places where you may not always need to see that much detail when debugging basic flow issues:

CopyLOGGER.log( Level.FINER, "processing[{0}]: {1}", new Object[]{ i, list.get(i) } );

Use the parameterized versions of the logging facilities to keep from generating tons of String concatenation garbage that GC will have to keep up with. Object[] as above is cheap, on the stack allocation usually.


With exception handling, always log the complete exception details:

Copytry {
    ...something that can throw an ignorable exception
} catch( Exception ex ) {
    LOGGER.log( Level.SEVERE, ex.toString(), ex );
}

I always pass ex.toString() as the message here, because then when I "grep -n" for "Exception" in log files, I can see the message too. Otherwise, it is going to be on the next line of output generated by the stack dump, and you have to have a more advanced RegEx to match that line too, which often gets you more output than you need to look through.

Answer from grwww on Stack Overflow
Top answer
1 of 3
391

java.util.logging keeps you from having to tote one more jar file around with your application, and it works well with a good Formatter.

In general, at the top of every class, you should have:

Copyprivate static final Logger LOGGER = Logger.getLogger( ClassName.class.getName() );

Then, you can just use various facilities of the Logger class.


Use Level.FINE for anything that is debugging at the top level of execution flow:

CopyLOGGER.log( Level.FINE, "processing {0} entries in loop", list.size() );

Use Level.FINER / Level.FINEST inside of loops and in places where you may not always need to see that much detail when debugging basic flow issues:

CopyLOGGER.log( Level.FINER, "processing[{0}]: {1}", new Object[]{ i, list.get(i) } );

Use the parameterized versions of the logging facilities to keep from generating tons of String concatenation garbage that GC will have to keep up with. Object[] as above is cheap, on the stack allocation usually.


With exception handling, always log the complete exception details:

Copytry {
    ...something that can throw an ignorable exception
} catch( Exception ex ) {
    LOGGER.log( Level.SEVERE, ex.toString(), ex );
}

I always pass ex.toString() as the message here, because then when I "grep -n" for "Exception" in log files, I can see the message too. Otherwise, it is going to be on the next line of output generated by the stack dump, and you have to have a more advanced RegEx to match that line too, which often gets you more output than you need to look through.

2 of 3
72

Should declare logger like this:

Copyprivate final static Logger LOGGER = Logger.getLogger(MyClass.class.getName());

so if you refactor your class name it follows.

I wrote an article about java logger with examples here.

🌐
DigitalOcean
digitalocean.com › community › tutorials › logger-in-java-logging-example
Java Logger: Logging Examples with Log4j & SLF4J | DigitalOcean
August 3, 2022 - Implement logging in Java with Logger, Log4j, and SLF4J. Complete guide covering configuration, log levels, best practices, and real-world examples.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › logging › Logger.html
Logger (Java Platform SE 8 )
April 21, 2026 - Java™ Platform Standard Ed. 8 ... A Logger object is used to log messages for a specific system or application component. Loggers are normally named, using a hierarchical dot-separated namespace. Logger names can be arbitrary strings, but they should normally be based on the package name or class name of the logged component, such as java.net or javax.swing.
🌐
GeeksforGeeks
geeksforgeeks.org › java › logging-in-java
Logging in Java - GeeksforGeeks
January 23, 2026 - LOGGER.severe(): logs a SEVERE message for a serious error. All log messages are handled and displayed by Java’s centralized logging system automatically.
🌐
Baeldung
baeldung.com › home › logging › introduction to java logging
Introduction to Java Logging | Baeldung
February 11, 2026 - In our example, the pattern is set based on the pattern param, where %d determines the date pattern, %p the log level output, %m the output of the logged message, and %n adds a new line symbol. More info about patterns can be found on the official Log4j 2 page. Finally, to enable an appender (or multiple), we need to add it to the <Loggers> section:
Find elsewhere
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › core › java-logging-overview.html
Java Logging Overview
October 20, 2025 - Logger objects are normally named entities, using dot-separated names such as java.awt. The namespace is hierarchical and is managed by the LogManager. The namespace should typically be aligned with the Java packaging namespace, but is not required to follow it exactly. For example, a Logger called java.awt might handle logging requests for classes in the java.awt package, but it might also handle logging for classes in sun.awt that support the client-visible abstractions defined in the java.awt package.
🌐
Medium
medium.com › @AlexanderObregon › javas-logger-log-method-explained-699ab4997825
Java’s Logger.log() Method Explained | Medium
November 7, 2024 - For example, resource constraints or minor failures. SEVERE: Indicates serious problems that may cause application breakdowns. ... import java.util.logging.Level; public class LoggingLevelsExample { private static final Logger logger = ...
🌐
Mkyong
mkyong.com › home › logging › java logging apis tutorial
Java Logging APIs Tutorial - Mkyong.com
June 10, 2021 - package com.mkyong; import java.util.logging.Level; import java.util.logging.Logger; public class HelloWorld2 { private static Logger logger = Logger.getLogger(HelloWorld.class.getName()); public static void main(String[] args) { // set log level to SEVERE, default level info logger.setLevel(Level.SEVERE); // Log a info level msg logger.info("This is level info logging"); logger.log(Level.WARNING, "This is level warning logging"); logger.log(Level.SEVERE, "This is level severe logging"); System.out.println("Hello Java Logging APIs."); } }
🌐
Dash0
dash0.com › home › faq › java util logging jul the complete guide to built in java logging
Java Util Logging (JUL): The Complete Guide to Built-in Java Logging · Dash0
May 5, 2026 - Java Util Logging (JUL), introduced in Java 1.4, is the native logging API built directly into the Java standard library. As part of the java.util.logging package, JUL provides developers with a ready-to-use logging solution without requiring external dependencies, making it an attractive option ...
🌐
Sentry
blog.sentry.io › a-guide-to-logging-and-debugging-in-java
Logging & Debugging in Java - Guide | Sentry Blog
September 25, 2025 - You can use these log levels with the following syntax: logger.<LEVEL>("<YOUR_MESSAGE_HERE>"). You may need to add the following to your module-info.java file to allow logging:
🌐
GeeksforGeeks
geeksforgeeks.org › java › logger-log-method-in-java-with-examples
Logger log() Method in Java with Examples - GeeksforGeeks
July 12, 2025 - // Java program to demonstrate ... GFG { public static void main(String[] args) { // Create a Logger Logger logger = Logger.getLogger( GFG.class.getName()); // log messages using // log(Level level, String msg, Object param1) ...
🌐
Kdgregory
kdgregory.com › index.php
Effective Logging
This article covers techniques to make logging more useful as a debugging tool, and less intrusive to the logical flow of your program code.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.util.logging.logger
Logger Class (Java.Util.Logging) | Microsoft Learn
During formatting, if the logger has (or inherits) a localization ResourceBundle and if the ResourceBundle has a mapping for the msg string, then the msg string is replaced by the localized value. Otherwise the original msg string is used. Typically, formatters use java.text.MessageFormat style formatting to format parameters, so for example ...
🌐
Programiz
programiz.com › java-programming › logging
Java Logging
The figure below represents the core components and the flow of control of the Java Logging API (java.util.logging). ... The Logger class provides methods for logging. We can instantiate objects from the Logger class and call its methods for logging purposes. Let's take an example.
🌐
Scribd
scribd.com › document › 451757803 › Java-Logging-Framework
Java Logging Framework Overview | PDF
JavaScript is disabled in your browser · Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
🌐
Baeldung
baeldung.com › home › java › core java › java platform logging api
Java Platform Logging API | Baeldung
January 5, 2024 - We’ve shown in this article how to create a custom logger in Java 9 by using the new Platform Logging API. Also, we’ve implemented an example using an external logging framework, which is one of the most useful use cases of this new API.
🌐
Reddit
reddit.com › r/java › a guide to logging in java
r/java on Reddit: A Guide to Logging in Java
September 13, 2018 - So now I know how the logger is called, but I am in a different line and have to go back ← ← ← to where I was before, so I can finally type LOGGER.debug("I don't really remember what I wanted to put in here");.