System.getLogger() in Java

System.getLogger(): This method is available in java.lang.System class of Java.

Syntax:

Logger java.lang.System.getLogger(String name)

This method takes one argument of type String as its parameter. This method returns an instance of Logger for the caller's use.

Parameters: One parameter is required for this method.

name The name of the logger.

Returns: An instance of Logger that can be used by the calling class.

Throws:

1. NullPointerException - if the name is null.

2. IllegalCallerException - if there is no Java caller frame on the stack.

Approach 1: When no exceptions.

Java

public class SystemgetLogger {
    public static void main(String[] args) {

        String name = "os.name";
        System.out.println(System.getLogger(name));
    }
}

Output:

sun.util.logging.internal.LoggingProviderImpl$JULWrapper@50f8360d


Approach 2: NullPointerException 

Java

public class SystemgetLogger {
    public static void main(String[] args) {

        String name = null;
        System.out.println(System.getLogger(name));
    }
}


Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.util.Objects.requireNonNull(Objects.java:208) at java.base/java.lang.System.getLogger(System.java:1674)


No comments:

Post a Comment