'logback AmqpAppender json format ( sending only "message" to rabbit)

Hope you are doing well.

I am using AmqpAppender to send the logs in json format to RabbitMQ. when consuming the message from RabbitMQ I get:

{
  "timestamp" : "2022-04-30 15:46:08.633",
  "level" : "ERROR",
  "thread" : "main",
  "logger" : "com.behsa.dsdp.mciofferinquiry.SdpMciSiebelValidateServiceApplication",
  "message" : "BaseErrorLogModel{mobileNumber='917852255', errorLevel='null', errorTrace='null', errorMessage='dcfvbgnm,', errorCode='0', additionalLog='null', pid='null', serviceName='null', setName='null', version='null', dateTime=null, ip='10.20.8.67', trackCode='dfghjkcvbgnmj', elapsed=null, execLog='null', comment='Soli'}",
  "context" : "default"
}

I just only need the field "message" to be sent to RabbitMQ. By the way I also need the value of "message" to be json format.

what should I do? any suggestions?

here is my configuration :

<filter class="ch.qos.logback.classic.filter.LevelFilter">
    <level>ERROR</level>
    <onMatch>ACCEPT</onMatch>
    <onMismatch>DENY</onMismatch>
</filter>
<layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
    <jsonFormatter
            class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
        <prettyPrint>true</prettyPrint>
    </jsonFormatter>
    <timestampFormat>yyyy-MM-dd' 'HH:mm:ss.SSS</timestampFormat>
</layout>
<exchangeName>${rabbit.exchange.name}</exchangeName>
<host>${rabbit.host}</host>
<port>5672</port>
<username>${rabbit.username}</username>
<password>${rabbit.password}</password>
<exchangeType>${rabbit.exchange.type}</exchangeType>
<routingKeyPattern>${rabbit.routine.key}</routingKeyPattern>
<charset>UTF-8</charset>

and my appender class:

public class LogbackRabbitAppender extends AmqpAppender {
    private static final Logger LOGGER = LoggerFactory.getLogger(LogbackRabbitAppender.class);

    @Override
    public void setPassword(String password) {
        try {
            super.setPassword(CipherUtil.decrypt(password));
        } catch (CoreException e) {
            LOGGER.warn("failed to decrypt password: ", e);
        }
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source