All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.breuninger.boot.jobs.service.JobMessageLogAppender Maven / Gradle / Ivy

There is a newer version: 2.0.5.RELEASE
Show newest version
package com.breuninger.boot.jobs.service;

import static com.breuninger.boot.jobs.domain.JobMessage.jobMessage;
import static com.breuninger.boot.jobs.domain.Level.ERROR;
import static com.breuninger.boot.jobs.domain.Level.INFO;
import static com.breuninger.boot.jobs.domain.Level.WARNING;

import java.time.OffsetDateTime;

import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.breuninger.boot.jobs.domain.JobMarker;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;

@Component
public class JobMessageLogAppender extends AppenderBase {

  private final JobService jobService;

  public JobMessageLogAppender(final JobService jobService) {
    this.jobService = jobService;

    final var lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    setContext(lc);
    start();

    lc.getLogger("ROOT").addAppender(this);
  }

  @Override
  protected void append(final ILoggingEvent eventObject) {
    final var mdcMap = eventObject.getMDCPropertyMap();
    // TODO: check for JOB marker:
    if (mdcMap.containsKey("job_id") && eventObject.getMarker() != null && JobMarker.JOB.contains(eventObject.getMarker())) {
      final var jobId = mdcMap.get("job_id");
      final var level = eventObject.getLevel();
      final var breuningerLevel = logLevelToBreuningerLevel(level);

      final var message = eventObject.getFormattedMessage();

      try {
        final var jobMessage = jobMessage(breuningerLevel, message, OffsetDateTime.now());
        jobService.appendMessage(jobId, jobMessage);
      } catch (final RuntimeException e) {
        addError("Failed to persist job message (jobId=" + jobId + "): " + message, e);
      }
    }
  }

  private com.breuninger.boot.jobs.domain.Level logLevelToBreuningerLevel(final Level level) {
    switch (level.levelStr) {
      case "ERROR":
        return ERROR;
      case "WARN":
        return WARNING;
      default:
        return INFO;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy