Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ITEM-241 : Ajout appender personnalisé #58

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,75 @@
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 {

// Utilisation d'un ThreadLocal pour stocker l'heure de début de chaque méthode
private static final ThreadLocal<Long> startTime = new ThreadLocal<>();

// Seuil en ms au dessus duquel on veut afficher un message
private final int msSeuilDetection = 0;

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

// Méthode à appeler pour démarrer le chronométrage
public static void startMethodExecution() {
startTime.set(System.currentTimeMillis());
}

/**
* Appender permettant une normalisation des logs
* Pour obtenir le temps d'execution d'une méthode, ajoutez
* ItemLogAppender.startMethodExecution();
* au début d'une méthode. L'appender calcule par méthode sans
* besoin d'une borne d'arrêt ou de fin
* @param event
*/
@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) {
// Calcul du temps d'exécution
Long startTimeValue = startTime.get();
long executionTime = startTimeValue != null ? System.currentTimeMillis() - startTimeValue : -1;

if (executionTime < msSeuilDetection) {
System.out.println("DEM_" + typeDemande + "_" + demandeId + " / "
+ event.getSource().getClassName() + " / "
+ event.getSource().getMethodName() + " : "
+ originalMessage);
}
//Uniquement si un ItemLogAppender.startMethodExecution(); à été placé au début d'une méthode
else{
System.out.println("DEM_" + typeDemande + "_" + demandeId + " / "
+ event.getSource().getClassName() + " / "
+ event.getSource().getMethodName() + " : "
+ originalMessage + " / Execution time: " + executionTime + " ms");
}
}
}
}

// 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);
}
}
Loading