1

I have a little JavaFX application where wan't to use java.util.logging. I have logged needed information to Level.INFO (currently visible) and Level.FINE (currently not visible) levels. When i trying to compile project via Intellij Idea, i see in console just Level.INFO logs.

Example of code.

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");
   }
}

How can i change the console default visible Level without writing for every logger lines of Handler code? Maybe some config-file in root?

Logs were written in style of grwww answer here, but there weren't said anything about changing visible Level.

Community
  • 1
  • 1
Kyryl Havrylenko
  • 674
  • 4
  • 11

1 Answers1

0

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.

Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • Thanks. Basically, i had to create "lib/logging.properties" in the root of the project and add to it `handlers= java.util.logging.ConsoleHandler` `.level= FINE` – Kyryl Havrylenko Mar 26 '16 at 23:11