Logger warning() method in Java with Examples
The warning() method of a Logger class used to Log a WARNING message.This method is used to pass WARNING types logs to all the registered output Handler objects.
WARNING Message: Warning may occur whenever the user has given wrong input or credentials.
There are two types of warning() method depending upon no of the parameter passed.
-
warning(String msg): This method is used to log a WARNING message. If the logger is enabled for logging WARNING level message then the given message is forwarded to all the registered output Handler objects.
Syntax:
public void warning(String msg)
Parameters: This method accepts a single parameter String which is the string message.
Return value: This method returns nothing.
Below programs illustrate warning(String msg) method:
Program 1:
import java.io.IOException;
import java.util.logging.*;
public class GFG {
public static void main(String[] args)
throws SecurityException, IOException
{
Logger logger
= Logger.getLogger(
GFG. class .getName());
logger.setLevel(Level.WARNING);
logger.warning( "Set WARNING = ERRORS" );
}
}
|
The output printed on console is shown below.
Output:
-
warning(Supplier msgSupplier): This method is used Log a WARNING message, constructed only if the logging level is such that the message will actually be logged. It means If the logger is enabled for the WARNING message level then the message is constructed by invoking the provided supplier function and forwarded to all the registered output Handler objects.
Syntax:
public void warning(Supplier msgSupplier)
Parameters: This method accepts a single parameter msgSupplier which is a function, which when called, produces the desired log message.
Return value: This method returns nothing.
Below programs illustrate warning(Supplier msgSupplier) method:
Program 1:
import java.io.IOException;
import java.util.function.Supplier;
import java.util.logging.*;
public class GFG {
public static void main(String[] args)
throws SecurityException, IOException
{
Logger logger
= Logger.getLogger(
GFG. class .getName());
logger.setLevel(Level.WARNING);
Supplier<String> StrSupplier
= () -> new String( "WARNING WARNING WARNING" );
logger.warning(StrSupplier);
}
}
|
The output printed on console is shown below.
Output:
References:
Last Updated :
27 Mar, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...