Logger log() Method in Java with Examples

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.

  1. 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)

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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");
        }
    }

    chevron_right

    
    

    Output

  2. 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)

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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");
        }
    }

    chevron_right

    
    

    Output:

  3. 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)

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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" });
        }
    }

    chevron_right

    
    

    Output:

  4. 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)

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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"));
        }
    }

    chevron_right

    
    

    Output:


  5. 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)

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
        }
    }

    chevron_right

    
    

    Output:

  6. 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)


    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
        }
    }

    chevron_right

    
    

    Output:

  7. 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)

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
        }
    }

    chevron_right

    
    

    Output:

References: https://docs.oracle.com/javase/10/docs/api/java/util/Logger.html#



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.