0

I have tried those approaches stated on this question and they don't work for me for some reasons. Here is a model class:

@JsonIgnoreProperties(ignoreUnknown = true)
@AllArgsConstructor
public class LogMessage {    

  @Getter @Setter private String timestamp;
  @Getter @Setter private String level;
  private Object message;
  @Getter @Setter private String loggername;
  @Getter @Setter private String thread;

  @JsonSetter("message")
  public void setMessage(Object data)
  {
    this.message = data;
  }

  @JsonRawValue
  public String getMessage() {
    return this.message == null ? null : this.message.toString();
  }
}

And the message field may be a JSON data and the parsing part code:

    ObjectMapper objectMapper = new ObjectMapper();
    try {
        Stream<String> stream = Files.lines(Paths.get(this.logFileName));
        List<LogMessage> sm = stream.filter(PATTERN_MAP.get(logLevel).asPredicate()).map(m -> {
            try {
                return objectMapper.readValue(m, LogMessage.class);
            } catch (IOException e) {
                logger.error("Parsing error");
                return null;
            }
        }).collect(Collectors.toList());
        return new ResponseEntity<>(sm, HttpStatus.OK);
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
        return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);
    }

And there are two lines of sample data which fails on the parsing:

{"level": "INFO", "timestamp": "2017-12-18T10:22:04.582","thread":"[Timer-0]", "loggername":" aspchina.service.ChoreographerSupportImpl.sendRequest(415)", "message": "RequestBody : {"action":{"actor":{"id":"FR2AR203","class":"unitOrConn"},"status":"stopped"}}"

No object is created with the above data.

vic
  • 2,548
  • 9
  • 44
  • 74

0 Answers0