Skip to content

Commit

Permalink
HSEARCH-798 Collect and report log levels for each category
Browse files Browse the repository at this point in the history
  • Loading branch information
marko-bekhta committed Nov 20, 2024
1 parent 863fd58 commit 3473cba
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,6 +46,7 @@ public class LoggerCategoriesProcessor extends AbstractProcessor {

private Messager messager;
private final Map<String, Set<String>> categories = new TreeMap<>();
private final Map<String, Set<String>> categoryLevels = new TreeMap<>();
private String moduleName;

@Override
Expand Down Expand Up @@ -83,6 +85,9 @@ public boolean process(Set<? extends TypeElement> 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 ) );
}
}
}
Expand Down Expand Up @@ -112,7 +117,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
ReportConstants.ROOT,
Map.of(
ReportConstants.MODULE_NAME, moduleName,
ReportConstants.CATEGORIES, toYamlCategories( categories )
ReportConstants.CATEGORIES, toYamlCategories( categories, categoryLevels )
)
),
writer
Expand All @@ -132,12 +137,13 @@ ReportConstants.CATEGORIES, toYamlCategories( categories )
return false;
}

private List<Map<String, Object>> toYamlCategories(Map<String, Set<String>> categories) {
private List<Map<String, Object>> toYamlCategories(Map<String, Set<String>> categories, Map<String, Set<String>> levels) {
List<Map<String, Object>> values = new ArrayList<>();
for ( var entry : categories.entrySet() ) {
Map<String, Object> 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 );
}
Expand All @@ -156,14 +162,32 @@ private boolean hasLoggingMethods(TypeElement logger) {
return false;
}

private Set<String> loggingLevels(TypeElement logger) {
Set<String> levels = new TreeSet<>();
for ( Element element : processingEnv.getElementUtils().getAllMembers( logger ) ) {
if ( element.getKind() == ElementKind.METHOD ) {
ExecutableElement executable = (ExecutableElement) element;
Optional<AnnotationMirror> logMessage = getLogMessage( executable );

logMessage
.ifPresent( annotationMirror -> levels.add( getAnnotationValueAsString( annotationMirror, "level" ) ) );
}
}
return levels;
}

private boolean hasLoggingAnnotation(ExecutableElement executable) {
return getLogMessage( executable ).isPresent();
}

private Optional<AnnotationMirror> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -74,6 +75,11 @@ public static void main(String[] args) throws IOException {
if ( descr != null ) {
c.descriptions.addAll( descr );
}

List<String> levels = (List<String>) category.get( ReportConstants.LOG_LEVELS );
if ( descr != null ) {
c.levels.addAll( levels );
}
}
}
}
Expand All @@ -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" );
}
}
}
}
Expand All @@ -100,6 +111,7 @@ private static class Category {
String name;
Set<String> descriptions = new TreeSet<>();
Set<String> modules = new TreeSet<>();
Set<String> levels = new TreeSet<>();

public Category(String name) {
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 3473cba

Please sign in to comment.