Home > Software design >  log.error is not getting printed when one of the parameters is Null
log.error is not getting printed when one of the parameters is Null

Time:11-30

I am facing a very peculiar issue with slf4j log in my SpringBoot application. Code example -

@RestControllerAdvice
@Slf4j
public class ControllerAdvice{

    public ResponseEntity getErrors(String status, String source, String uid, String res) {
        ...
        ...
        log.error("Error is {} source, uid, res: {} | {} | {}", status, source, uid, res);
        ...
        ...
    }
}

Here whenever any of parameter(status, source, uid or res) is null the entire log is getting skipped. Is there any way we can print the value, even if it is null?

The dependencies I am using are below -

//sfl4j
compile("org.slf4j:slf4j-api:1.7.32")

//lombok
compileOnly("org.projectlombok:lombok:1.18.2")
annotationProcessor("org.projectlombok:lombok:1.18.8")

CodePudding user response:

Try with String.format(...).

Like this : log.error(String.format("Error is %s source, uid, res: %s | %s | %s", status, source, uid, res));

CodePudding user response:

I was able to get success by moving from Slf4j to Log4j2. That is, @Log4j2.

  • Related