From ad73b953e9a04c6d6accc5ab69f1229284c08ea5 Mon Sep 17 00:00:00 2001 From: marko-bekhta Date: Wed, 20 Nov 2024 18:25:20 +0100 Subject: [PATCH] HSEARCH-798 Collect and report log levels for each category --- .../loggers/LoggerCategoriesProcessor.java | 32 ++++++++++++++++--- .../loggers/LoggerCategoriesReporter.java | 12 +++++++ .../build/report/loggers/ReportConstants.java | 1 + 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/build/config/src/main/java/org/hibernate/search/build/report/loggers/LoggerCategoriesProcessor.java b/build/config/src/main/java/org/hibernate/search/build/report/loggers/LoggerCategoriesProcessor.java index 0bb679eb3ef..f0c605ef9c7 100644 --- a/build/config/src/main/java/org/hibernate/search/build/report/loggers/LoggerCategoriesProcessor.java +++ b/build/config/src/main/java/org/hibernate/search/build/report/loggers/LoggerCategoriesProcessor.java @@ -12,6 +12,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; @@ -45,6 +46,7 @@ public class LoggerCategoriesProcessor extends AbstractProcessor { private Messager messager; private final Map> categories = new TreeMap<>(); + private final Map> categoryLevels = new TreeMap<>(); private String moduleName; @Override @@ -83,6 +85,9 @@ public boolean process(Set annotations, RoundEnvironment "Logger %s either has some log-message methods or extends a BasicLogger, but does not provide any description on what it is used for." .formatted( category ) ); } + + categoryLevels.computeIfAbsent( category, k -> new TreeSet<>() ) + .addAll( loggingLevels( logger ) ); } } } @@ -112,7 +117,7 @@ public boolean process(Set annotations, RoundEnvironment ReportConstants.ROOT, Map.of( ReportConstants.MODULE_NAME, moduleName, - ReportConstants.CATEGORIES, toYamlCategories( categories ) + ReportConstants.CATEGORIES, toYamlCategories( categories, categoryLevels ) ) ), writer @@ -132,12 +137,13 @@ ReportConstants.CATEGORIES, toYamlCategories( categories ) return false; } - private List> toYamlCategories(Map> categories) { + private List> toYamlCategories(Map> categories, Map> levels) { List> values = new ArrayList<>(); for ( var entry : categories.entrySet() ) { Map value = new HashMap<>(); value.put( ReportConstants.CATEGORY_NAME, entry.getKey() ); value.put( ReportConstants.CATEGORY_DESCRIPTION, new ArrayList<>( entry.getValue() ) ); + value.put( ReportConstants.LOG_LEVELS, new ArrayList<>( levels.getOrDefault( entry.getKey(), Set.of() ) ) ); values.add( value ); } @@ -156,14 +162,32 @@ private boolean hasLoggingMethods(TypeElement logger) { return false; } + private Set loggingLevels(TypeElement logger) { + Set levels = new TreeSet<>(); + for ( Element element : processingEnv.getElementUtils().getAllMembers( logger ) ) { + if ( element.getKind() == ElementKind.METHOD ) { + ExecutableElement executable = (ExecutableElement) element; + Optional logMessage = getLogMessage( executable ); + + logMessage + .ifPresent( annotationMirror -> levels.add( getAnnotationValueAsString( annotationMirror, "level" ) ) ); + } + } + return levels; + } + private boolean hasLoggingAnnotation(ExecutableElement executable) { + return getLogMessage( executable ).isPresent(); + } + + private Optional getLogMessage(ExecutableElement executable) { for ( AnnotationMirror am : executable.getAnnotationMirrors() ) { if ( ( (TypeElement) am.getAnnotationType().asElement() ).getQualifiedName() .contentEquals( "org.jboss.logging.annotations.LogMessage" ) ) { - return true; + return Optional.of( am ); } } - return false; + return Optional.empty(); } private boolean isVoid(ExecutableElement executable) { diff --git a/build/config/src/main/java/org/hibernate/search/build/report/loggers/LoggerCategoriesReporter.java b/build/config/src/main/java/org/hibernate/search/build/report/loggers/LoggerCategoriesReporter.java index 246ce423e56..161c6085bdf 100644 --- a/build/config/src/main/java/org/hibernate/search/build/report/loggers/LoggerCategoriesReporter.java +++ b/build/config/src/main/java/org/hibernate/search/build/report/loggers/LoggerCategoriesReporter.java @@ -20,6 +20,7 @@ import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; +import java.util.stream.Collectors; import org.yaml.snakeyaml.Yaml; @@ -74,6 +75,11 @@ public static void main(String[] args) throws IOException { if ( descr != null ) { c.descriptions.addAll( descr ); } + + List levels = (List) category.get( ReportConstants.LOG_LEVELS ); + if ( descr != null ) { + c.levels.addAll( levels ); + } } } } @@ -92,6 +98,11 @@ public static void main(String[] args) throws IOException { for ( String module : category.modules ) { writer.write( "* `%s`\n".formatted( module ) ); } + if ( !category.levels.isEmpty() ) { + writer.write( "Produces messages with log levels:::\n" ); + writer.write( category.levels.stream().map( "`%s`"::formatted ).collect( Collectors.joining(", ")) ); + writer.write( "\n" ); + } } } } @@ -100,6 +111,7 @@ private static class Category { String name; Set descriptions = new TreeSet<>(); Set modules = new TreeSet<>(); + Set levels = new TreeSet<>(); public Category(String name) { this.name = name; diff --git a/build/config/src/main/java/org/hibernate/search/build/report/loggers/ReportConstants.java b/build/config/src/main/java/org/hibernate/search/build/report/loggers/ReportConstants.java index 431a3c9350c..0e5f357f425 100644 --- a/build/config/src/main/java/org/hibernate/search/build/report/loggers/ReportConstants.java +++ b/build/config/src/main/java/org/hibernate/search/build/report/loggers/ReportConstants.java @@ -8,6 +8,7 @@ class ReportConstants { static final String ROOT = "org.hibernate.search.report"; static final String MODULE_NAME = "module"; + static final String LOG_LEVELS = "levels"; static final String CATEGORIES = "categories"; static final String CATEGORY_NAME = "name"; static final String CATEGORY_DESCRIPTION = "description";