Logger log() Method in Java with Examples

27 Jun, 2019
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 nothingProgram 1: Method log(Level level, String msg)// 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");    }}Outputlog(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 messageReturn value: This method returns nothingProgram 2: Method log(Level level, String msg, Object param1)// 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 messageReturn value: This method returns nothingProgram 3: Method log(Level level, String msg, Object[] param1)// 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 nothingProgram 4: Method log(Level level, String msg, Throwable thrown)// 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 nothingProgram 5: Method log(Level level, Throwable thrown, Supplier msgSupplier)// 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 nothingProgram 6: Method log(Level level, Supplier msgSupplier)// 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 nothingProgram 7: Method log(LogRecord record)// 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:
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 nothingProgram 1: Method log(Level level, String msg)// 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
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 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 messageReturn value: This method returns nothingProgram 2: Method log(Level level, String msg, Object param1)// 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:
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 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 messageReturn value: This method returns nothingProgram 3: Method log(Level level, String msg, Object[] param1)// 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:
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 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 nothingProgram 4: Method log(Level level, String msg, Throwable thrown)// 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:
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 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 nothingProgram 5: Method log(Level level, Throwable thrown, Supplier msgSupplier)// 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:
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 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 nothingProgram 6: Method log(Level level, Supplier msgSupplier)// 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:
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 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 nothingProgram 7: Method log(LogRecord record)// 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:
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 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#
Java - util package
Java-Functions
Java-Logger
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
ArrayList in Java
Stream In Java
Collections in Java
Multidimensional Arrays in Java
Singleton Class in Java
Stack Class in Java