- log(Level level, String msg): This method is used to Log a message, with no arguments.only message will be written in logger Output.
Syntax:
public void log(Level level, String msg)
Parameters: This method accepts two parameters level which is one of the message level identifiers, e.g., SEVERE and msg which is the string message (or a key in the message catalog).
Return value: This method returns nothing
Program 1: Method log(Level level, String msg)
import java.util.logging.Level;
import java.util.logging.Logger;
public class GFG {
public static void main(String[] args)
{
Logger logger
= Logger.getLogger(
GFG. class .getName());
logger.log(Level.INFO, "This is message 1" );
logger.log(Level.WARNING, "This is message 2" );
}
}
|
Output
- log(Level level, String msg, Object param1): This method is used to Log a message, with one object parameter.
Syntax:
public void log(Level level, String msg, Object param1)
Parameters: This method accepts three parameters level which is one of the message level identifiers, e.g., SEVERE, msg which is the string message (or a key in the message catalog) and param1 which is parameter to the message
Return value: This method returns nothing
Program 2: Method log(Level level, String msg, Object param1)
import java.util.logging.Level;
import java.util.logging.Logger;
public class GFG {
public static void main(String[] args)
{
Logger logger
= Logger.getLogger(
GFG. class .getName());
logger.log(Level.INFO, "logging: {0} " , "message1" );
logger.log(Level.SEVERE, "logging: {0} " , "message2" );
}
}
|
Output:
- log(Level level, String msg, Object[] params): This method is used to Log a message, with an array of object arguments.
Syntax:
public void log(Level level, String msg, Object[] params)
Parameters: This method accepts three parameters level which is one of the message level identifiers, e.g., SEVERE, msg which is the string message (or a key in the message catalog) and param1 which is array of parameters to the message
Return value: This method returns nothing
Program 3: Method log(Level level, String msg, Object[] param1)
import java.util.logging.Level;
import java.util.logging.Logger;
public class GFG {
public static void main(String[] args)
{
Logger logger
= Logger.getLogger(
GFG. class .getName());
logger.log(Level.INFO, "logging: {0} {1}" ,
new Object[] { "parameter1" , "parameter2" });
logger.log(Level.WARNING, "logging: {0} {1} {2}" ,
new Object[] { "p1" , "p2" , "p3" });
}
}
|
Output:
- log(Level level, String msg, Throwable thrown): This method is used to Log a message, with associated Throwable information.
Syntax:
public void log(Level level, String msg, Throwable thrown)
Parameters: This method accepts three parameters level which is one of the message level identifiers, e.g., SEVERE, msg which is the string message (or a key in the message catalog) and thrown which is Throwable associated with log message.
Return value: This method returns nothing
Program 4: Method log(Level level, String msg, Throwable thrown)
import java.util.logging.Level;
import java.util.logging.Logger;
public class GFG {
public static void main(String[] args)
{
Logger logger
= Logger.getLogger(
GFG. class .getName());
logger.log(Level.SEVERE, "logging:" ,
new RuntimeException( "Error" ));
logger.log(Level.WARNING, "logging: " ,
new Exception( "Exception" ));
}
}
|
Output:
- log(Level level, Throwable thrown, Supplier msgSupplier): This method is used to Log a lazily constructed message, with associated Throwable information.The message and the given Throwable are then stored in a LogRecord which is forwarded to all registered Output handlers.
Syntax:
public void log(Level level, Throwable thrown, Supplier msgSupplier)
Parameters: This method accepts three parameters level which is one of the message level identifiers, e.g., SEVERE, thrown which is the Throwable associated with log message and msgSupplier which is a function, which when called, produces the desired log message.
Return value: This method returns nothing
Program 5: Method log(Level level, Throwable thrown, Supplier msgSupplier)
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
public class GFG {
public static void main(String[] args)
{
Logger logger
= Logger.getLogger(
GFG. class .getName());
Supplier<String> StrSupplier
= () -> new String( "Logger logs" );
logger.log(Level.SEVERE,
new RuntimeException( "Error" ),
StrSupplier);
}
}
|
Output:
- log(Level level, Supplier msgSupplier): This method is used to Log a message, which is only to be constructed if the logging level is such that the message will actually be logged.
Syntax:
public void log(Level level, Supplier msgSupplier)
Parameters: This method accepts two parameters level which is one of the message level identifiers, e.g., SEVERE and msgSupplier which is a function, which when called, produces the desired log message.
Return value: This method returns nothing
Program 6: Method log(Level level, Supplier msgSupplier)
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
public class GFG {
public static void main(String[] args)
{
Logger logger
= Logger.getLogger(
GFG. class .getName());
Supplier<String> StrSupplier
= () -> new String( "Logger messages" );
logger.log(Level.SEVERE,
StrSupplier);
}
}
|
Output:
- log(LogRecord record): This method is used to Log a LogRecord.Using logRecord we will log the info to logger Outputs.
Syntax:
public void log(LogRecord record)
Parameters: This method accepts one parameter record which is the LogRecord to be published.
Return value: This method returns nothing
Program 7: Method log(LogRecord record)
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
public class GFG {
public static void main(String[] args)
{
Logger logger
= Logger.getLogger(
GFG. class .getName());
LogRecord record1 = new LogRecord(Level.INFO,
"Msg 1" );
LogRecord record2 = new LogRecord(Level.INFO,
"Msg 2" );
logger.log(record1);
logger.log(record2);
}
}
|
Output: