Skip to content

Commit

Permalink
readability
Browse files Browse the repository at this point in the history
  • Loading branch information
q3769 committed Mar 17, 2023
1 parent fc64556 commit 47fe93b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class GroupPattern implements LogPattern {
*/
@Nonnull
public static GroupPattern from(@NonNull String pattern) {
return new GroupPattern(PatternType.parseAllPatternsOrThrow(pattern));
return new GroupPattern(PatternType.parsePatternGroup(pattern));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ public boolean includeCallerThread() {
@Override
public void render(LogEntry logEntry, StringBuilder logTextBuilder) {
logTextBuilder.append(logEntry.getResolvedMessage());
if (logEntry.getException() == null) {
Throwable t = logEntry.getException();
if (t == null) {
return;
}
logTextBuilder.append(System.lineSeparator()).append(StackTraceUtils.stackTraceTextOf(logEntry.getException()));
logTextBuilder.append(System.lineSeparator()).append(StackTraceUtils.stackTraceTextOf(t));
}
}
70 changes: 35 additions & 35 deletions src/main/java/elf4j/impl/core/writer/pattern/PatternType.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,18 @@
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Objects;

/**
*
*/
public enum PatternType {
enum PatternType {
/**
*
*/
TIMESTAMP {
@Override
LogPattern parsePattern(String pattern) {
return this.isTargetTypeOf(pattern) ? TimestampPattern.from(pattern) : null;
LogPattern translate(String pattern) {
return TimestampPattern.from(pattern);
}

@Override
Expand All @@ -58,8 +57,8 @@ public boolean isTargetTypeOf(String pattern) {
}

@Override
LogPattern parsePattern(String pattern) {
return this.isTargetTypeOf(pattern) ? LevelPattern.from(pattern) : null;
LogPattern translate(String pattern) {
return LevelPattern.from(pattern);
}
},
/**
Expand All @@ -72,86 +71,87 @@ public boolean isTargetTypeOf(String pattern) {
}

@Override
LogPattern parsePattern(String pattern) {
return this.isTargetTypeOf(pattern) ? ThreadPattern.from(pattern) : null;
LogPattern translate(String pattern) {
return ThreadPattern.from(pattern);
}
},
/**
*
*/
CLASS {
@Override
public boolean isTargetTypeOf(String pattern) {
boolean isTargetTypeOf(String pattern) {
return isPatternOfType(this, pattern);
}

@Override
LogPattern parsePattern(String pattern) {
return this.isTargetTypeOf(pattern) ? ClassPattern.from(pattern) : null;
LogPattern translate(String pattern) {
return ClassPattern.from(pattern);
}
},
/**
*
*/
METHOD {
@Override
public boolean isTargetTypeOf(String pattern) {
boolean isTargetTypeOf(String pattern) {
return isPatternOfType(this, pattern);
}

@Override
LogPattern parsePattern(String pattern) {
return this.isTargetTypeOf(pattern) ? MethodPattern.from(pattern) : null;
LogPattern translate(String pattern) {
return MethodPattern.from(pattern);
}
},
/**
*
*/
MESSAGE {
@Override
public boolean isTargetTypeOf(String pattern) {
boolean isTargetTypeOf(String pattern) {
return isPatternOfType(this, pattern);
}

@Override
LogPattern parsePattern(String pattern) {
return this.isTargetTypeOf(pattern) ? MessageAndExceptionPattern.from(pattern) : null;
LogPattern translate(String pattern) {
return MessageAndExceptionPattern.from(pattern);
}
},
/**
*
*/
JSON {
@Override
public boolean isTargetTypeOf(String pattern) {
boolean isTargetTypeOf(String pattern) {
return isPatternOfType(this, pattern);
}

@Override
LogPattern parsePattern(String pattern) {
return this.isTargetTypeOf(pattern) ? JsonPattern.from(pattern) : null;
LogPattern translate(String pattern) {
return JsonPattern.from(pattern);
}
},
/**
*
*/
VERBATIM {
@Override
public boolean isTargetTypeOf(String pattern) {
boolean isTargetTypeOf(String pattern) {
return isPatternOfType(this, pattern);
}

@Override
LogPattern parsePattern(String pattern) {
return this.isTargetTypeOf(pattern) ? VerbatimPattern.from(pattern) : null;
LogPattern translate(String pattern) {
return VerbatimPattern.from(pattern);
}
};
private static final EnumSet<PatternType> PREDEFINED_PATTERN_TYPES = EnumSet.complementOf(EnumSet.of(VERBATIM));

/**
* @param pattern entire layout pattern text of a writer, including one or more individual pattern segments
* @return ordered list of individual patterns forming the entire layout pattern of the writer
*/
public static List<LogPattern> parseAllPatternsOrThrow(String pattern) {
static List<LogPattern> parsePatternGroup(String pattern) {
List<LogPattern> logPatterns = new ArrayList<>();
int length = pattern.length();
int i = 0;
Expand All @@ -174,32 +174,32 @@ public static List<LogPattern> parseAllPatternsOrThrow(String pattern) {
iPattern = pattern.substring(i, iEnd);
i = iEnd;
}
logPatterns.add(PatternType.parsePatternOrThrow(iPattern));
logPatterns.add(parsePattern(iPattern));
}
return logPatterns;
}

private static boolean isPatternOfType(PatternType targetPatternType, String pattern) {
if (targetPatternType == VERBATIM) {
return EnumSet.complementOf(EnumSet.of(VERBATIM)).stream().noneMatch(type -> type.isTargetTypeOf(pattern));
private static boolean isPatternOfType(PatternType patternType, String pattern) {
if (patternType == VERBATIM) {
return PREDEFINED_PATTERN_TYPES.stream().noneMatch(type -> type.isTargetTypeOf(pattern));
}
return targetPatternType.name().equalsIgnoreCase(pattern.split(":", 2)[0].trim());
return patternType.name().equalsIgnoreCase(pattern.split(":", 2)[0].trim());
}

private static LogPattern parsePatternOrThrow(String pattern) {
private static LogPattern parsePattern(String pattern) {
return EnumSet.allOf(PatternType.class)
.stream()
.map(type -> type.parsePattern(pattern))
.filter(Objects::nonNull)
.filter(type -> type.isTargetTypeOf(pattern))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("pattern: '" + pattern + "' not parsable"));
.orElseThrow(() -> new IllegalArgumentException("pattern: '" + pattern + "' not parsable"))
.translate(pattern);
}

/**
* @param pattern text configuration of an individual pattern segment
* @return true if this pattern type is the target type of the specified pattern text
*/
public abstract boolean isTargetTypeOf(String pattern);
abstract boolean isTargetTypeOf(String pattern);

abstract LogPattern parsePattern(String pattern);
abstract LogPattern translate(String pattern);
}

0 comments on commit 47fe93b

Please sign in to comment.