Skip to content

Commit

Permalink
Remove JANSI dependency in 2.x (#3070)
Browse files Browse the repository at this point in the history
This commit:

- Removes support for the outdated [Jansi 1.x](http://fusesource.github.io/jansi/) version in `Console` appender.
- Rewrites `JAnsiTextRenderer`, use in the `%m{ansi}` and `%ex{ansi}` pattern converters to use our internal ANSI support instead of Jansi.

Fixes #1736.
  • Loading branch information
ppkarwasz authored and vy committed Oct 22, 2024
1 parent dfe0ada commit 9ef736f
Show file tree
Hide file tree
Showing 45 changed files with 513 additions and 831 deletions.

Large diffs are not rendered by default.

7 changes: 0 additions & 7 deletions log4j-core-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,6 @@
<scope>test</scope>
</dependency>

<!-- Required for console color support in Windows -->
<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
<scope>test</scope>
</dependency>

<!-- Used for JMS appenders (needs an implementation of course) -->
<dependency>
<groupId>javax.jms</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
public interface Layouts {
interface Csv {}

interface Jansi {}

interface Json {}

interface Xml {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
* integration tests, an appropriate category interface should be specified.
*/
@Export
@Version("2.20.1")
@Version("2.20.2")
@BaselineIgnore("2.25.0")
package org.apache.logging.log4j.core.test.categories;

import aQute.bnd.annotation.baseline.BaselineIgnore;
import org.osgi.annotation.bundle.Export;
import org.osgi.annotation.versioning.Version;
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,15 @@
* </p>
*
* <pre>
* java -classpath log4j-core\target\test-classes;log4j-core\target\classes;log4j-api\target\classes;%HOME%\.m2\repository\org\fusesource\jansi\jansi\1.14\jansi-1.14.jar; org.apache.logging.log4j.core.appender.ConsoleAppenderAnsiMessagesMain log4j-core/target/test-classes/log4j2-console.xml
* java -classpath log4j-core\target\test-classes;log4j-core\target\classes;log4j-api\target\classes org.apache.logging.log4j.core.appender.ConsoleAppenderAnsiMessagesMain log4j-core/target/test-classes/log4j2-console.xml
* </pre>
*/
public class ConsoleAppenderAnsiMessagesMain {

private static final Logger LOG = LogManager.getLogger(ConsoleAppenderAnsiMessagesMain.class);

public static void main(final String[] args) {
System.setProperty("log4j.skipJansi", "false"); // LOG4J2-2087: explicitly enable
try (final LoggerContext ctx = Configurator.initialize(
try (final LoggerContext ignored = Configurator.initialize(
ConsoleAppenderAnsiMessagesMain.class.getName(), "target/test-classes/log4j2-console.xml")) {
LOG.fatal("\u001b[1;35mFatal message.\u001b[0m");
LOG.error("\u001b[1;31mError message.\u001b[0m");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,22 @@
import org.apache.logging.log4j.core.config.Configurator;

/**
* Tests https://issues.apache.org/jira/browse/LOG4J2-180
* Tests <a href="https://issues.apache.org/jira/browse/LOG4J2-180">LOG4J2-180</a>
* <p>
* Running from a Windows command line from the root of the project:
* </p>
*
* <pre>
* java -classpath log4j-core\target\test-classes;log4j-core\target\classes;log4j-api\target\classes;%HOME%\.m2\repository\org\fusesource\jansi\jansi\1.14\jansi-1.14.jar; org.apache.logging.log4j.core.appender.ConsoleAppenderAnsiStyleJira180Main log4j-core/target/test-classes/log4j2-180.xml
* java -classpath log4j-core\target\test-classes;log4j-core\target\classes;log4j-api\target\classes org.apache.logging.log4j.core.appender.ConsoleAppenderAnsiStyleJira180Main log4j-core/target/test-classes/log4j2-180.xml
* </pre>
*/
public class ConsoleAppenderAnsiStyleJira180Main {

private static final Logger LOG = LogManager.getLogger(ConsoleAppenderAnsiStyleJira180Main.class);

public static void main(final String[] args) {
System.setProperty("log4j.skipJansi", "false"); // LOG4J2-2087: explicitly enable
// System.out.println(System.getProperty("java.class.path"));
final String config = args.length == 0 ? "target/test-classes/log4j2-180.xml" : args[0];
try (final LoggerContext ctx =
try (final LoggerContext ignored =
Configurator.initialize(ConsoleAppenderAnsiMessagesMain.class.getName(), config)) {
LOG.fatal("Fatal message.");
LOG.error("Error message.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,21 @@
import org.apache.logging.log4j.core.config.Configurator;

/**
* Tests https://issues.apache.org/jira/browse/LOG4J2-272
* Tests <a href="https://issues.apache.org/jira/browse/LOG4J2-272">LOG4J2-272</a>
* <p>
* Running from a Windows command line from the root of the project:
* </p>
* <pre>
* java -classpath log4j-core\target\test-classes;log4j-core\target\classes;log4j-api\target\classes;%HOME%\.m2\repository\org\fusesource\jansi\jansi\1.14\jansi-1.14.jar; org.apache.logging.log4j.core.appender.ConsoleAppenderAnsiStyleJira272Main log4j-core/target/test-classes/log4j2-272.xml
* java -classpath log4j-core\target\test-classes;log4j-core\target\classes;log4j-api\target\classes org.apache.logging.log4j.core.appender.ConsoleAppenderAnsiStyleJira272Main log4j-core/target/test-classes/log4j2-272.xml
* </pre>
*/
public class ConsoleAppenderAnsiStyleJira272Main {

private static final Logger LOG = LogManager.getLogger(ConsoleAppenderAnsiStyleJira272Main.class);

public static void main(final String[] args) {
System.setProperty("log4j.skipJansi", "false"); // LOG4J2-2087: explicitly enable
// System.out.println(System.getProperty("java.class.path"));
final String config = args.length == 0 ? "target/test-classes/log4j2-272.xml" : args[0];
try (final LoggerContext ctx =
try (final LoggerContext ignored =
Configurator.initialize(ConsoleAppenderAnsiMessagesMain.class.getName(), config)) {
LOG.fatal("Fatal message.");
LOG.error("Error message.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,22 @@
import org.apache.logging.log4j.core.config.Configurator;

/**
* Tests https://issues.apache.org/jira/browse/LOG4J2-319
* Tests <a href="https://issues.apache.org/jira/browse/LOG4J2-319">LOG4J2-319</a>
* <p>
* Running from a Windows command line from the root of the project:
* </p>
*
* <pre>
* java -classpath log4j-core\target\test-classes;log4j-core\target\classes;log4j-api\target\classes;%HOME%\.m2\repository\org\fusesource\jansi\jansi\1.14\jansi-1.14.jar; org.apache.logging.log4j.core.appender.ConsoleAppenderAnsiStyleJira319Main log4j-core/target/test-classes/log4j2-319.xml
* java -classpath log4j-core\target\test-classes;log4j-core\target\classes;log4j-api\target\classes org.apache.logging.log4j.core.appender.ConsoleAppenderAnsiStyleJira319Main log4j-core/target/test-classes/log4j2-319.xml
* </pre>
*/
public class ConsoleAppenderAnsiStyleJira319Main {

private static final Logger LOG = LogManager.getLogger(ConsoleAppenderAnsiStyleJira319Main.class);

public static void main(final String[] args) {
System.setProperty("log4j.skipJansi", "false"); // LOG4J2-2087: explicitly enable
// System.out.println(System.getProperty("java.class.path"));
final String config = args.length == 0 ? "target/test-classes/log4j2-319.xml" : args[0];
try (final LoggerContext ctx =
try (final LoggerContext ignored =
Configurator.initialize(ConsoleAppenderAnsiMessagesMain.class.getName(), config)) {
LOG.fatal("Fatal message.");
LOG.error("Error message.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* </pre>
* or:
* <pre>
* java -classpath log4j-core\target\test-classes;log4j-core\target\classes;log4j-api\target\classes;%HOME%\.m2\repository\org\fusesource\jansi\jansi\1.14\jansi-1.14.jar; org.apache.logging.log4j.core.appender.ConsoleAppenderAnsiStyleLayoutMain log4j-core/target/test-classes/log4j2-console-style-ansi.xml
* java -classpath log4j-core\target\test-classes;log4j-core\target\classes;log4j-api\target\classes org.apache.logging.log4j.core.appender.ConsoleAppenderAnsiStyleLayoutMain log4j-core/target/test-classes/log4j2-console-style-ansi.xml
* </pre>
*
*/
Expand All @@ -54,11 +54,9 @@ public void test() {
}

public void test(final String[] args) {
System.setProperty("log4j.skipJansi", "false"); // LOG4J2-2087: explicitly enable
// System.out.println(System.getProperty("java.class.path"));
final String config =
args == null || args.length == 0 ? "target/test-classes/log4j2-console-style-ansi.xml" : args[0];
try (final LoggerContext ctx =
try (final LoggerContext ignored =
Configurator.initialize(ConsoleAppenderAnsiMessagesMain.class.getName(), config)) {
final Logger logger = LogManager.getLogger(ConsoleAppenderAnsiStyleLayoutMain.class);
logger.fatal("Fatal message.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public class ConsoleAppenderAnsiStyleNameLayoutMain {
private static final Logger LOG = LogManager.getLogger(ConsoleAppenderAnsiStyleNameLayoutMain.class);

public static void main(final String[] args) {
System.setProperty("log4j.skipJansi", "false"); // LOG4J2-2087: explicitly enable
try (final LoggerContext ctx = Configurator.initialize(
try (final LoggerContext ignored = Configurator.initialize(
ConsoleAppenderAnsiMessagesMain.class.getName(),
"target/test-classes/log4j2-console-style-name-ansi.xml")) {
LOG.fatal("Fatal message.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.test.categories.Layouts;
import org.junit.Test;
import org.junit.experimental.categories.Category;

/**
* Shows how to use ANSI escape codes to color messages. Each message is printed to the console in color, but the rest
Expand All @@ -34,21 +32,20 @@
* </p>
*
* <pre>
* mvn -Dtest=org.apache.logging.log4j.core.appender.ConsoleAppenderJAnsiXExceptionMain test
* mvn -Dtest=org.apache.logging.log4j.core.appender.ConsoleAppenderAnsiXExceptionMain test
* </pre>
*
* or, on Windows:
*
* <pre>
* java -classpath log4j-core\target\test-classes;log4j-core\target\classes;log4j-api\target\classes;%USERPROFILE%\.m2\repository\org\fusesource\jansi\jansi\1.14\jansi-1.14.jar; org.apache.logging.log4j.core.appender.ConsoleAppenderJAnsiXExceptionMain log4j-core/src/test/resources/log4j2-console-xex-ansi.xml
* java -classpath log4j-core\target\test-classes;log4j-core\target\classes;log4j-api\target\classes org.apache.logging.log4j.core.appender.ConsoleAppenderAnsiXExceptionMain log4j-core/src/test/resources/log4j2-console-xex-ansi.xml
* </pre>
*
*/
@Category(Layouts.Jansi.class)
public class ConsoleAppenderJAnsiXExceptionMain {
public class ConsoleAppenderAnsiXExceptionMain {

public static void main(final String[] args) {
new ConsoleAppenderJAnsiXExceptionMain().test(args);
new ConsoleAppenderAnsiXExceptionMain().test(args);
}

/**
Expand All @@ -60,12 +57,10 @@ public void test() {
}

public void test(final String[] args) {
System.setProperty("log4j.skipJansi", "false"); // LOG4J2-2087: explicitly enable
// System.out.println(System.getProperty("java.class.path"));
final String config =
args == null || args.length == 0 ? "target/test-classes/log4j2-console-xex-ansi.xml" : args[0];
final LoggerContext ctx = Configurator.initialize(ConsoleAppenderAnsiMessagesMain.class.getName(), config);
final Logger logger = LogManager.getLogger(ConsoleAppenderJAnsiXExceptionMain.class);
final Logger logger = LogManager.getLogger(ConsoleAppenderAnsiXExceptionMain.class);
try {
Files.getFileStore(Paths.get("?BOGUS?"));
} catch (final Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* </p>
*
* <pre>
* java -classpath log4j-core\target\test-classes;log4j-core\target\classes;log4j-api\target\classes;%HOME%\.m2\repository\org\fusesource\jansi\jansi\1.14\jansi-1.14.jar; org.apache.logging.log4j.core.appender.ConsoleAppenderNoAnsiStyleLayoutMain log4j-core/target/test-classes/log4j2-console-style-ansi.xml
* java -classpath log4j-core\target\test-classes;log4j-core\target\classes;log4j-api\target\classes org.apache.logging.log4j.core.appender.ConsoleAppenderNoAnsiStyleLayoutMain log4j-core/target/test-classes/log4j2-console-style-ansi.xml
* </pre>
*/
public class ConsoleAppenderDefaultSuppressedThrowable {
Expand All @@ -41,12 +41,11 @@ public class ConsoleAppenderDefaultSuppressedThrowable {
public static void main(final String[] args) {
final String config =
args.length == 0 ? "target/test-classes/log4j2-console-default-suppressed-throwable.xml" : args[0];
test(args, config);
test(config);
}

static void test(final String[] args, final String config) {
// System.out.println(System.getProperty("java.class.path"));
try (final LoggerContext ctx =
static void test(final String config) {
try (final LoggerContext ignored =
Configurator.initialize(ConsoleAppenderDefaultSuppressedThrowable.class.getName(), config)) {
final IOException ioEx = new IOException("test suppressed");
ioEx.addSuppressed(new IOException("test suppressed 1", new IOException("test 1")));
Expand All @@ -55,8 +54,6 @@ static void test(final String[] args, final String config) {
ioEx.addSuppressed(new IOException("test suppressed 2", ioEx2));
final IOException e = new IOException("test", ioEx);
LOG.error("Error message {}, suppressed?", "Hi", e);
System.out.println("printStackTrace");
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public class ConsoleAppenderHighlightLayoutDefaultMain {
private static final Logger LOG = LogManager.getLogger(ConsoleAppenderHighlightLayoutDefaultMain.class);

public static void main(final String[] args) {
System.setProperty("log4j.skipJansi", "false"); // LOG4J2-2087: explicitly enable
try (final LoggerContext ctx = Configurator.initialize(
try (final LoggerContext ignored = Configurator.initialize(
ConsoleAppenderAnsiMessagesMain.class.getName(),
"target/test-classes/log4j2-console-highlight-default.xml")) {
LOG.fatal("Fatal message.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public class ConsoleAppenderHighlightLayoutMain {
private static final Logger LOG = LogManager.getLogger(ConsoleAppenderHighlightLayoutMain.class);

public static void main(final String[] args) {
System.setProperty("log4j.skipJansi", "false"); // LOG4J2-2087: explicitly enable
try (final LoggerContext ctx = Configurator.initialize(
try (final LoggerContext ignored = Configurator.initialize(
ConsoleAppenderAnsiMessagesMain.class.getName(), "target/test-classes/log4j2-console-highlight.xml")) {
LOG.fatal("Fatal message.");
LOG.error("Error message.");
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
public class ConsoleAppenderJira1002ShortThrowableLayoutMain {

public static void main(final String[] args) {
ConsoleAppenderNoAnsiStyleLayoutMain.test(args, "target/test-classes/log4j2-1002.xml");
public static void main() {
ConsoleAppenderNoAnsiStyleLayoutMain.test("target/test-classes/log4j2-1002.xml");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* </p>
*
* <pre>
* java -classpath log4j-core\target\test-classes;log4j-core\target\classes;log4j-api\target\classes;%HOME%\.m2\repository\org\fusesource\jansi\jansi\1.14\jansi-1.14.jar; org.apache.logging.log4j.core.appender.ConsoleAppenderNoAnsiStyleLayoutMain log4j-core/target/test-classes/log4j2-console-style-ansi.xml
* java -classpath log4j-core\target\test-classes;log4j-core\target\classes;log4j-api\target\classes org.apache.logging.log4j.core.appender.ConsoleAppenderNoAnsiStyleLayoutMain log4j-core/target/test-classes/log4j2-console-style-ansi.xml
* </pre>
*/
public class ConsoleAppenderNoAnsiStyleLayoutMain {
Expand All @@ -43,12 +43,11 @@ private static void logThrowableFromMethod() {

public static void main(final String[] args) {
final String config = args.length == 0 ? "target/test-classes/log4j2-console-style-no-ansi.xml" : args[0];
test(args, config);
test(config);
}

static void test(final String[] args, final String config) {
// System.out.println(System.getProperty("java.class.path"));
try (final LoggerContext ctx =
static void test(final String config) {
try (final LoggerContext ignored =
Configurator.initialize(ConsoleAppenderNoAnsiStyleLayoutMain.class.getName(), config)) {
LOG.fatal("Fatal message.");
LOG.error("Error message.");
Expand Down
Loading

0 comments on commit 9ef736f

Please sign in to comment.