Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split and modernize JANSI support. #2916

Merged
merged 8 commits into from
Oct 7, 2024
Merged

Split and modernize JANSI support. #2916

merged 8 commits into from
Oct 7, 2024

Conversation

ppkarwasz
Copy link
Contributor

@ppkarwasz ppkarwasz commented Sep 2, 2024

This PR:

  • Adds a new ConsoleStreamProvider plugin type that can decide how the Console Appender stream is built.
  • Moves JAnsi support to a new log4j-jansi module.
  • Upgrades the JAnsi library to version 2.x Replaces JAnsi with JLine 3, since JAnsi is deprecated (cf. Archive this repository, Jansi is part of Jline3 now fusesource/jansi#294).
  • Rewrites JAnsiTextRenderer to use our internal AnsiEscape utility.

Fixes #1736.

This commit:

- Adds a new `ConsoleStreamProvider` plugin type that can decide how the Console Appender stream is built.
- Moves JAnsi support to a new `log4j-jansi` module.
- Upgrades the JAnsi library to version 2.x

Fixes #1736.
@ppkarwasz ppkarwasz added this to the 3.x milestone Sep 2, 2024
@ppkarwasz ppkarwasz marked this pull request as draft September 2, 2024 16:26
@ppkarwasz
Copy link
Contributor Author

This is blocked by #2691: if the TextRenderer interface is removed from the %ex pattern converter, the JAnsiTextRenderer can be removed, otherwise it should be kept.

JANSI is deprecated, see fusesource/jansi#294 for details.
We rewrite `JAnsiTextRenderer` to use our internal `AnsiEscape` util
instead of the external JAnsi library.
@ppkarwasz
Copy link
Contributor Author

I rewrite JAnsiTextRenderer to use our internal AnsiEscape ANSI sequence generator, instead of relying on JANSI.
Cf. 5ae37df

@ppkarwasz ppkarwasz marked this pull request as ready for review September 3, 2024 10:35
@ppkarwasz ppkarwasz requested a review from vy September 3, 2024 10:47
@ppkarwasz ppkarwasz self-assigned this Sep 3, 2024
Copy link
Member

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Module should not be named "log4j-jansi" since the PR no longer uses JAnsi. How about "log4j-ansi"?

@ppkarwasz
Copy link
Contributor Author

Module should not be named "log4j-jansi" since the PR no longer uses JAnsi. How about "log4j-ansi"?

The module uses org.jline:jansi-core instead of org.fusesource.jansi:jansi, so I believe the name is appropriate.

The JAnsiTextRenderer (which is part of log4j-core, not log4j-jansi) effectively could be renamed AnsiTextRenderer. Keeping the same name, however will allow me to backport part of this PR to 2.x.

@garydgregory
Copy link
Member

@ppkarwasz
TY for the explanation: -)
+1

Copy link
Member

@vy vy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my remark about removing the JAnsi support, completely.

This is blocked by #2691: if the TextRenderer interface is removed from the %ex pattern converter, the JAnsiTextRenderer can be removed, otherwise it should be kept.

#2691 cannot remove anything, since it is against 2.x. But be my guest to completely remove it while porting it to main. Hence, I see two paths we can take:

  1. Wait for Consolidate stack trace rendering in Pattern Layout #2691 to get merged to 2.x, port it to main, and then remove TextRenderer in main.
  2. Remove TextRenderer in main without waiting for Consolidate stack trace rendering in Pattern Layout #2691.

[@alan0428a, I am CC'ing you, in case you're interested in.]

src/site/antora/modules/ROOT/pages/manual/appenders.adoc Outdated Show resolved Hide resolved
@ppkarwasz
Copy link
Contributor Author

See my remark about removing the JAnsi support, completely.

This is blocked by #2691: if the TextRenderer interface is removed from the %ex pattern converter, the JAnsiTextRenderer can be removed, otherwise it should be kept.

#2691 cannot remove anything, since it is against 2.x.

Of course you can not remove it, but as far as I can tell, you are bypassing it. Since ThrowableProxy and related classes are not used any more, TextRenderer isn't used either. Am I right?

@ppkarwasz
Copy link
Contributor Author

Note that in this PR we are discussing 3 related, but distinct features:

Enabling ANSI escapes on Windows terminals

This feature is probably useless and can be dropped. It requires the JAnsi library.

Detecting if the console is a terminal

The JAnsi library is very good at it. It can detect separately if stdout and stderr are TTY devices on UNIX or character devices on Windows. If the stream is not attached to a TTY, the ANSI escape sequences will be dropped.

In Log4j Core 2 we emulate this behavior by checking if System.console() exists (cf. noConsoleNoAnsi. Iit exists if both stdin and stdout are connected to a terminal). Java 22 adds a new Console.isTerminal() method, but I am unsure how it works.

Coloring of stack traces and messages

In Log4j Core 2, JAnsi was also used to color stack traces and messages. This is enabled by adding ansi as an option to %ex, %xEx, %rEx or %m. The JAnsi library is not needed for %style or %highlight.

This PR rewrites JAnsiTextRenderer to work without JAnsi.

@ppkarwasz
Copy link
Contributor Author

@garydgregory,

How do you feel about removing JAnsi (but not ANSI) support as a test? For this PR it would mean:

  • I remove the log4j-jansi module,
  • I keep the modifications to JAnsiTextRenderer and I port them to 2.x.

@garydgregory
Copy link
Member

Hi @ppkarwasz

ATM, I only care that my Log4j 2 XML configurations don't break and keep their current colorized output. However that happens should be an implementation detail.

@ppkarwasz ppkarwasz merged commit 713ec50 into main Oct 7, 2024
9 checks passed
@ppkarwasz ppkarwasz deleted the feature/main/jansi-2 branch October 7, 2024 11:31
@ppkarwasz
Copy link
Contributor Author

@vy,

Should I port the changes to JAnsiTextRenderer to 2.x? It was used to support the ansi option in the %m, %ex, %xEx and %rEx pattern converters. Now only the %m{ansi} pattern converter requires it.

@vy
Copy link
Member

vy commented Oct 7, 2024

Should I port the changes to JAnsiTextRenderer to 2.x? It was used to support the ansi option in the %m, %ex, %xEx and %rEx pattern converters. Now only the %m{ansi} pattern converter requires it.

@ppkarwasz, yes, please do so.

Curious: Does that translate to no JAnsi dependency on 2.x?

@ppkarwasz
Copy link
Contributor Author

Curious: Does that translate to no JAnsi dependency on 2.x?

Are you suggesting we drop an end-of-life dependency (Jansi 1.x) released by a project that does not exist any more (Jansi, which was replaced by JLine) and break backward compatibility for Windows platforms older than 2017? 😛

Windows 7 and previous releases have a 3.5% market share, so I think we could do it.

@vy
Copy link
Member

vy commented Oct 7, 2024

Are you suggesting we drop an end-of-life dependency (Jansi 1.x) released by a project that does not exist any more (Jansi, which was replaced by JLine) and break backward compatibility for Windows platforms older than 2017? 😛

Windows 7 and previous releases have a 3.5% market share, so I think we could do it.

@ppkarwasz, yes, let's please indeed drop the JAnsi dependency.

@ppkarwasz
Copy link
Contributor Author

Done in #3070

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants