Skip to content

Commit

Permalink
Add system properties flags for queue router
Browse files Browse the repository at this point in the history
  • Loading branch information
agentgt committed Oct 5, 2024
1 parent 78f49c6 commit 6015023
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 10 deletions.
3 changes: 2 additions & 1 deletion core/src/main/java/io/jstach/rainbowgum/LevelResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ static LevelResolver cached(LevelResolver resolver) {
enum StaticLevelResolver implements LevelConfig {

ALL(Level.ALL), //
TRACE(Level.TRACE), INFO(Level.INFO), //
TRACE(Level.TRACE), //
INFO(Level.INFO), //
DEBUG(Level.DEBUG), //
ERROR(Level.ERROR), //
WARNING(Level.WARNING), //
Expand Down
13 changes: 13 additions & 0 deletions core/src/main/java/io/jstach/rainbowgum/LogProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,19 @@ public interface LogProperties {
*/
static final String GLOBAL_CHANGE_PROPERTY = ROOT_PREFIX + "global.change";

/**
* The level threshold of events captured before Rainbow Gum is fully bound. Before
* Rainbow Gum is bound events are queued. By default events that are below
* <code>INFO</code> are not queued.
*/
static final String GLOBAL_QUEUE_LEVEL_PROPERTY = ROOT_PREFIX + "global.queue.level";

/**
* The level threshold of events that will be outputted to standard error if Rainbow
* Gum is not bound yet. events that are below <code>INFO</code> are not queued.
*/
static final String GLOBAL_QUEUE_ERROR_PROPERTY = ROOT_PREFIX + "global.queue.error";

/**
* Will globally turn of any ANSI escape output as well disable extensions that do
* things for ANSI escape like JANSI.
Expand Down
41 changes: 34 additions & 7 deletions core/src/main/java/io/jstach/rainbowgum/LogRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -776,18 +776,46 @@ final class QueueEventsRouter implements InternalRootRouter, Route {

private final ConcurrentLinkedQueue<LogEvent> events = new ConcurrentLinkedQueue<>();

private static final LevelResolver INFO_RESOLVER = StaticLevelResolver.of(Level.INFO);
private final LevelResolver levelResolver;

private final LevelResolver errorLevelResolver;

private final RouteChangePublisher changePublisher = new RouteChangePublisher(s -> true);

static QueueEventsRouter of() {
return new QueueEventsRouter(StaticLevelResolver.of(queueLevel()), StaticLevelResolver.of(errorLevel()));
}

private static Level queueLevel() {
return LogProperty.builder()
.build(LogProperties.GLOBAL_QUEUE_LEVEL_PROPERTY)
.map(LevelResolver::parseLevel)
.get(LogProperties.StandardProperties.SYSTEM_PROPERTIES)
.value(Level.INFO);
}

private static final Level errorLevel() {
return LogProperty.builder()
.build(LogProperties.GLOBAL_QUEUE_ERROR_PROPERTY)
.map(LevelResolver::parseLevel)
.get(LogProperties.StandardProperties.SYSTEM_PROPERTIES)
.value(Level.ERROR);
}

private QueueEventsRouter(LevelResolver levelResolver, LevelResolver errorLevelResolver) {
super();
this.levelResolver = levelResolver;
this.errorLevelResolver = errorLevelResolver;
}

@Override
public LevelResolver levelResolver() {
return INFO_RESOLVER;
return levelResolver;
}

@Override
public Route route(String loggerName, Level level) {
if (INFO_RESOLVER.isEnabled(loggerName, level)) {
if (levelResolver.isEnabled(loggerName, level)) {
return this;
}
return Routes.NotFound;
Expand All @@ -805,10 +833,9 @@ public void close() {
@Override
public void log(LogEvent event) {
events.add(event);
if (event.level() == Level.ERROR) {
if (errorLevelResolver.isEnabled(event.loggerName(), event.level())) {
MetaLog.error(event);
}

}

@Override
Expand Down Expand Up @@ -836,7 +863,7 @@ enum GlobalLogRouter implements InternalRootRouter, Route {

INSTANCE;

private volatile InternalRootRouter delegate = new QueueEventsRouter();
private volatile InternalRootRouter delegate = QueueEventsRouter.of();

private final ReentrantLock drainLock = new ReentrantLock();

Expand Down Expand Up @@ -920,7 +947,7 @@ public void start(LogConfig config) {

@Override
public void close() {
var d = _drain(new QueueEventsRouter());
var d = _drain(QueueEventsRouter.of());
d.close();
}

Expand Down
9 changes: 9 additions & 0 deletions doc/overview.html
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,15 @@ <h3 id="system_logger">java.lang.System.Logger and java.util.logging</h3>
(the SLF4J adapter will initialize Rainbow Gum on System.Logger usage if using {@link io.jstach.rainbowgum.slf4j/ })
</li>
</ul>
<p>
The following System properties allow greater control of the queueing and initialization.
Because of how early initialization can happen this configuration can only be done with System properties.
</p>
<ul>
<li>{@value io.jstach.rainbowgum.LogProperties#GLOBAL_QUEUE_LEVEL_PROPERTY} = level</li>
<li>{@value io.jstach.rainbowgum.LogProperties#GLOBAL_QUEUE_ERROR_PROPERTY} = level</li>
<li>{@value io.jstach.rainbowgum.systemlogger.RainbowGumSystemLoggerFinder#INTIALIZE_RAINBOW_GUM_PROPERTY} = init option</li>
</ul>
<p>
<em>
<strong>NOTE:</strong>
Expand Down
10 changes: 8 additions & 2 deletions rainbowgum-jdk/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@
* to never load and thus never replay the events and you will not be able to
* figure out what happened. If no
* {@link io.jstach.rainbowgum.spi.RainbowGumServiceProvider.RainbowGumEagerLoad}
* is found the SystemLogger will initialize Rainbow Gum.
* is found the SystemLogger will initialize Rainbow Gum. You can change the queuing
* threshold and error level outputting with System properties:
* </p>
* <ul>
* <li>{@value io.jstach.rainbowgum.LogProperties#GLOBAL_QUEUE_LEVEL_PROPERTY} = level</li>
* <li>{@value io.jstach.rainbowgum.LogProperties#GLOBAL_QUEUE_ERROR_PROPERTY} = level</li>
* </ul>
* <p>
* SLF4J does <a href="https://www.slf4j.org/manual.html#jep264">provide an
* adapter/bridge for the System.Logger
Expand All @@ -35,7 +40,8 @@
* </ul>
* An alternative to using the SLF4J bridge if eager initialization is desired is
* to set a System property with
* {@value io.jstach.rainbowgum.jdk.systemlogger.SystemLoggingFactory#INTIALIZE_RAINBOW_GUM_PROPERTY} to
* <code>{@value io.jstach.rainbowgum.jdk.systemlogger.SystemLoggingFactory#INTIALIZE_RAINBOW_GUM_PROPERTY}</code>
* to
* the values in {@link io.jstach.rainbowgum.systemlogger.RainbowGumSystemLoggerFinder.InitOption}.
* however that maybe difficult if one cannot set system properties before loading logging.
* <p>
Expand Down

0 comments on commit 6015023

Please sign in to comment.