LogRecord setMessage() method in Java with Examples

The setMessage() method of java.util.logging.LogRecord is used to set the “raw” log message, before localization or formatting for this LogRecord object.we can also set null as raw message.

Syntax:

public void setMessage(String message)

Parameters: This method accepts message which is the raw message string.



Return: This method returns nothing.

Below programs illustrate setMessage() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate setMessage() method
  
import java.util.logging.Level;
import java.util.logging.LogRecord;
  
import java.util.logging.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Create LogRecord object
        LogRecord logRecord = new LogRecord(
            Level.parse("800"),
            "Hi Logger");
  
        System.out.println(
            "Previous Message: "
            + logRecord.getMessage());
  
        // set message of the LogRecord
        logRecord.setMessage("GFG Logger");
  
        // print result
        System.out.println(
            "New Message: "
            + logRecord.getMessage());
    }
}

chevron_right


Output:

Previous Message: Hi Logger
New Message: GFG Logger

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate setMessage() method
  
import java.util.logging.Level;
import java.util.logging.LogRecord;
  
import java.util.logging.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Create LogRecord object
        LogRecord logRecord = new LogRecord(
            Level.INFO,
            "GFG Logger");
  
        System.out.println(
            "Previous Message: "
            + logRecord.getMessage());
  
        // set message of the LogRecord
        logRecord.setMessage(null);
  
        // print result
        System.out.println(
            "New Message: "
            + logRecord.getMessage());
    }
}

chevron_right


Output:

Previous Message: GFG Logger
New Message: null

References: https://docs.oracle.com/javase/10/docs/api/java/util/logging/LogRecord.html#setMessage(java.lang.String)




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.