Skip to content

Commit

Permalink
ITEM-241 : Ajout appender personnalisé
Browse files Browse the repository at this point in the history
Récupération numéro et type demande et concaténation dans chaque message envoyé dans les logs
  • Loading branch information
pierre-maraval committed Oct 15, 2024
1 parent 357e809 commit d1563e1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import fr.abes.item.core.service.ILigneFichierService;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.ThreadContext;
import org.hibernate.exception.ConstraintViolationException;
import org.hibernate.exception.JDBCConnectionException;
import org.springframework.batch.core.ExitStatus;
Expand Down Expand Up @@ -110,6 +111,8 @@ public ExitStatus afterStep(StepExecution stepExecution) {
stepExecution.getJobExecution().getExecutionContext().put("lignes", this.lignesFichier);
stepExecution.getJobExecution().getExecutionContext().put("demandeId", this.demandeId);
stepExecution.getJobExecution().getExecutionContext().put("typeDemande", this.typeDemande.toString());
ThreadContext.put("demandeId", String.valueOf(this.demandeId));
ThreadContext.put("typeDemande", this.typeDemande.toString());
}
return stepExecution.getExitStatus();
}
Expand Down
3 changes: 2 additions & 1 deletion batch/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
<PatternLayout
pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{dark,yellow}: %msg%n%throwable" />
</Console>
<ItemLogAppender name="ItemLogAppender" />
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
<Logger name="fr.abes.item" level="debug" additivity="false">
<AppenderRef ref="Console"/>
<AppenderRef ref="ItemLogAppender"/>
</Logger>
</Loggers>
</Configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package fr.abes.item.core.configuration;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.ThreadContext;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;

@Plugin(name = "ItemLogAppender", category = "core", elementType = Appender.ELEMENT_TYPE)
public class ItemLogAppender extends AbstractAppender {

protected ItemLogAppender(String name, Filter filter) {
super(name, filter, null);
}

@Override
public void append(LogEvent event) {
if (event.getLevel().isMoreSpecificThan(Level.INFO)) {
String originalMessage = event.getMessage().getFormattedMessage();
String demandeId = ThreadContext.get("demandeId");
String typeDemande = ThreadContext.get("typeDemande");
if (demandeId != null && typeDemande != null) {
System.out.println("DEM_" + typeDemande + "_" + demandeId + " : " + originalMessage);
}
}
}

// Méthode statique pour créer l'instance de l'appender via le fichier de configuration
@PluginFactory
public static ItemLogAppender createAppender(
@PluginAttribute("name") String name,
@PluginElement("Filter") Filter filter) {
return new ItemLogAppender(name, filter);
}
}

0 comments on commit d1563e1

Please sign in to comment.