1

I found this example of using java.util.logging online here. I don't understand how to modify it to use the sample across multiple classes
do I just declare this in every class?

Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);  

also in the class, they supplied there is a syntax error:

// suppress the logging output to the console
Logger rootLogger = Logger.*getLogger*("");

what is the correct way to get to the root logger?

package com.vogella.logger;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class MyLogger {
    static private FileHandler fileTxt;
    static private SimpleFormatter formatterTxt;

    static private FileHandler fileHTML;
    static private Formatter formatterHTML;

    static public void setup() throws IOException {

        // get the global logger to configure it
        Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);

        // suppress the logging output to the console
        Logger rootLogger = Logger.*getLogger*("");
        Handler[] handlers = rootLogger.getHandlers();
        if (handlers[0] instanceof ConsoleHandler) {
            rootLogger.removeHandler(handlers[0]);
        }

        logger.setLevel(Level.INFO);
        fileTxt = new FileHandler("Logging.txt");
        fileHTML = new FileHandler("Logging.html");

        // create a TXT formatter
        formatterTxt = new SimpleFormatter();
        fileTxt.setFormatter(formatterTxt);
        logger.addHandler(fileTxt);

        // create an HTML formatter
        formatterHTML = new MyHtmlFormatter();
        fileHTML.setFormatter(formatterHTML);
        logger.addHandler(fileHTML);
    }
}

1 Answer 1

2

Yes. Using GLOBAL_LOGGER_NAME as stated in the linked Javadoc as well serves as logging using the global logger from each class. Though the usefulness is not much as it is a default java logger provided.

GLOBAL_LOGGER_NAME is a name for the global logger. This name is provided as a convenience to developers who are making casual use of the Logging package....

The preferred way to get the global logger object is via the call Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).

It's also an alternate of Logger.getGlobal() from the older jdk versions.


To configure the root logger use :-

Logger logger = Logger.getLogger("");
Sign up to request clarification or add additional context in comments.

1 Comment

thank you sir, any idea about the other question (Logger rootLogger = Logger.*getLogger*("");)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.