Home >Java >javaTutorial >How to set log level for logging mechanism in Java function?
The methods for setting the log level for the logging mechanism in Java functions are: setting the log level through the setLevel() or LogManager.setLevel() method. Log levels include: OFF, SEVERE, WARNING, INFO, CONFIG, FINE, FINER and FINEST, where INFO and its upper levels are logged to the log file.
When logging in Java, the log level can be used to control the output to the log file. Record quantity and type. Here's how to set the log level for the logging mechanism in a Java function:
import java.util.logging.*;
Logger logger = Logger.getLogger("myLogger");
You can set the log level by one of the following methods:
logger.setLevel(Level.INFO);
LogManager.getLogManager().getLogger("myLogger").setLevel(Level.INFO);
Log level:
Consider the following Java function, It needs to record logs during the call:
public static void doSomething() { // 记录 INFO 级别的日志 logger.info("Doing something important"); }
To set the logging mechanism in this function to only output INFO level or higher logging, use the following code:
Logger logger = Logger.getLogger("myLogger"); logger.setLevel(Level.INFO);
The above is the detailed content of How to set log level for logging mechanism in Java function?. For more information, please follow other related articles on the PHP Chinese website!