Friday, November 21, 2025
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
32405 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6781 POSTS0 COMMENTS
Nicole Veronica
11928 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11995 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7166 POSTS0 COMMENTS
Thapelo Manthata
6862 POSTS0 COMMENTS
Umr Jansen
6847 POSTS0 COMMENTS