Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Logger info(String) method in Java with Examples

  • Last Updated : 26 Mar, 2019

The info() method of a Logger class used to Log an INFO message.This method is used to forward logs to all the registered output Handler objects.

INFO message: Info is for the use of administrators or advanced users. It denotes mostly the actions that have lead to a change in state for the application.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

There are two types of info() method depending upon no of parameter passed.

  1. info(String msg): This method is used to forward the string passed as paratemer to method to all the registered output Handler objects.



    Syntax:

    public void info(String msg)
    

    Parameters: This method accepts a single parameter String which is information we want to pass to logs.

    Return value: This method returns nothing.

    Below programs illustrate info(String msg) method:

    Program 1:




    // Java program to demonstrate
    // Logger.info(String msg) method
      
    import java.util.logging.Logger;
      
    public class GFG {
      
        public static void main(String[] args)
        {
      
            // Create a Logger
            Logger logger
                = Logger.getLogger(
                    GFG.class.getName());
      
            // Call info method
            logger.info("This is message 1");
            logger.info("This is message 2");
            logger.info("This is message 3");
            logger.info("This is message 4");
        }
    }

    The output printed on eclipse ide is shown below.
    Output:

    Program 2:




    // Java program to demonstrate
    // Logger.info(String msg) method
      
    import java.util.logging.Logger;
      
    public class GFG {
      
        public static void main(String[] args)
        {
      
            // Create a Logger
            Logger logger
                = Logger
                      .getLogger("com.api.jar");
      
            // Call info method
            logger.info("Welcome to gfg");
            logger.info("google of codes");
        }
    }

    The output printed on IDE is shown below.
    Output:

  2. info(Supplier msgSupplier): This method is used Log an INFO 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 INFO 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 info(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 info(String msg) method:
    Program 1:




    // Java program to demonstrate
    // Logger.info(Supplier) method
      
    import java.util.logging.Logger;
    import java.util.function.Supplier;
      
    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");
      
            // Call info(Supplier<String>)
            logger.info(StrSupplier);
        }
    }

    The output printed on eclipse ide is shown below.
    Output:

References:




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!