Skip to content
Related Articles

Related Articles

Improve Article

Logger config() method in Java with Examples

  • Last Updated : 26 Mar, 2019
Geek Week

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 are two types of config() method depending upon the number of parameters passed.

  1. config(String msg): This method is used to log a CONFIG message. If the logger is enabled for logging CONFIG level message then the given message is forwarded to all the registered output Handler objects.

    Syntax:



    public void config(String msg)
    

    Parameters: This method accepts a single parameter String which is the string message.

    Return value: This method returns nothing.

    Below programs illustrate config(String msg) method:

    Program 1:




    // Java program to demonstrate
    // Logger.config(String msg) method
      
    import java.io.IOException;
    import java.util.logging.*;
      
    public class GFG {
      
        public static void main(String[] args)
            throws SecurityException, IOException
        {
      
            // Create a Logger
            Logger logger
                = Logger.getLogger(
                    GFG.class.getName());
      
            // Create a file handler object
            FileHandler handler
                = new FileHandler("logs.txt");
      
            // Add file handler as
            // handler of logs
            logger.addHandler(handler);
      
            // Set Logger level()
            logger.setLevel(Level.CONFIG);
      
            // Call config method
            logger.config("Set Geeks=CODING");
        }
    }

    The output printed on logs.txt file is shown below.
    Output:

    Program 2:




    // Java program to demonstrate
    // Logger.config(String msg) method
      
    import java.io.IOException;
    import java.util.logging.*;
      
    public class GFG {
      
        public static void main(String[] args)
            throws SecurityException, IOException
        {
      
            // Create a Logger
            Logger logger
                = Logger.getLogger(
                    GFG.class.getName());
      
            // Create a file handler object
            FileHandler handler
                = new FileHandler("logs.txt");
      
            // Add file handler as
            // handler of logs
            logger.addHandler(handler);
      
            // Set Logger level()
            logger.setLevel(Level.CONFIG);
      
            // Call config method
            logger.config("This is config message 1");
            logger.config("This is config message 2");
        }
    }

    The output printed on logs.txt file is shown below.
    Output:

  2. config(Supplier msgSupplier): This method is used Log a CONFIG 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 CONFIG 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 config(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 config(Supplier msgSupplier) method:

    Program 1:




    // Java program to demonstrate
    // Logger.config(Supplier<String>) method
      
    import java.io.IOException;
    import java.util.function.Supplier;
    import java.util.logging.*;
      
    public class GFG {
      
        public static void main(String[] args)
            throws SecurityException, IOException
        {
      
            // Create a Logger
            Logger logger
                = Logger.getLogger(
                    GFG.class.getName());
      
            // Create a file handler object
            FileHandler handler
                = new FileHandler("logs.txt");
      
            // Add file handler as
            // handler of logs
            logger.addHandler(handler);
      
            // Set Logger level()
            logger.setLevel(Level.CONFIG);
      
            // Create a supplier<String> method
            Supplier<String> StrSupplier
                = () -> new String("Welcome to GFG");
      
            // Call config(Supplier<String>)
            logger.config(StrSupplier);
        }
    }

    The output printed on log.txt is shown below.
    Output:

References:

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :