Logger log() Method in Java with Examples Last Updated : 27 Jun, 2019 Comments Improve Suggest changes Like Article Like Report The log() method of Logger is used to Log a message. If the logger is currently enabled for the given message level which is passed as parameter then a corresponding LogRecord is created and forwarded to all the registered Output Handler objects. But in logger class, there are seven different log() method depending upon the parameters passed to the method. 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) Java // Java program to demonstrate // Logger.log(Level level, String msg) method import java.util.logging.Level; import java.util.logging.Logger; public class GFG { public static void main(String[] args) { // Create a Logger Logger logger = Logger.getLogger( GFG.class.getName()); // log messages using log(Level level, String msg) 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) Java // Java program to demonstrate // Logger.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) { // Create a Logger Logger logger = Logger.getLogger( GFG.class.getName()); // log messages using // log(Level level, String msg, Object param1) 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) Java // Java program to demonstrate // Logger.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) { // Create a Logger Logger logger = Logger.getLogger( GFG.class.getName()); // log messages using // log(Level level, String msg, Object[] param1) 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) Java // Java program to demonstrate // Logger.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) { // Create a Logger Logger logger = Logger.getLogger( GFG.class.getName()); // log messages using // log(Level level, String msg, Throwable thrown) 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) Java // Java program to demonstrate // Logger.log(Level level, Throwable thrown, Supplier<String> 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) { // Create a Logger Logger logger = Logger.getLogger( GFG.class.getName()); // Create a supplier<String> method Supplier<String> StrSupplier = () -> new String("Logger logs"); // log messages using // log(Level level, Throwable thrown, Supplier<String> msgSupplier) 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) Java // Java program to demonstrate // Logger.log(Level level, <String> 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) { // Create a Logger Logger logger = Logger.getLogger( GFG.class.getName()); // Create a supplier<String> method Supplier<String> StrSupplier = () -> new String("Logger messages"); // log messages using // log(Level level, Supplier<String> msgSupplier) 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) Java // Java program to demonstrate // Logger.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) { // Create a Logger Logger logger = Logger.getLogger( GFG.class.getName()); // create logRecords LogRecord record1 = new LogRecord(Level.INFO, "Msg 1"); LogRecord record2 = new LogRecord(Level.INFO, "Msg 2"); // log messages using // log(LogRecord record) logger.log(record1); logger.log(record2); } } Output: References: https://docs.oracle.com/javase/10/docs/api/java/util/Logger.html# Comment More infoAdvertise with us Next Article Logger log() Method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java Java - util package Java-Functions Java-Logger Practice Tags : Java Similar Reads Logger fine() method in Java with Examples The fine() method of a Logger class used to Log a FINE message. This method is used to pass FINE types logs to all the registered output Handler objects. FINE, FINER and FINEST provide tracking information as when what is happening/ has happened in our application. FINE displays the most important m 3 min read Logger finer() method in Java with Examples The finer() method of a Logger class used to Log an FINER message.This method is used to pass FINER types logs to all the registered output Handler objects. FINER message: FINER outputs a detailed tracing message and may include logging calls regarding method entering, exiting, throwing exceptions. 3 min read Logger config() method in Java with Examples The config() method of a Logger class used to Log an config message. This method is used to pass config types logs to all the registered output Handler objects. Config Level: Configuration Information may be like what CPU the application is running on, how much is the disk and memory space. There ar 3 min read Logger finest() method in Java with Examples The finest() method of a Logger class used to Log an FINEST message.This method is used to pass FINEST types logs to all the registered output Handler objects. FINEST message: FINEST provides highly detailed tracing message. There are two types of finest() method depending upon the number of the par 3 min read Logger severe() method in Java with Examples The severe() method of a Logger class used to Log a SEVERE message.This method is used to pass SEVERE types logs to all the registered output Handler objects. SEVERE Message: Severe occurs when something terrible has occurred and the application cannot continue further. Ex like database unavailable, 2 min read Like