Read the Java Logging Overview by Oracle.
Configuring a Logger Inline in Code
import javafx.application.Application;
import javafx.stage.Stage;
import java.util.logging.*;
public class Main extends Application {
private static final Logger logger = Logger.getLogger(Main.class.getName());
@Override
public void start(Stage primaryStage) throws Exception {
logger.log(Level.INFO, "App started. Message's visible");
logger.log(Level.FINE, "Message's not visible");
}
public static void main(String[] args) {
logger.setLevel(Level.FINE);
Handler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(Level.FINE);
logger.addHandler(consoleHandler);
launch(args);
}
}
Note that the level is configured on both the logger and a handler.
Information on how to do this came from:
Using a File or a Class to Configure the Logger
Note that the sample I provided above is for an inline code based approach. It is also possible to define the logging configuration using a logging configuration class or an external file (or class path resource). To do this refer to the LogManager documentation and the Java Logging Overview previously linked.
The LogManager defines two optional system properties that allow control over the initial configuration:
- "java.util.logging.config.class"
- "java.util.logging.config.file"
These two properties may be specified on the command line to the "java" command.
If the "java.util.logging.config.class" property is set, then the
property value is treated as a class name. The given class will be
loaded, an object will be instantiated, and that object's constructor
is responsible for reading in the initial configuration. (That object
may use other system properties to control its configuration.) The
alternate configuration class can use readConfiguration(InputStream)
to define properties in the LogManager.
If "java.util.logging.config.class" property is not set, then the
"java.util.logging.config.file" system property can be used to specify
a properties file (in java.util.Properties format). The initial
logging configuration will be read from this file.
If neither of these properties is defined then the LogManager uses its
default configuration. The default configuration is typically loaded
from the properties file "lib/logging.properties" in the Java
installation directory.