Monday, July 6, 2026
HomeLanguagesJavaLogRecord setMessage() method in Java with Examples

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:




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


Output:

Previous Message: Hi Logger
New Message: GFG Logger

Program 2:




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


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)

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32519 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6900 POSTS0 COMMENTS
Nicole Veronica
12015 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12110 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7263 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6968 POSTS0 COMMENTS