-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathExceptionHighlighter.java
More file actions
40 lines (32 loc) · 1.5 KB
/
ExceptionHighlighter.java
File metadata and controls
40 lines (32 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.github.stickerifier.stickerify.logger;
import static ch.qos.logback.core.pattern.color.ANSIConstants.RED_FG;
import static ch.qos.logback.core.pattern.color.ANSIConstants.RESET;
import static com.github.stickerifier.stickerify.logger.HighlightHelper.changeColorTo;
import static com.github.stickerifier.stickerify.logger.HighlightHelper.greenHighlight;
import static com.github.stickerifier.stickerify.logger.HighlightHelper.replaceFirst;
import static com.github.stickerifier.stickerify.logger.HighlightHelper.retrieveMimeType;
import ch.qos.logback.classic.pattern.ThrowableProxyConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.pattern.Converter;
/**
* Custom converter class to be used by Logback to highlight important substrings in exception logs.
*
* @see Converter
*/
public class ExceptionHighlighter extends ThrowableProxyConverter {
static final String CONTINUE_RED = changeColorTo(RESET + RED_FG);
@Override
public String convert(ILoggingEvent event) {
var fullMessage = super.convert(event);
var throwable = event.getThrowableProxy();
if (throwable != null && throwable.getMessage() != null) {
var exceptionMessage = throwable.getMessage();
var mimeType = retrieveMimeType(exceptionMessage);
if (mimeType != null) {
var highlightedMessage = replaceFirst(exceptionMessage, mimeType, greenHighlight(mimeType, CONTINUE_RED));
return replaceFirst(fullMessage, exceptionMessage, highlightedMessage);
}
}
return fullMessage;
}
}