Skip to content

Commit 7ea10b1

Browse files
committed
Remove ANSI escape code prior to emitting with Logback appender
This closes #1915
1 parent cccacb0 commit 7ea10b1

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

org.eclipse.m2e.logback.feature/feature.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<feature
33
id="org.eclipse.m2e.logback.feature"
44
label="%featureName"
5-
version="2.7.100.qualifier"
5+
version="2.7.101.qualifier"
66
provider-name="%providerName"
77
plugin="org.eclipse.m2e.core"
88
license-feature="org.eclipse.license"

org.eclipse.m2e.logback/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-SymbolicName: org.eclipse.m2e.logback;singleton:=true
4-
Bundle-Version: 2.7.100.qualifier
4+
Bundle-Version: 2.7.101.qualifier
55
Bundle-Name: M2E Logback Appender and Configuration
66
Bundle-Vendor: Eclipse.org - m2e
77
Bundle-RequiredExecutionEnvironment: JavaSE-21

org.eclipse.m2e.logback/src/org/eclipse/m2e/logback/appender/EclipseLogAppender.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public class EclipseLogAppender extends UnsynchronizedAppenderBase<ILoggingEvent
2929

3030
private static final ILog ECLIPSE_LOG = Platform.getLog(EclipseLogAppender.class);
3131

32+
// ANSI escape code pattern: matches ESC followed by [ and various control sequences
33+
private static final java.util.regex.Pattern ANSI_PATTERN = java.util.regex.Pattern.compile("\u001B\\[[;\\d]*m");
34+
3235
@Override
3336
protected void append(ILoggingEvent logEvent) {
3437
int severity = switch(logEvent.getLevel().levelInt) {
@@ -38,7 +41,8 @@ protected void append(ILoggingEvent logEvent) {
3841
default -> -1;
3942
};
4043
if(severity != -1) {
41-
IStatus status = new Status(severity, BUNDLE_ID, logEvent.getFormattedMessage().strip(), getThrowable(logEvent));
44+
IStatus status = new Status(severity, BUNDLE_ID, removeAnsiCodes(logEvent.getFormattedMessage()).strip(),
45+
getThrowable(logEvent));
4246
ECLIPSE_LOG.log(status);
4347
}
4448
}
@@ -50,4 +54,17 @@ private static Throwable getThrowable(ILoggingEvent logEvent) {
5054
Object[] args = logEvent.getArgumentArray();
5155
return args != null && args.length > 0 && args[args.length - 1] instanceof Throwable throwable ? throwable : null;
5256
}
57+
58+
/**
59+
* Removes ANSI escape codes from the given string.
60+
*
61+
* @param text the text that may contain ANSI codes
62+
* @return the text with all ANSI codes removed, or null if input is null
63+
*/
64+
private static String removeAnsiCodes(String text) {
65+
if(text == null) {
66+
return null;
67+
}
68+
return ANSI_PATTERN.matcher(text).replaceAll("");
69+
}
5370
}

0 commit comments

Comments
 (0)