Repository navigation
CAMEL-26937: camel-jbang - the short commands start without log4j, and print without a JLine terminal when piped #30016
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...g-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/CamelCommandColorsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.camel.dsl.jbang.core.commands; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.PrintStream; | ||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| import org.apache.camel.dsl.jbang.core.common.EnvironmentHelper; | ||
| import org.jline.jansi.Ansi; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.junit.jupiter.api.Assumptions.assumeFalse; | ||
|
|
||
| /** | ||
| * Without a terminal, a command prints its colored text without the colors itself, as the console would, without | ||
| * building the JLine terminal that the console needs to find that out (CAMEL-26937). | ||
| */ | ||
| class CamelCommandColorsTest { | ||
|
|
||
| @Test | ||
| void coloredTextWithoutTerminalIsPrintedWithoutColors() throws Exception { | ||
| assumeFalse(EnvironmentHelper.hasTerminal(), "runs without a terminal"); | ||
|
|
||
| String out = printToConsole(new ColoredCommand(new CamelJBangMain())); | ||
|
|
||
| assertThat(out).isEqualTo("red and bold" + System.lineSeparator() + "plain"); | ||
| } | ||
|
|
||
| @Test | ||
| void consoleStripsTheColorsWithoutTerminal() { | ||
| assumeFalse(EnvironmentHelper.hasTerminal(), "runs without a terminal"); | ||
|
|
||
| assertThat(new ColoredCommand(new CamelJBangMain()).colorsStripped()).isTrue(); | ||
| } | ||
|
|
||
| private static String printToConsole(CamelCommand command) throws Exception { | ||
| PrintStream previous = System.out; | ||
| ByteArrayOutputStream bytes = new ByteArrayOutputStream(); | ||
| System.setOut(new PrintStream(bytes, true, StandardCharsets.UTF_8)); | ||
| try { | ||
| command.doCall(); | ||
| } finally { | ||
| System.setOut(previous); | ||
| } | ||
| return bytes.toString(StandardCharsets.UTF_8); | ||
| } | ||
|
|
||
| private static final class ColoredCommand extends CamelCommand { | ||
|
|
||
| ColoredCommand(CamelJBangMain main) { | ||
| super(main); | ||
| } | ||
|
|
||
| @Override | ||
| public Integer doCall() { | ||
| printlnColored(Ansi.ansi().fgRed().a("red").reset().a(" and ").bold().a("bold").reset()); | ||
| printColored(Ansi.ansi().a("plain")); | ||
| return 0; | ||
| } | ||
| } | ||
| } |
218 changes: 218 additions & 0 deletions
218
...amel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/common/CliLoggerProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.camel.dsl.jbang.run.common; | ||
|
|
||
| import java.util.Deque; | ||
| import java.util.Map; | ||
| import java.util.ServiceLoader; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import org.slf4j.ILoggerFactory; | ||
| import org.slf4j.IMarkerFactory; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.helpers.NOP_FallbackServiceProvider; | ||
| import org.slf4j.helpers.SubstituteLogger; | ||
| import org.slf4j.spi.MDCAdapter; | ||
| import org.slf4j.spi.SLF4JServiceProvider; | ||
|
|
||
| /** | ||
| * The SLF4J provider of a Camel CLI started from the command line: its loggers log nothing until the CLI configures the | ||
| * logging ({@link RuntimeUtil#configureLog}), and then log with the SLF4J provider on the classpath (log4j). | ||
| * <p/> | ||
| * Most commands, such as camel ps or camel get, turn the logging off, and something that only creates a logger (a | ||
| * static logger of a class of Camel) then made log4j start, which is about a third of such a command (CAMEL-26937). | ||
| * Log4j already logged nothing until the CLI configured it (CAMEL-26429), so the commands log as before. The main | ||
| * method of the CLI installs it, see {@link CliLogging#install()}. | ||
| */ | ||
| public final class CliLoggerProvider implements SLF4JServiceProvider { | ||
|
|
||
| private final CliLoggerFactory loggerFactory = new CliLoggerFactory(); | ||
| private final MDCAdapter mdcAdapter = new CliMDCAdapter(this); | ||
| private final Supplier<SLF4JServiceProvider> delegateFinder; | ||
| private volatile SLF4JServiceProvider delegate; | ||
|
|
||
| /** | ||
| * The provider that SLF4J creates, as the system property {@code slf4j.provider} names it. | ||
| */ | ||
| public CliLoggerProvider() { | ||
| this(CliLoggerProvider::findProvider); | ||
| } | ||
|
|
||
| CliLoggerProvider(Supplier<SLF4JServiceProvider> delegateFinder) { | ||
| this.delegateFinder = delegateFinder; | ||
| } | ||
|
|
||
| /** | ||
| * The CLI configured the logging: from now on the loggers log with the provider on the classpath. | ||
| */ | ||
| void configured() { | ||
| loggerFactory.delegateTo(delegate().getLoggerFactory()); | ||
| } | ||
|
|
||
| @Override | ||
| public void initialize() { | ||
| // SLF4J chose this provider, as something asked for a logger | ||
| CliLogging.bound(this::configured); | ||
| } | ||
|
|
||
| @Override | ||
| public ILoggerFactory getLoggerFactory() { | ||
| return loggerFactory; | ||
| } | ||
|
|
||
| @Override | ||
| public IMarkerFactory getMarkerFactory() { | ||
| return delegate().getMarkerFactory(); | ||
| } | ||
|
|
||
| @Override | ||
| public MDCAdapter getMDCAdapter() { | ||
| // SLF4J asks for it when it chooses the provider, before anything logs | ||
| return mdcAdapter; | ||
| } | ||
|
|
||
| @Override | ||
| public String getRequestedApiVersion() { | ||
| return "2.0.99"; | ||
| } | ||
|
|
||
| // the provider on the classpath, which is found as SLF4J would without this one | ||
| private SLF4JServiceProvider delegate() { | ||
| SLF4JServiceProvider answer = delegate; | ||
| if (answer == null) { | ||
| synchronized (this) { | ||
| answer = delegate; | ||
| if (answer == null) { | ||
| answer = delegateFinder.get(); | ||
| answer.initialize(); | ||
| delegate = answer; | ||
| } | ||
| } | ||
| } | ||
| return answer; | ||
| } | ||
|
|
||
| private static SLF4JServiceProvider findProvider() { | ||
| ClassLoader cl = CliLoggerProvider.class.getClassLoader(); | ||
| for (SLF4JServiceProvider provider : ServiceLoader.load(SLF4JServiceProvider.class, cl)) { | ||
| if (!(provider instanceof CliLoggerProvider)) { | ||
| return provider; | ||
| } | ||
| } | ||
| return new NOP_FallbackServiceProvider(); | ||
| } | ||
|
|
||
| /** | ||
| * The loggers created before the logging is configured stand in for the loggers of the provider on the classpath, | ||
| * which they log with once it is. | ||
| */ | ||
| static final class CliLoggerFactory implements ILoggerFactory { | ||
|
|
||
| private final Map<String, SubstituteLogger> loggers = new ConcurrentHashMap<>(); | ||
| private volatile ILoggerFactory delegate; | ||
|
|
||
| @Override | ||
| public Logger getLogger(String name) { | ||
| ILoggerFactory factory = delegate; | ||
| if (factory != null) { | ||
| return factory.getLogger(name); | ||
| } | ||
| synchronized (this) { | ||
| factory = delegate; | ||
| if (factory != null) { | ||
| return factory.getLogger(name); | ||
| } | ||
| // a substitute logger without a delegate logs nothing | ||
| return loggers.computeIfAbsent(name, n -> new SubstituteLogger(n, null, true)); | ||
| } | ||
| } | ||
|
|
||
| synchronized void delegateTo(ILoggerFactory factory) { | ||
| if (delegate == null) { | ||
| loggers.forEach((name, logger) -> logger.setDelegate(factory.getLogger(name))); | ||
| delegate = factory; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The MDC of the provider on the classpath, which its layouts read, created when something first uses the MDC. | ||
| */ | ||
| static final class CliMDCAdapter implements MDCAdapter { | ||
|
|
||
| private final CliLoggerProvider provider; | ||
|
|
||
| CliMDCAdapter(CliLoggerProvider provider) { | ||
| this.provider = provider; | ||
| } | ||
|
|
||
| private MDCAdapter adapter() { | ||
| return provider.delegate().getMDCAdapter(); | ||
| } | ||
|
|
||
| @Override | ||
| public void put(String key, String val) { | ||
| adapter().put(key, val); | ||
| } | ||
|
|
||
| @Override | ||
| public String get(String key) { | ||
| return adapter().get(key); | ||
| } | ||
|
|
||
| @Override | ||
| public void remove(String key) { | ||
| adapter().remove(key); | ||
| } | ||
|
|
||
| @Override | ||
| public void clear() { | ||
| adapter().clear(); | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, String> getCopyOfContextMap() { | ||
| return adapter().getCopyOfContextMap(); | ||
| } | ||
|
|
||
| @Override | ||
| public void setContextMap(Map<String, String> contextMap) { | ||
| adapter().setContextMap(contextMap); | ||
| } | ||
|
|
||
| @Override | ||
| public void pushByKey(String key, String value) { | ||
| adapter().pushByKey(key, value); | ||
| } | ||
|
|
||
| @Override | ||
| public String popByKey(String key) { | ||
| return adapter().popByKey(key); | ||
| } | ||
|
|
||
| @Override | ||
| public Deque<String> getCopyOfDequeByKey(String key) { | ||
| return adapter().getCopyOfDequeByKey(key); | ||
| } | ||
|
|
||
| @Override | ||
| public void clearDequeByKey(String key) { | ||
| adapter().clearDequeByKey(key); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.