How to set the logging level in Java logger?

In Java, we can use the Logger class to achieve logging. The Logger class is a class provided by the Java standard library for logging in a program. Inside the Logger, there is a Level class used for representing different logging levels.

Here are the steps for setting the print level of Logger.

  1. Firstly, you need to import the java.util.logging.Logger class and the java.util.logging.Level class.
import java.util.logging.Logger;
import java.util.logging.Level;
  1. Create a Logger object.
Logger logger = Logger.getLogger("myLogger");
  1. Set the logging level of the Logger.
logger.setLevel(Level.INFO);

In the above code, we set the Logger’s print level to INFO. This means that only log messages at INFO level and above will be recorded.

Level class provides several commonly used logging levels:

  1. Turn off logging.
  2. SEVERE: The log level for serious errors.
  3. WARNING: Alert log level.
  4. INFO: Information log level.
  5. CONFIG: Set log level.
  6. FINE: Detailed logging level.
  7. FINER: Increased level of detail in logging.
  8. FINEST: The most detailed log level.
  9. Enable all logging.

You can select the appropriate log level to record log as needed.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds