LogRecord getLoggerName() method in Java with Examples


The getLoggerName() method is a part of the java.util.logging.LogRecord class in Java. It is used to get the name of the logger associated with the log record.

Syntax:

public String getLoggerName()

Returns: - A string representing the name of the logger associated with the log record.

Example 1:

import java.util.logging.*;

public class Example {
    private static final Logger LOGGER = Logger.getLogger(Example.class.getName());

    public static void main(String[] args) {
        LOGGER.info("This is a log message");
        LogRecord record = new LogRecord(Level.INFO, "Another log message");
        String loggerName = record.getLoggerName();
        System.out.println("Logger name: " + loggerName);
    }
}

Output:

Logger name: com.example.Example

In the above example, we have created a Logger object with the name com.example.Example. We then log a message using this logger and create a LogRecord object with another log message. We then use the getLoggerName() method to get the name of the logger associated with this log record, which is com.example.Example.

Example 2:

import java.util.logging.*;

public class Example {
    private static final Logger LOGGER = Logger.getLogger("com.example");

    public static void main(String[] args) {
        LOGGER.info("This is a log message");
        LogRecord record = new LogRecord(Level.INFO, "Another log message");
        String loggerName = record.getLoggerName();
        System.out.println("Logger name: " + loggerName);
    }
}

Output:

Logger name: com.example

In this example, we have created a Logger object with the name com.example. We then log a message using this logger and create a LogRecord object with another log message. We then use the getLoggerName() method to get the name of the logger associated with this log record, which is com.example.



About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.